|
|
|
@ -10,13 +10,13 @@ it handle user input and GUI
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import colorsys
|
|
|
|
|
from json import load
|
|
|
|
|
import json
|
|
|
|
|
import pygame as pg
|
|
|
|
|
import sys
|
|
|
|
|
from time import process_time
|
|
|
|
|
import socket
|
|
|
|
|
import SafariChess_backend as backend
|
|
|
|
|
from SafariChess_Classes import Button
|
|
|
|
|
import network.server as Server
|
|
|
|
|
from threading import Thread, Lock
|
|
|
|
|
|
|
|
|
|
#设置棋盘的参数
|
|
|
|
|
WIDTH = 1000
|
|
|
|
@ -34,6 +34,9 @@ SIZE = 64
|
|
|
|
|
IMAGES = {}
|
|
|
|
|
bias_top = 100 #棋盘的上边距
|
|
|
|
|
bias_left = 100 #棋盘的左边距
|
|
|
|
|
#multithreading?
|
|
|
|
|
mutex = Lock()
|
|
|
|
|
mutex_playing = Lock()
|
|
|
|
|
|
|
|
|
|
def loadImages():#加载图片,a,b双方分别有象狮豹狼狐鹰鼠七个角色
|
|
|
|
|
pieces = ["r7", "r6", "r5", "r4", "r3", "r2", "r1",
|
|
|
|
@ -80,8 +83,7 @@ def drawPieces(screen,board):
|
|
|
|
|
if piece != "00":
|
|
|
|
|
screen.blit(IMAGES[piece], pg.Rect((column * SIZE) + bias_left + 5, (row * SIZE) + bias_top + 5, SIZE - 10, SIZE - 10))
|
|
|
|
|
|
|
|
|
|
def uploadtoServer(move : backend.Move):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# square_selected 是当前选中的可以移动的棋子的位置
|
|
|
|
|
def protrudeSquares(screen,game_state,square_selected,valid_moves):
|
|
|
|
@ -234,7 +236,22 @@ def MainMenu():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(mode,p1,p2):
|
|
|
|
|
def main(mode,p1,p2,network = None):
|
|
|
|
|
NewPlayerMessage = '' #来自对手的信息
|
|
|
|
|
if mode == 2:
|
|
|
|
|
network=backend.Network()
|
|
|
|
|
player1 = input("Enter your name: ")
|
|
|
|
|
myPlayerData = {
|
|
|
|
|
'type': 0, # game state type ?
|
|
|
|
|
'msg': {
|
|
|
|
|
'name': player1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
network.send(myPlayerData)
|
|
|
|
|
print("Waiting for other player...")
|
|
|
|
|
NewPlayerMessage = network.receive()
|
|
|
|
|
print()
|
|
|
|
|
player2 = NewPlayerMessage['counterpart_name']
|
|
|
|
|
pg.init()
|
|
|
|
|
screen = pg.display.set_mode((BOARD_WIDTH + MOVE_LOG_PANEL_WIDTH, BOARD_HEIGHT))
|
|
|
|
|
pg.display.set_caption("Safafi Chess Game")
|
|
|
|
@ -242,7 +259,8 @@ def main(mode,p1,p2):
|
|
|
|
|
screen.fill(pg.Color("white"))
|
|
|
|
|
loadImages()
|
|
|
|
|
drawBoard(screen)
|
|
|
|
|
game_state=backend.GameState()
|
|
|
|
|
isOnline = bool(network != None)
|
|
|
|
|
game_state=backend.GameState(isNet=isOnline,MySide=True if not isOnline else bool(NewPlayerMessage['side']))
|
|
|
|
|
valid_moves=game_state.getAllMoves()
|
|
|
|
|
running = True
|
|
|
|
|
mademove = False
|
|
|
|
@ -250,57 +268,105 @@ def main(mode,p1,p2):
|
|
|
|
|
square_selected = ()#刚开始没有选择任何一个棋子
|
|
|
|
|
click_queue = []#点击队列,记录第一次点击和第二次点击位置,方便移动棋子
|
|
|
|
|
pg.display.update()
|
|
|
|
|
startGamePage()
|
|
|
|
|
while running:
|
|
|
|
|
for e in pg.event.get():
|
|
|
|
|
#接下来处理游戏中的事件
|
|
|
|
|
if e.type == pg.QUIT:
|
|
|
|
|
pg.quit()
|
|
|
|
|
sys.exit()
|
|
|
|
|
startGamePage(mode)
|
|
|
|
|
|
|
|
|
|
if mode == 1:
|
|
|
|
|
while running:
|
|
|
|
|
for e in pg.event.get():
|
|
|
|
|
#接下来处理游戏中的事件
|
|
|
|
|
if e.type == pg.QUIT:
|
|
|
|
|
pg.quit()
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
elif e.type == pg.MOUSEBUTTONDOWN:
|
|
|
|
|
#鼠标点击事件:用于选择棋子,移动棋子
|
|
|
|
|
if not game_over:
|
|
|
|
|
mouse_loc = pg.mouse.get_pos()
|
|
|
|
|
row = int((mouse_loc[1] - bias_top) / SIZE)
|
|
|
|
|
col = int((mouse_loc[0] - bias_left) / SIZE) #* get position of mouse click
|
|
|
|
|
if square_selected == (row,col) or col >=DIMENSION_COLUMNS or row >= DIMENSION_ROWS:
|
|
|
|
|
square_selected = ()
|
|
|
|
|
click_queue = []
|
|
|
|
|
else:
|
|
|
|
|
square_selected = (row,col)
|
|
|
|
|
click_queue.append(square_selected)
|
|
|
|
|
if(len(click_queue) == 2):
|
|
|
|
|
cur_piece_loc = click_queue[0]
|
|
|
|
|
nxt_piece_loc = click_queue[1]
|
|
|
|
|
move = backend.Move(cur_piece_loc,nxt_piece_loc,game_state.board)
|
|
|
|
|
if move in valid_moves:
|
|
|
|
|
game_state.makeMove(move)
|
|
|
|
|
mademove = True
|
|
|
|
|
square_selected = ()
|
|
|
|
|
click_queue = []
|
|
|
|
|
else:
|
|
|
|
|
click_queue = [square_selected]
|
|
|
|
|
|
|
|
|
|
elif e.type == pg.KEYDOWN:
|
|
|
|
|
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
elif e.type == pg.MOUSEBUTTONDOWN:
|
|
|
|
|
#鼠标点击事件:用于选择棋子,移动棋子
|
|
|
|
|
if not game_over:
|
|
|
|
|
mouse_loc = pg.mouse.get_pos()
|
|
|
|
|
row = int((mouse_loc[1] - bias_top) / SIZE)
|
|
|
|
|
col = int((mouse_loc[0] - bias_left) / SIZE) #* get position of mouse click
|
|
|
|
|
if square_selected == (row,col) or col >=DIMENSION_COLUMNS or row >= DIMENSION_ROWS:
|
|
|
|
|
square_selected = ()
|
|
|
|
|
click_queue = []
|
|
|
|
|
else:
|
|
|
|
|
square_selected = (row,col)
|
|
|
|
|
click_queue.append(square_selected)
|
|
|
|
|
if(len(click_queue) == 2):
|
|
|
|
|
cur_piece_loc = click_queue[0]
|
|
|
|
|
nxt_piece_loc = click_queue[1]
|
|
|
|
|
move = backend.Move(cur_piece_loc,nxt_piece_loc,game_state.board)
|
|
|
|
|
if move in valid_moves:
|
|
|
|
|
game_state.makeMove(move)
|
|
|
|
|
mademove = True
|
|
|
|
|
if mademove:
|
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
|
ShowGameState(screen,game_state,valid_moves,square_selected)
|
|
|
|
|
if game_state.conquer():
|
|
|
|
|
showGameOverText(screen,"player "+game_state.win_person+" wins")
|
|
|
|
|
game_over = True
|
|
|
|
|
clock.tick(60)
|
|
|
|
|
pg.display.flip()
|
|
|
|
|
elif mode == 2:
|
|
|
|
|
while running:
|
|
|
|
|
|
|
|
|
|
for e in pg.event.get():
|
|
|
|
|
#接下来处理游戏中的事件
|
|
|
|
|
if e.type == pg.QUIT:
|
|
|
|
|
pg.quit()
|
|
|
|
|
sys.exit()
|
|
|
|
|
elif e.type == pg.MOUSEBUTTONDOWN:
|
|
|
|
|
#鼠标点击事件:用于选择棋子,移动棋子
|
|
|
|
|
if not game_over:
|
|
|
|
|
mouse_loc = pg.mouse.get_pos()
|
|
|
|
|
row = int((mouse_loc[1] - bias_top) / SIZE)
|
|
|
|
|
col = int((mouse_loc[0] - bias_left) / SIZE) #* get position of mouse click
|
|
|
|
|
if square_selected == (row,col) or col >=DIMENSION_COLUMNS or row >= DIMENSION_ROWS:
|
|
|
|
|
square_selected = ()
|
|
|
|
|
click_queue = []
|
|
|
|
|
else:
|
|
|
|
|
click_queue = [square_selected]
|
|
|
|
|
square_selected = (row,col)
|
|
|
|
|
click_queue.append(square_selected)
|
|
|
|
|
if(len(click_queue) == 2):
|
|
|
|
|
cur_piece_loc = click_queue[0]
|
|
|
|
|
nxt_piece_loc = click_queue[1]
|
|
|
|
|
move = backend.Move(cur_piece_loc,nxt_piece_loc,game_state.board)
|
|
|
|
|
if move in valid_moves:
|
|
|
|
|
game_state.makeMove(move)
|
|
|
|
|
mademove = True
|
|
|
|
|
square_selected = ()
|
|
|
|
|
click_queue = []
|
|
|
|
|
else:
|
|
|
|
|
click_queue = [square_selected]
|
|
|
|
|
|
|
|
|
|
elif e.type == pg.KEYDOWN:
|
|
|
|
|
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
elif e.type == pg.KEYDOWN:
|
|
|
|
|
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if mademove:
|
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
|
ShowGameState(screen,game_state,valid_moves,square_selected)
|
|
|
|
|
if game_state.conquer():
|
|
|
|
|
showGameOverText(screen,"player "+game_state.win_person+" wins")
|
|
|
|
|
game_over = True
|
|
|
|
|
clock.tick(60)
|
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
|
|
if mademove:
|
|
|
|
|
valid_moves = game_state.getAllMoves()
|
|
|
|
|
ShowGameState(screen,game_state,valid_moves,square_selected)
|
|
|
|
|
if game_state.conquer():
|
|
|
|
|
showGameOverText(screen,"player "+game_state.win_person+" wins")
|
|
|
|
|
game_over = True
|
|
|
|
|
clock.tick(60)
|
|
|
|
|
pg.display.flip()
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
print("Loading...")
|
|
|
|
|
print("Initialing game...")
|
|
|
|
|
mode = MainMenu()
|
|
|
|
|
main()
|
|
|
|
|
player1='player1'
|
|
|
|
|
network = None #前后端通信接口
|
|
|
|
|
player2='player2'
|
|
|
|
|
main(mode,player1,player2,network)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|