diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..11a5d8e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +main.py \ No newline at end of file diff --git a/server/config.txt b/server/config.txt new file mode 100644 index 0000000..1ead9da --- /dev/null +++ b/server/config.txt @@ -0,0 +1,5 @@ +SOCKET_HOST=127.0.0.1 +SOCKET_PORT=50005 +MAX_WAITING_TIME=600 +MAX_THNIKING_TIME=15 +MAX_TOTAL_TIME=1000 diff --git a/server/server.py b/server/server.py new file mode 100644 index 0000000..9770956 --- /dev/null +++ b/server/server.py @@ -0,0 +1,247 @@ +# coding:utf-8 +import socket +import sys +import json +import uuid +import time +from threading import Thread, Lock + +if(sys.version[:1] == "3"): + import queue as Queue + from _thread import * +else: + import Queue + from thread import * + +class Config: + SOCKET_HOST = '127.0.0.1' # Symbolic name meaning all available interfaces + SOCKET_PORT = 50005 # Arbitrary non-privileged port + MAX_WAITING_TIME = 180 + MAX_THNIKING_TIME = 60 + MAX_TOTAL_TIME = 600 + +class Client: + def __init__(self, conn, addr, name, side, time, total): + self.conn = conn + self.addr = addr + self.name = name + self.side = side + self.time = time + self.total = total + +mutex = Lock() +mutex_playing = Lock() + +playing_ones = {} +waiting_players = Queue.Queue() +#init queues +#for games in range(1, 10): +# waiting_players[games] = Queue.Queue() + +def load_config(): + with open('config.txt', 'r') as f: + for line in f.readlines(): + line = line.strip() + if line.find('SOCKET_HOST=') >= 0: + Config.SOCKET_HOST = line[line.index('=') + 1 : ] + elif line.find('SOCKET_PORT=') >= 0: + Config.SOCKET_PORT = int(line[line.index('=') + 1 : ]) + elif line.find('MAX_WAITING_TIME=') >= 0: + Config.MAX_WAITING_TIME = int(line[line.index('=') + 1 : ]) + elif line.find('MAX_THNIKING_TIME=') >= 0: + Config.MAX_THNIKING_TIME = int(line[line.index('=') + 1 : ]) + elif line.find('MAX_TOTAL_TIME=') >= 0: + Config.MAX_TOTAL_TIME = int(line[line.index('=') + 1 : ]) + else: + pass + +def get_counterpart_from(waiting_ones): + counterpart = None + mutex.acquire() + if not waiting_ones.empty(): + counterpart = waiting_ones.get() + mutex.release() + return counterpart + +def to_wait_in_queue(client, the_queue): + mutex.acquire() + the_queue.put(client) + mutex.release() + +def remove_one_from_queue(the_queue): + mutex.acquire() + if (the_queue.qsize() == 1): + the_queue.get() + mutex.release() + +def send_msg_to(client, msg): + packet = json.dumps(msg) + if (sys.version[:1] == "3"): + packet = packet.encode('utf-8') + #print(client.addr[0] + ":" + str(client.addr[1]) + "\t" + str(msg)) + client.conn.send(packet) + +def __start_match_between(client0, client1): + match_uuid = str(uuid.uuid4()) + mutex_playing.acquire() + playing_ones[match_uuid] = (client0, client1) + mutex_playing.release() + + client0.side = 0 + client0.time = Config.MAX_THNIKING_TIME + client0.total = Config.MAX_TOTAL_TIME + client1.side = 1 + client1.time = -1 + client1.total = Config.MAX_TOTAL_TIME + + msg0 = { + "status": 1, + "counterpart_name": client1.name, + "game_id": match_uuid, + "side": client0.side, + "think_time": Config.MAX_THNIKING_TIME, + "total_time": Config.MAX_TOTAL_TIME, + } + send_msg_to(client0, msg0) + msg1 = { + "status": 1, + "counterpart_name": client0.name, + "game_id": match_uuid, + "side": client1.side, + "think_time": Config.MAX_THNIKING_TIME, + "total_time": Config.MAX_TOTAL_TIME, + } + send_msg_to(client1, msg1) + +def join_game_handler(msg, addr, conn): + new_client = Client(conn, addr, msg['name'], -1, -1, -1) + #game_type = msg["id"] + #the_queue = waiting_players[game_type] + counterpart = get_counterpart_from(waiting_players) + if not counterpart: #wait + to_wait_in_queue(new_client, waiting_players) + return + else: + #counterpart=get_counterpart_from(waiting_players[game_type]) + __start_match_between(new_client, counterpart) + +def quit_game_handler(msg): + match_uuid = msg['game_id'] + if match_uuid is None: + remove_one_from_queue(waiting_players) + return + pairs = None + mutex_playing.acquire() + if (match_uuid in playing_ones): + pairs = playing_ones[match_uuid] + del playing_ones[match_uuid] + mutex_playing.release() + + if pairs is not None: + if(pairs[0].side == msg['side']): + to_notify = pairs[1] + else: + to_notify = pairs[0] + msg = { + "status": 2, #exit + "game_id": match_uuid, + "side": msg['side'], + "request": msg['request'], + } + send_msg_to(to_notify, msg) + +def timer_thread(): + while True: + time.sleep(1) + mutex_playing.acquire() + for game_id in list(playing_ones.keys()): + (client0, client1) = playing_ones[game_id] + #print("%d - %d" % (client0.time, client1.time)) + if (client0.time < 0): + client = client1 + else: + client = client0 + + if (client.time == 0 or client.total == 0): + msg = { + "status": 3, #timeout exit + "game_id": game_id, + "side": client.side, + } + send_msg_to(client0, msg) + send_msg_to(client1, msg) + del playing_ones[game_id] + else: + client.time -= 1 + client.total -= 1 + mutex_playing.release() + +def transfer_message(msg): + match_uuid = msg['game_id'] + pairs = playing_ones[match_uuid] + if(pairs[0].side == msg['side']): + to_notify = pairs[1] + pairs[0].time = -1 + else: + to_notify = pairs[0] + pairs[1].time = -1 + to_notify.time = Config.MAX_THNIKING_TIME + send_msg_to(to_notify, msg) + +def client_thread(conn, addr): + while True: + data = conn.recv(1024) + if not data: + break + print(data) + data = json.loads(data) + + if not 'type' in data: + transfer_message(data['msg']) + continue + if data['type'] == 0: + join_game_handler(data['msg'], addr, conn) + continue + elif data['type'] == 1: + transfer_message(data['msg']) + continue + elif data['type'] == 2: + quit_game_handler(data['msg']) + break + elif data['type'] == 3: + break + else: + #delivering message between the two clients + transfer_message(data['msg']) + continue + #came out of loop + conn.close() + +def main(): + load_config() + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + print('Socket server created') + #try: + server.bind((Config.SOCKET_HOST, Config.SOCKET_PORT)) + #except (socket.error, msg): + # print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) + # sys.exit() + + print('Socket bind complete') + server.listen(10) + print('Socket now listening') + + #now keep talking with the client + start_new_thread(timer_thread, ()) + while True: + #wait to accept a connection - blocking call + conn, addr = server.accept() + print ('Connected with ' + addr[0] + ':' + str(addr[1])) + + #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function. + start_new_thread(client_thread, (conn, addr)) + server.close() + +if __name__ == '__main__': + main() + \ No newline at end of file diff --git a/server/游戏服务器及通信协议.pdf b/server/游戏服务器及通信协议.pdf new file mode 100644 index 0000000..96d04d0 Binary files /dev/null and b/server/游戏服务器及通信协议.pdf differ diff --git a/src/__pycache__/Client_connection.cpython-38.pyc b/src/__pycache__/Client_connection.cpython-38.pyc new file mode 100644 index 0000000..5d78e12 Binary files /dev/null and b/src/__pycache__/Client_connection.cpython-38.pyc differ diff --git a/src/__pycache__/socket_client.cpython-38.pyc b/src/__pycache__/socket_client.cpython-38.pyc new file mode 100644 index 0000000..20f927f Binary files /dev/null and b/src/__pycache__/socket_client.cpython-38.pyc differ diff --git a/src/__pycache__/start_game.cpython-38.pyc b/src/__pycache__/start_game.cpython-38.pyc index 0cfb684..d4f996e 100644 Binary files a/src/__pycache__/start_game.cpython-38.pyc and b/src/__pycache__/start_game.cpython-38.pyc differ diff --git a/src/main.py b/src/main.py index fb3846e..9f71163 100644 --- a/src/main.py +++ b/src/main.py @@ -1604,47 +1604,72 @@ Axis = s.Chessmen_axis(axis) def server_thread(conn,addr) : global player,Axis while True: - msg = conn.recv(1024) - if not msg: - break - print(msg) - msg = json.loads(msg) - #TODO : side的确定 - if not 'status' in msg: - status_2,game_id,side,old_x,old_y,x,y, num = C.handle_move(msg) - id = num + 1 - player.game_id = game_id - player.status_2 = status_2 - Axis.axis[id]['old_x'] = old_x - Axis.axis[id]['old_y'] = old_y - Axis.axis[id]['x'] = x - Axis.axis[id]['y'] = y - player.id = id - continue - if msg["status"] == 1: - status_2,counterpart_name,game_id,side,think_time,total_time = C.handle_new_player(msg) - player.game_id = game_id - player.status_2 = status_2 - player.side = side - player.counterpart_name = counterpart_name - player.think_time = think_time - player.total_time = total_time - continue - elif msg["status"] == 2: - status_2,request,game_id,side = C.handle_exit_game(msg) - player.status_2 = status_2 - player.request = request - player.game_id = game_id - player.side = side - break - elif msg["status"] == 3: - status_2, game_id, side = C.handle_time_over(msg) - player.status_2 = status_2 - player.game_id = game_id - player.side = side + try: + msg = conn.recv(1024) + if not msg: + break + print(msg) + msg = json.loads(msg) + #TODO : side的确定 + if not 'status' in msg: + status_2,game_id,side,old_x,old_y,x,y, num = C.handle_move(msg) + id = num + 1 + player.game_id = game_id + player.status_2 = status_2 + Axis.axis[id]['old_x'] = old_x + Axis.axis[id]['old_y'] = old_y + Axis.axis[id]['x'] = x + Axis.axis[id]['y'] = y + player.id = id + player.invalid = 1 + continue + if msg["status"] == 1: + status_2,counterpart_name,game_id,side,think_time,total_time = C.handle_new_player(msg) + player.game_id = game_id + player.status_2 = status_2 + player.side = side + player.counterpart_name = counterpart_name + player.think_time = think_time + player.total_time = total_time + player.invalid = 1 + continue + elif msg["status"] == 2: + status_2,request,game_id,side = C.handle_exit_game(msg) + player.status_2 = status_2 + player.request = request + player.game_id = game_id + player.side = side + player.invalid = 1 + break + elif msg["status"] == 3: + status_2, game_id, side = C.handle_time_over(msg) + player.status_2 = status_2 + player.game_id = game_id + player.side = side + player.invalid = 1 + break + except: break conn.close() + +mutex_playing = Lock() +def time_thread(): + mutex_playing.acquire() + is_over_time = 0 + while True: + time.sleep(1) + if player.think_time <= 0: + is_over_time = 1 + break + else: + player.think_time -= 1 + player.total_time -= 1 + if is_over_time: + return False + else : + return True + class Config: SOCKET_HOST = '127.0.0.1'#Symbolic name meaning all available interfaces SOCKET_PORT = 50005#Arbitrary non-privileged port @@ -1659,13 +1684,21 @@ def main(): MAP = init.calc_map() MAP1 = init.clac_MAP1() game_status = start_game.starting_screen() + demo = 0 # 标识是否为网络版 + stop = 0 # 标识是否请求暂停游戏 + exit = 0 # 标识是否请求退出游戏 + report = 0 # 标识是否举报对方 + other_player_move = 0 # 标识对方是否能移动 if not game_status: return False + elif game_status == 1: + demo = 0 + elif game_status == 2: + demo = 1 run = True clock = pygame.time.Clock() font = pygame.font.SysFont('kaiti',40) status = id = old_x = old_y = x = y = turn = 0 - demo = 1 #标识是否为网络版 if demo == 1: name = input("Please tell us your name:") player.name = name @@ -1677,6 +1710,10 @@ def main(): C.conn_to_server(client, name) while run: clock.tick(FPS) + if left_win(x, y, MAP, MAP1): + run = False + if right_win(x, y, MAP, MAP1): + run = False if demo == 1: C.start_new_thread(server_thread, (client,Address)) for event in pygame.event.get(): @@ -1726,7 +1763,7 @@ def main(): status = 2 #TODO: Cirlce the elective animal else: - if (id == 1 and turn == 0 and status == 1 and demo == 0) or (id == 1 and demo == 1 and status == 1 and turn == player.side): + if (id == 1 and turn == 0 and status == 1 and demo == 0) or (id == 1 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1740,7 +1777,7 @@ def main(): turn = 1 else: status = 0 - elif (id == 2 and turn == 0 and status == 1 and demo == 0) or (id == 2 and demo == 1 and status == 1 and turn == player.side): + elif (id == 2 and turn == 0 and status == 1 and demo == 0) or (id == 2 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1754,7 +1791,7 @@ def main(): turn = 1 else : status = 0 - elif (id == 3 and turn == 0 and status == 1 and demo == 0) or (id == 3 and demo == 1 and status == 1 and turn == player.side): + elif (id == 3 and turn == 0 and status == 1 and demo == 0) or (id == 3 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1768,7 +1805,7 @@ def main(): turn = 1 else : status = 0 - elif (id == 4 and turn == 0 and status == 1 and demo == 0) or (id == 4 and demo == 1 and status == 1 and turn == player.side): + elif (id == 4 and turn == 0 and status == 1 and demo == 0) or (id == 4 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1782,7 +1819,7 @@ def main(): turn = 1 else : status = 0 - elif (id == 5 and turn == 0 and status == 1 and demo == 0) or (id == 5 and demo == 1 and status == 1 and turn == player.side): + elif (id == 5 and turn == 0 and status == 1 and demo == 0) or (id == 5 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1796,7 +1833,7 @@ def main(): turn = 1 else : status = 0 - elif (id == 6 and turn == 0 and status == 1 and demo == 0) or (id == 6 and demo == 1 and status == 1 and turn == player.side): + elif (id == 6 and turn == 0 and status == 1 and demo == 0) or (id == 6 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1810,7 +1847,7 @@ def main(): turn = 1 else : status = 0 - elif (id == 7 and turn == 0 and status == 1 and demo == 0) or (id == 7 and demo == 1 and status == 1 and turn == player.side): + elif (id == 7 and turn == 0 and status == 1 and demo == 0) or (id == 7 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: @@ -1824,7 +1861,7 @@ def main(): turn = 1 else : status = 0 - elif (id == 8 and turn == 1 and status == 2 and demo == 0) or (id == 8 and turn == player.side and status == 2 and demo == 1): + elif (id == 8 and turn == 1 and status == 2 and demo == 0) or (id == 8 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1838,7 +1875,7 @@ def main(): turn = 0 else : status = 0 - elif (id == 9 and turn == 1 and status == 2 and demo == 0) or (id == 9 and turn == player.side and status == 2 and demo == 1): + elif (id == 9 and turn == 1 and status == 2 and demo == 0) or (id == 9 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1852,7 +1889,7 @@ def main(): turn = 0 else : status = 0 - elif (id == 10 and turn == 1 and status == 2 and demo == 0) or (id == 10 and turn == player.side and status == 2 and demo == 1): + elif (id == 10 and turn == 1 and status == 2 and demo == 0) or (id == 10 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1866,7 +1903,7 @@ def main(): turn = 0 else : status = 0 - elif (id == 11 and turn == 1 and status == 2 and demo == 0) or (id == 11 and turn == player.side and status == 2 and demo == 1): + elif (id == 11 and turn == 1 and status == 2 and demo == 0) or (id == 11 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1880,7 +1917,7 @@ def main(): turn = 0 else : status = 0 - elif (id == 12 and turn == 1 and status == 2 and demo == 0) or (id == 12 and turn == player.side and status == 2 and demo == 1): + elif (id == 12 and turn == 1 and status == 2 and demo == 0) or (id == 12 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1894,7 +1931,7 @@ def main(): turn = 0 else : status = 0 - elif (id == 13 and turn == 1 and status == 2 and demo == 0) or (id == 13 and turn == player.side and status == 2 and demo == 1): + elif (id == 13 and turn == 1 and status == 2 and demo == 0) or (id == 13 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1908,7 +1945,7 @@ def main(): turn = 0 else : status = 0 - elif (id == 14 and turn == 1 and status == 2 and demo == 0) or (id == 14 and turn == player.side and status == 2 and demo == 1): + elif (id == 14 and turn == 1 and status == 2 and demo == 0) or (id == 14 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: @@ -1922,94 +1959,136 @@ def main(): turn = 0 else : status = 0 - elif demo == 1 and player.id and turn != player.side: + elif demo == 1 and player.id and turn != player.side and player.invalid == 1 and player.status_2 == -1: counterpart_id = player.id counterpart_old_x , counterpart_old_y= Axis.axis[counterpart_id]['old_x'],Axis.axis[counterpart_id]['old_y'] counterpart_x ,counterpart_y = Axis.axis[counterpart_id]['x'],Axis.axis[counterpart_id]['y'] - if counterpart_id == 1: + if counterpart_id == 1 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_mouse.x , l_mouse.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 2: + player.invalid = 0 + elif counterpart_id == 2 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_eagle.x , l_eagle.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 3: + player.invalid = 0 + elif counterpart_id == 3 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_fox.x , l_fox.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 4: + player.invalid = 0 + elif counterpart_id == 4 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_wolf.x , l_wolf.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 5: + player.invalid = 0 + elif counterpart_id == 5 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_leopard.x , l_leopard.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 6: + player.invalid = 0 + elif counterpart_id == 6 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_lion.x , l_lion.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 7: + player.invalid = 0 + elif counterpart_id == 7 and turn == 0: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id l_elephant.x , l_elephant.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 8: + player.invalid = 0 + elif counterpart_id == 8 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_mouse.x , r_mouse.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 9: + player.invalid = 0 + elif counterpart_id == 9 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_eagle.x , r_eagle.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 10: + player.invalid = 0 + elif counterpart_id == 10 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_fox.x , r_fox.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 11: + player.invalid = 0 + elif counterpart_id == 11 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_wolf.x , r_wolf.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 12: + player.invalid = 0 + elif counterpart_id == 12 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_leopard.x , r_leopard.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 13: + player.invalid = 0 + elif counterpart_id == 13 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_lion.x , r_lion.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) - elif counterpart_id == 14: + player.invalid = 0 + elif counterpart_id == 14 and turn == 1: if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] MAP[counterpart_x][counterpart_y] = counterpart_id r_elephant.x , r_elephant.y = init.tran(counterpart_x,counterpart_y) turn = (1-turn) + player.invalid = 0 draw_window(l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, r_leopard, r_mouse, r_fox, id, x, y, status, MAP, MAP1, turn) @@ -2023,7 +2102,6 @@ def main(): if right_win(x, y, MAP, MAP1): print_text(WIN, font, 500, 400, "Blue Win!", BLUE) pygame.display.update() - time.sleep(2) #os.system("pause") return start_game.starting_screen() @@ -2038,4 +2116,7 @@ if __name__ == "__main__": again = main() l_animals = r_animals = 7 ID = [1 for i in range(20)] + axis = [(0, 0), (2, 6), (1, 1), (1, 5), (2, 2), (2, 4), (2, 3), (2, 0), (6, 0), (7, 5), (7, 1), (6, 4), (6, 2), + (6, 3), (6, 6)] + Axis = s.Chessmen_axis(axis) pygame.quit() diff --git a/src/main_an.py b/src/main_an.py index 4073155..018f8cb 100644 --- a/src/main_an.py +++ b/src/main_an.py @@ -2,7 +2,23 @@ import pygame import init import start_game import time +import Client_connection as C import os +import socket +import sys +import json +import uuid +import time +from threading import Thread, Lock +import socket_client as s +import pygame_menu + +if(sys.version[:1] == "3"): + import queue as Queue + from _thread import * +else: + import Queue + from _thread import * WIDTH , HEIGHT = 1200 , 800 @@ -22,7 +38,6 @@ Elephant = pygame.mixer.Sound('audio/Elephant.mp3') Leopard = pygame.mixer.Sound('audio/Leopard.mp3') Lion = pygame.mixer.Sound('audio/Lion.mp3') Mouse = pygame.mixer.Sound('audio/Mouse.mp3') -#Start = pygame.mixer.Sound('audio/start.mp3') Tiger = pygame.mixer.Sound('audio/Tiger.mp3') Wolf = pygame.mixer.Sound('audio/Wolf.mp3') Fox = pygame.mixer.Sound('audio/Fox.mp3') @@ -129,7 +144,7 @@ def draw_window(l_elephant,l_eagle,l_wolf,l_lion,l_leopard,l_mouse,l_fox,r_eleph l_elephant = pygame.Rect(281,10,CHESSSIZE_Y,CHESSSIZE_Y) l_eagle = pygame.Rect(157,120,CHESSSIZE_Y,CHESSSIZE_Y) l_wolf = pygame.Rect(284,229,CHESSSIZE_Y,CHESSSIZE_Y) -l_lion = pygame.Rect(292,343,CHESSSIZE_Y,CHESSSIZE_Y) +l_lion = pygame.Rect(282,343,CHESSSIZE_Y,CHESSSIZE_Y) l_leopard = pygame.Rect(285,450,CHESSSIZE_Y,CHESSSIZE_Y) l_mouse = pygame.Rect(285,665,CHESSSIZE_Y,CHESSSIZE_Y) l_fox = pygame.Rect(157,557,CHESSSIZE_Y,CHESSSIZE_Y) @@ -1277,7 +1292,6 @@ def right_win(x,y,MAP,MAP1): return 0 if isWin == 1: return 1 - #print(isWin) return 0 def right_move(old_x,old_y,x,y,id,MAP,MAP1): @@ -1584,51 +1598,153 @@ def right_Wall(id,MAP,MAP1): else: return 0 -def goBack(l_axis,r_axis,MAP,MAP1,turn,animalsName): - if turn: - id,old_x,old_y = l_axis.pop() - x, y = init.calc(animalsName[id].x,animalsName[id].y) - MAP[x][y] = MAP1[x][y] - MAP[old_x][old_y] = id - animalsName[id].x , animalsName[id].y = init.tran(old_x,old_y) - turn = 0 +player = s.Player() +axis = [(0, 0), (2, 6), (1, 1), (1, 5), (2, 2), (2, 4), (2, 3), (2, 0), (6, 0), (7, 5), (7, 1), (6, 4), (6, 2), + (6, 3), (6, 6)] +Axis = s.Chessmen_axis(axis) +def server_thread(conn,addr) : + global player,Axis + while True: + try: + msg = conn.recv(1024) + if not msg: + break + print(msg) + msg = json.loads(msg) + #TODO : side的确定 + if not 'status' in msg: + status_2,game_id,side,old_x,old_y,x,y, num = C.handle_move(msg) + id = num + 1 + player.game_id = game_id + player.status_2 = status_2 + Axis.axis[id]['old_x'] = old_x + Axis.axis[id]['old_y'] = old_y + Axis.axis[id]['x'] = x + Axis.axis[id]['y'] = y + player.id = id + player.invalid = 1 + continue + if msg["status"] == 1: + status_2,counterpart_name,game_id,side,think_time,total_time = C.handle_new_player(msg) + player.game_id = game_id + player.status_2 = status_2 + player.side = side + player.counterpart_name = counterpart_name + player.think_time = think_time + player.total_time = total_time + player.invalid = 1 + continue + elif msg["status"] == 2: + status_2,request,game_id,side = C.handle_exit_game(msg) + player.status_2 = status_2 + player.request = request + player.game_id = game_id + player.side = side + player.invalid = 1 + break + elif msg["status"] == 3: + status_2, game_id, side = C.handle_time_over(msg) + player.status_2 = status_2 + player.game_id = game_id + player.side = side + player.invalid = 1 + break + except: + break + conn.close() + + +mutex_playing = Lock() +def time_thread(): + mutex_playing.acquire() + is_over_time = 0 + while True: + time.sleep(1) + if player.think_time <= 0: + is_over_time = 1 + break + else: + player.think_time -= 1 + player.total_time -= 1 + if is_over_time: + return False else : - id, old_x, old_y = r_axis.pop() - x, y = init.calc(animalsName[id].x, animalsName[id].y) - MAP[x][y] = MAP1[x][y] - MAP[old_x][old_y] = id - animalsName[id].x, animalsName[id].y = init.tran(old_x, old_y) - turn = 1 - return l_axis,r_axis,MAP,MAP1,turn + return True + +class Config: + SOCKET_HOST = '127.0.0.1'#Symbolic name meaning all available interfaces + SOCKET_PORT = 50005#Arbitrary non-privileged port + MAX_WAITING_TIME = 180 + MAX_THNIKING_TIME = 60 + MAX_TOTAL_TIME = 600 + +def pause(): + pass -animalsName = init.getAnimalsname() -print(animalsName) + +def report(): + pass +def quit(): + return 0 +def surrender(): + pass +def start_menu(): + pygame.init() + surface = pygame.display.set_mode((600, 400)) + menu = pygame_menu.Menu('DO YOUR ACTIONS', 400, 300, theme = pygame_menu.themes.THEME_BLUE) + menu.add.button('Pause', pause) + menu.add.button('Surrender', surrender) + menu.add.button('Report', report) + menu.add.button('Quit', pygame_menu.events.EXIT) + draw_window(l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, + r_leopard, r_mouse, r_fox, 0, 0, 0, 0, 0, 0, 0) + + menu.mainloop(surface) def main(): global l_animals,r_animals + global player, Axis BGM.play(-1) MAP = init.calc_map() MAP1 = init.clac_MAP1() game_status = start_game.starting_screen() + demo = 0 # 标识是否为网络版 + stop = 0 # 标识是否请求暂停游戏 + exit = 0 # 标识是否请求退出游戏 + report = 0 # 标识是否举报对方 + other_player_move = 0 # 标识对方是否能移动 if not game_status: return False + elif game_status == 1: + demo = 0 + elif game_status == 2: + demo = 1 run = True - #print(animalsName[1].x) - l_axis=r_axis=[] clock = pygame.time.Clock() font = pygame.font.SysFont('kaiti',40) status = id = old_x = old_y = x = y = turn = 0 + if demo == 1: + name = input("Please tell us your name:") + player.name = name + Address = (Config.SOCKET_HOST, Config.SOCKET_PORT) + if demo == 1: + C.load_config() + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client.connect((Config.SOCKET_HOST, Config.SOCKET_PORT)) + C.conn_to_server(client, name) while run: - #print("left win:",left_win(x, y, MAP, MAP1)) - #print("right win:",right_win(x, y, MAP, MAP1)) + clock.tick(FPS) + if left_win(x, y, MAP, MAP1): run = False - break if right_win(x, y, MAP, MAP1): run = False - break - clock.tick(FPS) + if demo == 1: + C.start_new_thread(server_thread, (client,Address)) for event in pygame.event.get(): - if event.type == pygame.QUIT: + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + start_game.starting_menu() + elif event.type == pygame.QUIT: run = False elif event.type == pygame.MOUSEBUTTONDOWN: b1, b2, b3 = pygame.mouse.get_pressed() @@ -1636,11 +1752,11 @@ def main(): mouse_x , mouse_y = event.pos old_x = x old_y = y - x = int(mouse_x / WIDTH * 9) - y = int(mouse_y / HEIGHT * 7) - if status == 0: + x = int((mouse_x+10) / WIDTH * 9) + y = int((mouse_y+10) / HEIGHT * 7) + if status == 0 : id = MAP[x][y] - if 0 < id < 8 and turn == 0: + if (0 < id < 8 and turn == 0 and demo == 0) or (0 < id < 8 and demo == 1 and turn == player.side): if id == 1: Mouse.play() elif id == 2: @@ -1656,7 +1772,7 @@ def main(): elif id == 7: Elephant.play() status = 1 - elif 7 < id <15 and turn == 1: + elif (7 < id <15 and turn == 1 and demo == 0) or (7 < id <15 and demo == 1 and turn == player.side): if id == 8: Mouse.play() if id == 9: @@ -1674,216 +1790,344 @@ def main(): status = 2 #TODO: Cirlce the elective animal else: - if id == 1 and turn == 0 and status == 1: + if (id == 1 and turn == 0 and status == 1 and demo == 0) or (id == 1 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if(ismove(old_x,old_y,x,y,id,MAP,MAP1)): - #print(MAP1[old_x][old_y]) l_mouse.x,l_mouse.y = init.tran(x,y) - animalsName[1]=l_mouse MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if(demo == 1): + C.move(client,player.game_id,player.side,id,old_x,old_y,x,y) turn = 1 - l_axis.append((id,old_x,old_y)) else: status = 0 - elif id == 2 and turn == 0 and status == 1: + elif (id == 2 and turn == 0 and status == 1 and demo == 0) or (id == 2 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : l_eagle.x , l_eagle.y = init.tran(x,y) - animalsName[2] = l_leopard MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 1 - l_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 3 and turn == 0 and status == 1: + elif (id == 3 and turn == 0 and status == 1 and demo == 0) or (id == 3 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : l_fox.x , l_fox.y = init.tran(x,y) - animalsName[3] = l_fox MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 1 - l_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 4 and turn == 0 and status == 1: + elif (id == 4 and turn == 0 and status == 1 and demo == 0) or (id == 4 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : l_wolf.x , l_wolf.y = init.tran(x,y) - animalsName[4] = l_wolf MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 1 - l_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 5 and turn == 0 and status == 1: + elif (id == 5 and turn == 0 and status == 1 and demo == 0) or (id == 5 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : l_leopard.x , l_leopard.y = init.tran(x,y) - animalsName[5] = l_leopard MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 1 - l_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 6 and turn == 0 and status == 1: + elif (id == 6 and turn == 0 and status == 1 and demo == 0) or (id == 6 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : l_lion.x , l_lion.y = init.tran(x,y) - animalsName[6] = l_lion MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 1 - l_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 7 and turn == 0 and status == 1: + elif (id == 7 and turn == 0 and status == 1 and demo == 0) or (id == 7 and demo == 1 and status == 1 and turn == player.side and turn == 0): if MAP[x][y] == id: status = 1 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : l_elephant.x , l_elephant.y = init.tran(x,y) - animalsName[7] = l_elephant MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 1 - l_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 8 and turn == 1 and status == 2: + elif (id == 8 and turn == 1 and status == 2 and demo == 0) or (id == 8 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_mouse.x , r_mouse.y = init.tran(x,y) - animalsName[8] = r_mouse MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 9 and turn == 1 and status == 2: + elif (id == 9 and turn == 1 and status == 2 and demo == 0) or (id == 9 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_eagle.x , r_eagle.y = init.tran(x,y) - animalsName[9] = r_eagle MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 10 and turn == 1 and status == 2: + elif (id == 10 and turn == 1 and status == 2 and demo == 0) or (id == 10 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_fox.x , r_fox.y = init.tran(x,y) - animalsName[10] = r_fox MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 11 and turn == 1 and status == 2: + elif (id == 11 and turn == 1 and status == 2 and demo == 0) or (id == 11 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_wolf.x , r_wolf.y = init.tran(x,y) - animalsName[11] = r_wolf MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 12 and turn == 1 and status == 2: + elif (id == 12 and turn == 1 and status == 2 and demo == 0) or (id == 12 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_leopard.x , r_leopard.y = init.tran(x,y) - animalsName[12] = r_leopard MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 13 and turn == 1 and status == 2: + elif (id == 13 and turn == 1 and status == 2 and demo == 0) or (id == 13 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_lion.x , r_lion.y = init.tran(x,y) - animalsName[13] = r_lion MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 - elif id == 14 and turn == 1 and status == 2: + elif (id == 14 and turn == 1 and status == 2 and demo == 0) or (id == 14 and turn == player.side and status == 2 and demo == 1 and turn == 1): if MAP[x][y] == id: status = 2 else: if (ismove(old_x,old_y,x,y,id,MAP,MAP1)) : r_elephant.x , r_elephant.y = init.tran(x,y) - animalsName[14] = r_elephant MAP[old_x][old_y] = MAP1[old_x][old_y] MAP[x][y] = id status = 0 + if (demo == 1): + C.move(client, player.game_id, player.side, id, old_x, old_y, x, y) turn = 0 - r_axis.append((id, old_x, old_y)) else : status = 0 + elif demo == 1 and player.id and turn != player.side and player.invalid == 1 and player.status_2 == -1: + counterpart_id = player.id + counterpart_old_x , counterpart_old_y= Axis.axis[counterpart_id]['old_x'],Axis.axis[counterpart_id]['old_y'] + counterpart_x ,counterpart_y = Axis.axis[counterpart_id]['x'],Axis.axis[counterpart_id]['y'] + if counterpart_id == 1 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_mouse.x , l_mouse.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 2 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_eagle.x , l_eagle.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 3 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_fox.x , l_fox.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 4 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_wolf.x , l_wolf.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 5 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_leopard.x , l_leopard.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 6 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_lion.x , l_lion.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 7 and turn == 0: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + l_elephant.x , l_elephant.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 8 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_mouse.x , r_mouse.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 9 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_eagle.x , r_eagle.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 10 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_fox.x , r_fox.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 11 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_wolf.x , r_wolf.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 12 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_leopard.x , r_leopard.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 13 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_lion.x , r_lion.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + elif counterpart_id == 14 and turn == 1: + if MAP[counterpart_x][counterpart_y] != counterpart_id: + if 1 <= MAP[counterpart_x][counterpart_y] <= 14: + ID[MAP[counterpart_x][counterpart_y]] = 0 + MAP[counterpart_old_x][counterpart_old_y] = MAP1[counterpart_old_x][counterpart_old_y] + MAP[counterpart_x][counterpart_y] = counterpart_id + r_elephant.x , r_elephant.y = init.tran(counterpart_x,counterpart_y) + turn = (1-turn) + player.invalid = 0 + + draw_window(l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, + r_leopard, r_mouse, r_fox, id, x, y, status, MAP, MAP1, turn) if left_win(x, y, MAP, MAP1): run = False if right_win(x, y, MAP, MAP1): run = False - l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, r_leopard, r_fox, r_mouse = init.update(animalsName) - draw_window(l_elephant,l_eagle,l_wolf,l_lion,l_leopard,l_mouse,l_fox,r_elephant,r_eagle,r_wolf,r_lion,r_leopard,r_mouse,r_fox,id, x, y, status, MAP, MAP1,turn) - #print("left win:", left_win(x, y, MAP, MAP1)) - #print("right win:", right_win(x, y, MAP, MAP1)) if left_win(x, y, MAP, MAP1): - print_text(WIN, font, 500, 400, "Left Win!", RED) + print_text(WIN, font, 500, 400, "Red Win!", RED) pygame.display.update() if right_win(x, y, MAP, MAP1): - print_text(WIN, font, 500, 400, "Right Win!", BLUE) + print_text(WIN, font, 500, 400, "Blue Win!", BLUE) pygame.display.update() time.sleep(2) #os.system("pause") @@ -1894,13 +2138,12 @@ if __name__ == "__main__": again = 1 while again == 1: l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, r_leopard, r_fox, r_mouse=init.refresh() - #print(r_eagle) draw_window(l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, r_leopard, r_mouse, r_fox,0,0,0,0,0,0,0) again = main() l_animals = r_animals = 7 ID = [1 for i in range(20)] - l_elephant, l_eagle, l_wolf, l_lion, l_leopard, l_mouse, l_fox, r_elephant, r_eagle, r_wolf, r_lion, r_leopard, r_fox, r_mouse = init.refresh() - animalsName = init.getAnimalsname() - print(animalsName) + axis = [(0, 0), (2, 6), (1, 1), (1, 5), (2, 2), (2, 4), (2, 3), (2, 0), (6, 0), (7, 5), (7, 1), (6, 4), (6, 2), + (6, 3), (6, 6)] + Axis = s.Chessmen_axis(axis) pygame.quit() diff --git a/src/menu.py b/src/menu.py new file mode 100644 index 0000000..3a2a178 --- /dev/null +++ b/src/menu.py @@ -0,0 +1,30 @@ +import pygame +import pygame_menu + + + +def pause(): + pass + + +def report(): + pass + + +def surrender(): + pass + + +def start_menu(): + pygame.init() + surface = pygame.display.set_mode((600, 400)) + menu = pygame_menu.Menu('DO YOUR ACTIONS', 400, 300, theme = pygame_menu.themes.THEME_BLUE) + menu.add.button('Pause', pause) + menu.add.button('Surrender', surrender) + menu.add.button('Report', report) + menu.add.button('Quit', pygame_menu.events.EXIT) + + menu.mainloop(surface) + +if __name__ == '__main__': + start_menu() diff --git a/src/socket_client.py b/src/socket_client.py index d3b200b..9e4cf83 100644 --- a/src/socket_client.py +++ b/src/socket_client.py @@ -1,5 +1,5 @@ class Player: - def __init__(self, counterpart_name=None,name=None, game_id=None, side=None,think_time=30,total_time=600,id = None,status_2 = None,request = None): + def __init__(self, counterpart_name=None,name=None, game_id=None, side=None,think_time=30,total_time=600,id = None,status_2 = None,request = None,invalid = 1): self.name = name self.game_id = game_id self.side = side @@ -9,6 +9,7 @@ class Player: self.status_2 = status_2 self.counterpart_name = counterpart_name self.request = request + self.invalid = invalid class Chessmen_axis: diff --git a/src/start_game.py b/src/start_game.py index c7afe0b..cd8b055 100644 --- a/src/start_game.py +++ b/src/start_game.py @@ -101,3 +101,64 @@ def starting_screen(): if exit_button.check_click(pygame.mouse.get_pos()): return 0 +def starting_menu(): + WIN.blit(SPACE, (0, 0)) + + game_title = font.render('Menu', True, WHITE) + + WIN.blit(game_title, (WIDTH//2 - game_title.get_width()//2, 150)) + + play_button = Button('Pause', WHITE, None, 350, centered_x=True) + Oplay_button = Button('Report', WHITE, None, 400, centered_x=True) + exit_button = Button('Quit', WHITE, None, 500, centered_x=True) + surrender_button = Button('Surrender', WHITE, None, 450, centered_x=True) + + play_button.display() + Oplay_button.display() + exit_button.display() + surrender_button.display() + + pygame.display.update() + + while True: + + if play_button.check_click(pygame.mouse.get_pos()): + play_button = Button('Pause', RED, None, 350, centered_x=True) + else: + play_button = Button('Pause', WHITE, None, 350, centered_x=True) + + if Oplay_button.check_click(pygame.mouse.get_pos()): + Oplay_button = Button('Report', RED, None, 400, centered_x=True) + else: + Oplay_button = Button('Report', WHITE, None, 400, centered_x=True) + + if surrender_button.check_click(pygame.mouse.get_pos()): + surrender_button = Button('Surrender', RED, None, 450, centered_x=True) + else: + surrender_button = Button('Surrender', WHITE, None, 450, centered_x=True) + + if exit_button.check_click(pygame.mouse.get_pos()): + exit_button = Button('Quit', RED, None, 500, centered_x=True) + else: + exit_button = Button('Quit', WHITE, None, 500, centered_x=True) + + play_button.display() + Oplay_button.display() + exit_button.display() + surrender_button.display() + + pygame.display.update() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + raise SystemExit + if pygame.mouse.get_pressed()[0]: + if play_button.check_click(pygame.mouse.get_pos()): + return 1 + if Oplay_button.check_click(pygame.mouse.get_pos()): + return 2 + if surrender_button.check_click(pygame.mouse.get_pos()): + return 3 + if exit_button.check_click(pygame.mouse.get_pos()): + return 0 \ No newline at end of file