diff --git a/Image/MenuBg.bmp b/Image/MenuBg.bmp new file mode 100644 index 0000000..d2fb8cd Binary files /dev/null and b/Image/MenuBg.bmp differ diff --git a/Image/music.bmp b/Image/music.bmp new file mode 100644 index 0000000..230d382 Binary files /dev/null and b/Image/music.bmp differ diff --git a/Image/music1.mp3 b/Image/music1.mp3 new file mode 100644 index 0000000..2de48db Binary files /dev/null and b/Image/music1.mp3 differ diff --git a/Image/叫停.bmp b/Image/叫停.bmp new file mode 100644 index 0000000..19cc4f2 Binary files /dev/null and b/Image/叫停.bmp differ diff --git a/Image/游戏前奏.aiff b/Image/游戏前奏.aiff new file mode 100644 index 0000000..22eb11d Binary files /dev/null and b/Image/游戏前奏.aiff differ diff --git a/SafariChess_Classes.py b/SafariChess_Classes.py index 207c2f6..330c133 100644 --- a/SafariChess_Classes.py +++ b/SafariChess_Classes.py @@ -2,7 +2,7 @@ SafariChess_Classes.py Classes for button and keyboard events """ - +import pygame as pg class Button: def __init__(self, image, position, callback=None): self.image = image @@ -14,6 +14,56 @@ class Button: if self.rect.collidepoint(event.pos): self.callback(self) -#class DropDown(): -#下拉选择框 + +#下拉框 +class DropDown(): + + def __init__(self, color_menu, color_option, x, y, w, h, font, main, options): + self.color_menu = color_menu + self.color_option = color_option + self.rect = pg.Rect(x, y, w, h) + self.font = font + self.main = main + self.options = options + self.draw_menu = False + self.menu_active = False + self.active_option = -1 + + def draw(self, surf): + pg.draw.rect(surf, self.color_menu[self.menu_active], self.rect, 0) + msg = self.font.render(self.main, 1, (0, 0, 0)) + surf.blit(msg, msg.get_rect(center = self.rect.center)) + + if self.draw_menu: + for i, text in enumerate(self.options): + rect = self.rect.copy() + rect.y += (i+1) * self.rect.height + pg.draw.rect(surf, self.color_option[1 if i == self.active_option else 0], rect, 0) + msg = self.font.render(text, 1, (0, 0, 0)) + surf.blit(msg, msg.get_rect(center = rect.center)) + + def update(self, event_list): + mpos = pg.mouse.get_pos() + self.menu_active = self.rect.collidepoint(mpos) + + self.active_option = -1 + for i in range(len(self.options)): + rect = self.rect.copy() + rect.y += (i+1) * self.rect.height + if rect.collidepoint(mpos): + self.active_option = i + break + + if not self.menu_active and self.active_option == -1: + self.draw_menu = False + + for event in event_list: + if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: + if self.menu_active: + self.draw_menu = not self.draw_menu + elif self.draw_menu and self.active_option >= 0: + self.draw_menu = False + return self.active_option + return -1 + diff --git a/SafariChess_Gamev1.0.py b/SafariChess_Gamev1.0.py index 77f4ee6..132cdc6 100644 --- a/SafariChess_Gamev1.0.py +++ b/SafariChess_Gamev1.0.py @@ -16,8 +16,9 @@ import pygame as pg import sys import socket import SafariChess_backend as backend -from SafariChess_Classes import Button +from SafariChess_Classes import Button, DropDown from threading import Thread, Lock +import time #设置棋盘的参数 WIDTH = 1000 @@ -224,18 +225,44 @@ def MainMenu(): #ui screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pg.display.set_caption("SafariChess by ZY&&DQ") - bg = pg.transform.scale(pg.image.load("Image/MenuBg.jpg"), (SCREEN_WIDTH, SCREEN_HEIGHT)) + bg = pg.transform.scale(pg.image.load("Image/MenuBg.bmp"), (SCREEN_WIDTH, SCREEN_HEIGHT)) bt1 = pg.transform.scale(pg.image.load("Image/button1.jpg"),(150,40)) bt2 = pg.transform.scale(pg.image.load("Image/button2.jpg"),(150,40)) bt3 = pg.transform.scale(pg.image.load("Image/button3.jpg"),(150,40)) bt4 = pg.transform.scale(pg.image.load("Image/button4.jpg"),(150,40)) + music_settings = pg.transform.scale(pg.image.load("Image/music.bmp"),(100,40)) + + #按钮设置 #主菜单包含如下按钮: #单机游戏,双人游戏,人机对战,退出游戏 - button_1 = Button(bt1,(350,350)) - button_2 = Button(bt2,(350,400)) - button_3 = Button(bt3,(350,450)) - button_4 = Button(bt4,(350,500)) + button_1 = Button(bt1,(300,350)) + button_2 = Button(bt2,(300,400)) + button_3 = Button(bt3,(300,450)) + button_4 = Button(bt4,(300,500)) + music_button = Button(music_settings,(600,20)) + + #音乐设置 + pg.mixer.init() + music = pg.mixer.Sound("./Image/music1.mp3") + music.set_volume(0) + music.play() + pg.display.flip() + + + + + COLOR_INACTIVE = (255, 108, 0) + COLOR_ACTIVE = (255, 208, 0) + COLOR_LIST_INACTIVE = (0, 143, 90) + COLOR_LIST_ACTIVE = (3, 252, 140) + + # music_ops = DropDown([COLOR_INACTIVE, COLOR_ACTIVE],[COLOR_LIST_INACTIVE, COLOR_LIST_ACTIVE], + # 600,30, 100, 20, + # pg.font.SysFont("Courier", 20), + # "Music", + # music_lists) + mode = -1 run = True while run: @@ -264,16 +291,42 @@ def MainMenu(): if button_4.rect.collidepoint(event.pos): pg.quit() sys.exit() + if music_button.rect.collidepoint(event.pos): + if music.get_volume() == 0: + music.set_volume(0.5) + else: + music.set_volume(0) + + + + screen.blit(bg, (0, 0)) screen.blit(button_1.image, button_1.rect) screen.blit(button_2.image, button_2.rect) screen.blit(button_3.image, button_3.rect) screen.blit(button_4.image, button_4.rect) + screen.blit(music_button.image, music_button.rect) pg.display.flip() return mode + + + + +#绘制Text在屏幕上 +def showText(screen,fontObj,text,x,y): + textSurfaceObj = fontObj.render(text, True, pg.Color('red'),pg.Color('white'))# 配置要显示的文字 + textRectObj = textSurfaceObj.get_rect()# 获得要显示的对象的rect + textRectObj.center = (x, y)# 设置显示对象的坐标 + screen.blit(textSurfaceObj, textRectObj)# 绘制字体 + + + + + + def main(mode): global networkMsg networkMsg = None @@ -298,6 +351,9 @@ def main(mode): square_selected = () #刚开始没有选择任何一个棋子 click_queue = [] #点击队列,记录第一次点击和第二次点击位置,方便移动棋子 + button_music = Button(pg.transform.scale(pg.image.load("Image/music.bmp"),(100,50)),(500,0)) + screen.blit(button_music.image, button_music.rect) + pg.display.update() startGamePage(mode) @@ -336,6 +392,11 @@ def main(mode): elif e.type == pg.KEYDOWN: #设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能 pass + elif e.type == COUNT:# 判断事件是否为计时事件 + counts=counts+1 + countstext=str(counts) + showText(screen,bigfont,countstext,200,350) + pg.display.update() if mademove: valid_moves = game_state.getAllMoves() @@ -347,6 +408,23 @@ def main(mode): clock.tick(60) pg.display.flip() elif mode == 2: + ##一些按钮实现 + button_stop = Button(pg.transform.scale(pg.image.load("Image/叫停.bmp"),(100,50)),(600,0)) + screen.blit(button_stop.image, button_stop.rect) + # 初始化计时器 + counts=0 + # 自定义计时事件 + COUNT = pg.USEREVENT +1 + # 每隔1秒发送一次自定义事件 + pg.time.set_timer(COUNT,1000) + now = time.ctime()# 获得系统当前时间 + countdown_clock = now[11:19]# 格式化形式为时分秒 + bigfont = pg.font.SysFont("Courier", 32) + minfont = pg.font.SysFont("Courier", 24) + showText(screen,bigfont,"Time:",600,600) + showText(screen,minfont,"Count:",600,500) + showText(screen,bigfont,countdown_clock,600,650) + pg.display.update() startNetworkServices() myPlayerData = { 'type': 0, # game state type ? @@ -360,6 +438,7 @@ def main(mode): lastNetworkMsg = networkMsg client.send(login_packet) while networkMsg == None: pass + while running: #print('current moving color: ',game_state.color()) if lastNetworkMsg != networkMsg:#handle @@ -471,13 +550,15 @@ def main(mode): game_over = True clock.tick(5) + + pg.display.flip() if __name__ == '__main__': print("Loading...") print("Initialing game...") - #mode = MainMenu() - mode = 2 + mode = MainMenu() + #mode = 2 main(mode) diff --git a/__pycache__/SafariChess_Classes.cpython-39.pyc b/__pycache__/SafariChess_Classes.cpython-39.pyc index a8f5bb0..a0215f8 100644 Binary files a/__pycache__/SafariChess_Classes.cpython-39.pyc and b/__pycache__/SafariChess_Classes.cpython-39.pyc differ