def Map(gameDisplay): gameDisplay.fill((255, 255, 000)) map = select.selectMap(2) map = select.constructMap(map) # map = np.array(select.constructMap(map)) # map = map.transpose() # print(map) # map -> color # 0 - green (normal) # 1 - blue (water) # 2 - brown (mountain) x = 200 y = 100 xAis = len(map) # 15 yAis = len(map[1]) # 10 xIndex = 0 yIndex = 0 # print(map[xIndex]) # print(map[xIndex][yIndex]) while yAis: if (yIndex == yAis): break xIndex = 0 y += 40 x = 200 while xAis: if (xIndex == xAis): break if (map[xIndex][yIndex] == 0): Color = light_green elif (map[xIndex][yIndex] == 1): Color = blue elif (map[xIndex][yIndex] == 2): Color = brown pygame.draw.rect(gameDisplay, Color, (x, y, 50, 50)) x += 40 # y+=40 xIndex += 1 yIndex += 1 pygame.display.update()
def loop(self, client, addr): while True: try: data = client.recv(2048).decode('utf-8') if data: print(data) payload = json.loads(data) print(payload) except socket.error: break if payload['event'] == RequestType.joinevent: if len(self.rooms) == 0: self.rooms.append(Room()) self.rooms[len(self.rooms) - 1].mapId = random.randint( 1, 2) self.rooms[len(self.rooms) - 1].playerType = random.randint(1, 2) self.rooms[len(self.rooms) - 1].user.append( payload['player']) room['room'] = len(self.rooms) room['turn'] = 1 self.userlist[payload['player']].send( bytes(json.dumps(room).encode('utf-8'))) else: for r in self.rooms: if r.isfull(): continue self.rooms[len(self.rooms) - 1].user.append( payload['player']) room['room'] = len(self.rooms) room['turn'] = 2 self.userlist[payload['player']].send( bytes(json.dumps(room).encode('utf-8'))) break else: self.rooms.append(Room()) self.rooms[len(self.rooms) - 1].mapId = random.randint( 1, 2) self.rooms[len(self.rooms) - 1].playerType = random.randint(1, 2) self.rooms[len(self.rooms) - 1].user.append( payload['player']) room['room'] = len(self.rooms) room['turn'] = 1 self.userlist[payload['player']].send( bytes(json.dumps(room).encode('utf-8'))) elif payload['event'] == RequestType.sync: for i in range(2): if self.rooms[payload['room'] - 1].user[i] != payload['player']: u = self.rooms[payload['room'] - 1].user[i] break self.userlist[u].send( bytes(json.dumps(payload).encode('utf-8'))) elif payload['event'] == RequestType.rank: pay = select.selectRank(payload['name']) self.userlist[payload['player']].send( bytes(json.dumps(pay).encode('utf-8'))) elif payload['event'] == RequestType.map: map = select.selectMap(self.rooms[(payload['room'] - 1)].mapId) self.userlist[payload['player']].send( bytes(json.dumps(map).encode('utf-8'))) elif payload['event'] == RequestType.ac: if payload['num'] == 1: data = UserSignUp.register(payload['name']) self.userlist[payload['player']].send( bytes(json.dumps(data).encode('utf-8'))) elif payload['num'] == 2: data = UserSignUp.login(payload['name']) self.userlist[payload['player']].send( bytes(json.dumps(data).encode('utf-8'))) elif payload['event'] == RequestType.player: player = select.selectDeploy(self.rooms[payload['room'] - 1].mapId) self.userlist[payload['player']].send( bytes(json.dumps(player).encode('utf-8'))) elif payload['event'] == RequestType.win: print('event 8 success.') updatePlayer.winTimesUpdate(name=payload['name'], win=payload['win']) self.userlist[payload['player']].send( bytes(json.dumps({ 'event': 9 }).encode('utf-8'))) elif payload['event'] == RequestType.game_end: print('in the game end event', payload) self.userlist[payload['player']].send( bytes(json.dumps({ 'event': 9 }).encode('utf-8')))
import Player import Army import select import json datas = select.selectMap(1) map = select.constructMap(datas) # for i in range(len(map)): # print(map[i]) ## 0:路 1:水 2:山 areas = json.loads(datas)["Player1_Area"] ##生成區域要判斷 print(type(areas["x1"])) def set(player, ForS, i, x, y): ##傳入要設定的玩家,極其要設定的該軍隊,即要設定的XY座標 ForS first or second if ForS == 1: ##是player1 areas = json.loads(datas)["Player1_Area"] ##生成區域要判斷 elif ForS == 2: ##是player2 areas = json.loads(datas)["Player2_Area"] ##生成區域要判斷 if (player.army[i].x == None & player.army[i].y == None): ##如果玩家該軍隊尚未被設置將可以進行設置 if (x >= areas["x1"] & x <= areas["x2"] & y >= areas["y1"] & y <= areas["y1"]): ##如果玩家要設定軍隊的座標再生成區域內 if (map[x][y] == 1): ##確認是否生成座標是否不再水面或是山上 player.army[i].x = x ##設定軍隊座標 player.army[i].y = y ##設定軍隊座標 return True else: ##座標是在水面或山上 print("只能生成在路面") return False else: ##玩家要設定軍隊的座標不再合法生成區域內