|
|
@ -18,7 +18,7 @@ import socket
|
|
|
|
import SafariChess_backend as backend
|
|
|
|
import SafariChess_backend as backend
|
|
|
|
from SafariChess_Classes import Button, DropDown
|
|
|
|
from SafariChess_Classes import Button, DropDown
|
|
|
|
from threading import Thread, Lock
|
|
|
|
from threading import Thread, Lock
|
|
|
|
from itertools import cycle
|
|
|
|
from itertools import count, cycle
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
#设置棋盘的参数
|
|
|
|
#设置棋盘的参数
|
|
|
@ -36,7 +36,7 @@ DIMENSION_COLUMNS = 9
|
|
|
|
SIZE = 64
|
|
|
|
SIZE = 64
|
|
|
|
IMAGES = {}
|
|
|
|
IMAGES = {}
|
|
|
|
bias_top = 100 #棋盘的上边距
|
|
|
|
bias_top = 100 #棋盘的上边距
|
|
|
|
bias_left = 100 #棋盘的左边距
|
|
|
|
bias_left = 20 #棋盘的左边距
|
|
|
|
#网络道具
|
|
|
|
#网络道具
|
|
|
|
client = None
|
|
|
|
client = None
|
|
|
|
networkMsg = None
|
|
|
|
networkMsg = None
|
|
|
@ -236,17 +236,16 @@ def MainMenu():
|
|
|
|
button_2 = Button(bt2,(300,400))
|
|
|
|
button_2 = Button(bt2,(300,400))
|
|
|
|
button_3 = Button(bt3,(300,450))
|
|
|
|
button_3 = Button(bt3,(300,450))
|
|
|
|
button_4 = Button(bt4,(300,500))
|
|
|
|
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_INACTIVE = (255, 108, 0)
|
|
|
|
COLOR_ACTIVE = (255, 208, 0)
|
|
|
|
COLOR_ACTIVE = (255, 208, 0)
|
|
|
@ -288,12 +287,13 @@ def MainMenu():
|
|
|
|
pg.quit()
|
|
|
|
pg.quit()
|
|
|
|
sys.exit()
|
|
|
|
sys.exit()
|
|
|
|
if music_button.rect.collidepoint(event.pos):
|
|
|
|
if music_button.rect.collidepoint(event.pos):
|
|
|
|
if music.get_volume() == 0:
|
|
|
|
|
|
|
|
music.set_volume(0.5)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
music.set_volume(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if pg.mixer.music.get_volume() == 0:
|
|
|
|
|
|
|
|
pg.mixer.music.set_volume(0.5)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
pg.mixer.music.set_volume(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
screen.blit(bg, (0, 0))
|
|
|
|
screen.blit(bg, (0, 0))
|
|
|
@ -309,8 +309,6 @@ def MainMenu():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#绘制Text在屏幕上
|
|
|
|
#绘制Text在屏幕上
|
|
|
|
def showText(screen,fontObj,text,x,y):
|
|
|
|
def showText(screen,fontObj,text,x,y):
|
|
|
|
textSurfaceObj = fontObj.render(text, True, pg.Color('red'),pg.Color('white'))# 配置要显示的文字
|
|
|
|
textSurfaceObj = fontObj.render(text, True, pg.Color('red'),pg.Color('white'))# 配置要显示的文字
|
|
|
@ -319,6 +317,16 @@ def showText(screen,fontObj,text,x,y):
|
|
|
|
screen.blit(textSurfaceObj, textRectObj)# 绘制字体
|
|
|
|
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
|
|
|
|
game_over = False
|
|
|
|
square_selected = () #刚开始没有选择任何一个棋子
|
|
|
|
square_selected = () #刚开始没有选择任何一个棋子
|
|
|
|
click_queue = [] #点击队列,记录第一次点击和第二次点击位置,方便移动棋子
|
|
|
|
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)
|
|
|
|
screen.blit(button_music.image, button_music.rect)
|
|
|
|
|
|
|
|
|
|
|
|
pg.display.update()
|
|
|
|
pg.display.update()
|
|
|
|
startGamePage(mode)
|
|
|
|
startGamePage(mode) #游戏进入前,可以进一步优化一些显示细节
|
|
|
|
|
|
|
|
|
|
|
|
if mode == 1:
|
|
|
|
if mode == 1:
|
|
|
|
while running:
|
|
|
|
while running:
|
|
|
|
for e in pg.event.get():
|
|
|
|
for e in pg.event.get():
|
|
|
@ -390,11 +396,7 @@ def main(mode):
|
|
|
|
elif e.type == pg.KEYDOWN:
|
|
|
|
elif e.type == pg.KEYDOWN:
|
|
|
|
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
|
|
|
|
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
elif e.type == COUNT:# 判断事件是否为计时事件
|
|
|
|
|
|
|
|
counts=counts+1
|
|
|
|
|
|
|
|
countstext=str(counts)
|
|
|
|
|
|
|
|
showText(screen,bigfont,countstext,200,350)
|
|
|
|
|
|
|
|
pg.display.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if mademove:
|
|
|
|
if mademove:
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
@ -405,26 +407,10 @@ def main(mode):
|
|
|
|
game_over = True
|
|
|
|
game_over = True
|
|
|
|
clock.tick(60)
|
|
|
|
clock.tick(60)
|
|
|
|
pg.display.flip()
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
|
|
|
|
elif mode == 2:
|
|
|
|
elif mode == 2:
|
|
|
|
##一些按钮实现
|
|
|
|
counts=20 #双方倒计时
|
|
|
|
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()
|
|
|
|
startNetworkServices()
|
|
|
|
|
|
|
|
|
|
|
|
#! start login
|
|
|
|
#! start login
|
|
|
|
myPlayerData = {
|
|
|
|
myPlayerData = {
|
|
|
|
'type': 0, # game state type ?
|
|
|
|
'type': 0, # game state type ?
|
|
|
@ -451,7 +437,19 @@ def main(mode):
|
|
|
|
|
|
|
|
|
|
|
|
pg.display.flip()
|
|
|
|
pg.display.flip()
|
|
|
|
#! end login
|
|
|
|
#! 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:
|
|
|
|
while running:
|
|
|
|
#! 特别注意:红方是0,蓝方是1
|
|
|
|
#! 特别注意:红方是0,蓝方是1
|
|
|
|
#print('current moving color: ',game_state.color())
|
|
|
|
#print('current moving color: ',game_state.color())
|
|
|
@ -506,6 +504,10 @@ def main(mode):
|
|
|
|
game_state.makeMove(theMove)
|
|
|
|
game_state.makeMove(theMove)
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
other_stage = not other_stage
|
|
|
|
other_stage = not other_stage
|
|
|
|
|
|
|
|
counts=20
|
|
|
|
|
|
|
|
#轮到的方
|
|
|
|
|
|
|
|
showText(screen,bigfont,backend.GameState().color() ,700,350)
|
|
|
|
|
|
|
|
pg.display.update()
|
|
|
|
game_state.conquer()#update conquer stage
|
|
|
|
game_state.conquer()#update conquer stage
|
|
|
|
|
|
|
|
|
|
|
|
thisMove = None
|
|
|
|
thisMove = None
|
|
|
@ -547,12 +549,22 @@ def main(mode):
|
|
|
|
click_queue = []
|
|
|
|
click_queue = []
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
click_queue = [square_selected]
|
|
|
|
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:
|
|
|
|
if mademove:
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
mademove = False
|
|
|
|
mademove = False
|
|
|
|
if isOnline:
|
|
|
|
if isOnline:
|
|
|
|
print('waiting for the other player to move...')
|
|
|
|
print('waiting for the other player to move...')
|
|
|
|
|
|
|
|
|
|
|
|
other_stage = not other_stage
|
|
|
|
other_stage = not other_stage
|
|
|
|
thisMoveJson = {
|
|
|
|
thisMoveJson = {
|
|
|
|
'type': 1,
|
|
|
|
'type': 1,
|
|
|
@ -572,10 +584,10 @@ def main(mode):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client.send(json.dumps(thisMoveJson).encode('utf-8'))
|
|
|
|
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:
|
|
|
|
if game_state.conquer() and not game_over:
|
|
|
|
print("GAME OVER!")
|
|
|
|
print("GAME OVER!")
|
|
|
|
#
|
|
|
|
#
|
|
|
@ -602,9 +614,6 @@ def main(mode):
|
|
|
|
showGameOverText(screen,"player "+ ("b" if time_out_side==1 else "r")+" timed out") #超时了
|
|
|
|
showGameOverText(screen,"player "+ ("b" if time_out_side==1 else "r")+" timed out") #超时了
|
|
|
|
|
|
|
|
|
|
|
|
clock.tick(50)
|
|
|
|
clock.tick(50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pg.display.flip()
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|