diff --git a/Image/music.bmp b/Image/music.bmp index 230d382..12b3713 100644 Binary files a/Image/music.bmp and b/Image/music.bmp differ diff --git a/Image/music1.bmp b/Image/music1.bmp new file mode 100644 index 0000000..230d382 Binary files /dev/null and b/Image/music1.bmp differ diff --git a/Image/restart.bmp b/Image/restart.bmp new file mode 100644 index 0000000..004d9e7 Binary files /dev/null and b/Image/restart.bmp differ diff --git a/Image/叫停.bmp b/Image/叫停.bmp index 19cc4f2..b0b6af3 100644 Binary files a/Image/叫停.bmp and b/Image/叫停.bmp differ diff --git a/Image/认输.bmp b/Image/认输.bmp new file mode 100644 index 0000000..84ac09f Binary files /dev/null and b/Image/认输.bmp differ diff --git a/Image/退出.bmp b/Image/退出.bmp new file mode 100644 index 0000000..11828a1 Binary files /dev/null and b/Image/退出.bmp differ diff --git a/SafariChess_Gamev1.0.py b/SafariChess_Gamev1.0.py index 0e20afd..6740153 100644 --- a/SafariChess_Gamev1.0.py +++ b/SafariChess_Gamev1.0.py @@ -18,7 +18,7 @@ import socket import SafariChess_backend as backend from SafariChess_Classes import Button, DropDown from threading import Thread, Lock -from itertools import cycle +from itertools import count, cycle import time #设置棋盘的参数 @@ -36,7 +36,7 @@ DIMENSION_COLUMNS = 9 SIZE = 64 IMAGES = {} bias_top = 100 #棋盘的上边距 -bias_left = 100 #棋盘的左边距 +bias_left = 20 #棋盘的左边距 #网络道具 client = None networkMsg = None @@ -236,18 +236,17 @@ def MainMenu(): 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)) + music_button = Button(music_settings,(700,0)) - #音乐设置 - pg.mixer.init() - music = pg.mixer.Sound("./Image/music1.mp3") - music.set_volume(0) - music.play() - pg.display.flip() - + #音乐设置 + file=r'./Image/music1.mp3' #要播放的歌曲本地地址 + pg.mixer.init() #mixer的初始化 + music = pg.mixer.music.load(file) #载入一个音乐文件用于播放 + pg.mixer.music.play(-1) #播放音乐,-1表示循环播放 + COLOR_INACTIVE = (255, 108, 0) COLOR_ACTIVE = (255, 208, 0) COLOR_LIST_INACTIVE = (0, 143, 90) @@ -287,14 +286,15 @@ 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) + if music_button.rect.collidepoint(event.pos): + + if pg.mixer.music.get_volume() == 0: + pg.mixer.music.set_volume(0.5) else: - music.set_volume(0) - - - + pg.mixer.music.set_volume(0) + + pg.display.flip() + screen.blit(bg, (0, 0)) screen.blit(button_1.image, button_1.rect) @@ -309,8 +309,6 @@ def MainMenu(): - - #绘制Text在屏幕上 def showText(screen,fontObj,text,x,y): textSurfaceObj = fontObj.render(text, True, pg.Color('red'),pg.Color('white'))# 配置要显示的文字 @@ -318,8 +316,18 @@ def showText(screen,fontObj,text,x,y): textRectObj.center = (x, y)# 设置显示对象的坐标 screen.blit(textSurfaceObj, textRectObj)# 绘制字体 - - + +def drawButton22(screen): + ##一些按钮的实例化 + button_stop = Button(pg.transform.scale(pg.image.load("Image/叫停.bmp"),(100,50)),(400,0)) + screen.blit(button_stop.image, button_stop.rect) + button_quit = Button(pg.transform.scale(pg.image.load("Image/退出.bmp"),(100,50)),(300,0)) + screen.blit(button_quit.image, button_quit.rect) + button_fail = Button(pg.transform.scale(pg.image.load("Image/认输.bmp"),(100,50)),(500,0)) + screen.blit(button_fail.image, button_fail.rect) + button_restart = Button(pg.transform.scale(pg.image.load("Image/restart.bmp"),(100,50)),(100,0)) + screen.blit(button_restart.image, button_restart.rect) + @@ -348,13 +356,11 @@ def main(mode): game_over = False square_selected = () #刚开始没有选择任何一个棋子 click_queue = [] #点击队列,记录第一次点击和第二次点击位置,方便移动棋子 - - button_music = Button(pg.transform.scale(pg.image.load("Image/music.bmp"),(100,50)),(500,0)) + ##音乐按钮,均有 + button_music = Button(pg.transform.scale(pg.image.load("Image/music.bmp"),(80,40)),(650,0)) screen.blit(button_music.image, button_music.rect) - pg.display.update() - startGamePage(mode) - + startGamePage(mode) #游戏进入前,可以进一步优化一些显示细节 if mode == 1: while running: for e in pg.event.get(): @@ -390,11 +396,7 @@ 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() @@ -405,26 +407,10 @@ def main(mode): game_over = True 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() + counts=20 #双方倒计时 startNetworkServices() - #! start login myPlayerData = { 'type': 0, # game state type ? @@ -451,7 +437,19 @@ def main(mode): pg.display.flip() #! end login + # 自定义计时事件-----计时器实现 + COUNTS = pg.USEREVENT +1 + # 每隔1秒发送一次自定义事件 + pg.time.set_timer(COUNTS,1000) #以毫秒为单位 + bigfont = pg.font.SysFont("Consolas", 20) + minfont = pg.font.SysFont("Courier", 18) + showText(screen,bigfont,"RemainTime:",680,200) #650,250为时间,650,350为对方轮数 + showText(screen,minfont,"Turn:",650,300) + clock = pg.time.Clock() + clock.tick(60) + + drawButton22(screen) while running: #! 特别注意:红方是0,蓝方是1 #print('current moving color: ',game_state.color()) @@ -501,11 +499,15 @@ def main(mode): game_over = True #Move handler - elif 'src' and 'dst' in networkMsg.keys(): + elif 'src' and 'dst' in networkMsg.keys(): theMove = backend.Move([networkMsg['src']['y'],networkMsg['src']['x']],[networkMsg['dst']['y'],networkMsg['dst']['x']],game_state.board) game_state.makeMove(theMove) valid_moves = game_state.getAllMoves() other_stage = not other_stage + counts=20 + #轮到的方 + showText(screen,bigfont,backend.GameState().color() ,700,350) + pg.display.update() game_state.conquer()#update conquer stage thisMove = None @@ -547,12 +549,22 @@ def main(mode): click_queue = [] else: click_queue = [square_selected] - + elif e.type == COUNTS:# 判断事件是否为计时事件 + counts -= 1 + text = str(counts).rjust(3) if counts >= 0 else '0' + showText(screen,bigfont,text,650,250) + pg.display.update() + + + + + if mademove: valid_moves = game_state.getAllMoves() mademove = False if isOnline: - print('waiting for the other player to move...') + print('waiting for the other player to move...') + other_stage = not other_stage thisMoveJson = { 'type': 1, @@ -571,11 +583,11 @@ def main(mode): } } client.send(json.dumps(thisMoveJson).encode('utf-8')) - #思路变成:定时对敌方进行扫描,若收到更新相关包则进行局面更新。 - ShowGameState(screen,game_state,valid_moves,square_selected) + ShowGameState(screen,game_state,valid_moves,square_selected) + if game_state.conquer() and not game_over: print("GAME OVER!") # @@ -601,10 +613,7 @@ def main(mode): if time_out_side != None: showGameOverText(screen,"player "+ ("b" if time_out_side==1 else "r")+" timed out") #超时了 - clock.tick(50) - - - + clock.tick(50) pg.display.flip() if __name__ == '__main__':