def __gorilla_collision_thread_do_work(self): thread_endpoint = self.__add_cc_endpoint() while not self.__check_thread_kill(): if self.princess_reached is False and self.players[ 0].ready is True and self.players[1].ready is True: for player in self.players: if player.x is not None and player.y is not None: thread_endpoint.send( Message(CCMethods.GORILLA_COLLISION, self.gorilla.x, self.gorilla.y, player.x, player.y)) msg = thread_endpoint.recv() if msg.args[0]: self.__reset_player_pos(player) player.lose_life() message = json.dumps({ "command": ServerMessage.GORILLA_HIT.value, "lives": player.lives }) player.send(message) message = json.dumps({ "command": ServerMessage.GORILLA_HIT_OPPONENT.value, "lives": player.lives }) self.send_to_opponent(message, player) thread_endpoint.send(Message(CCMethods.KILL_PROCESS)) thread_endpoint.close()
def __coin_thread_do_work(self): thread_endpoint = self.__add_cc_endpoint() while not self.__check_thread_kill(): if self.princess_reached is False and self.players[ 0].ready is True and self.players[1].ready is True: if self.coin.drawn is False: time.sleep(10) self.__coin_set_position() self.coin.drawn = True message = json.dumps({ "command": ServerMessage.DRAW_COIN.value, "x": self.coin.x, "y": self.coin.y }) self.send_to_all(message) else: for player in self.players: if player.x is not None and player.y is not None: thread_endpoint.send( Message(CCMethods.COIN_COLLISION, self.coin.x, self.coin.y, player.x, player.y)) msg = thread_endpoint.recv() if msg.args[0]: self.coin.drawn = False message = json.dumps({ "command": ServerMessage.REMOVE_COIN.value }) self.send_to_all(message) self.__coin_add_effect(player) thread_endpoint.send(Message(CCMethods.KILL_PROCESS)) thread_endpoint.close()
def __players_falling_thread_do_work(self): thread_endpoint = self.__add_cc_endpoint() while not self.__check_thread_kill(): if self.players[0].ready is True and self.players[1].ready is True: for player in self.players: if player.x is not None and player.y is not None and ( player.latest_direction == Direction.LEFT or player.latest_direction == Direction.RIGHT): thread_endpoint.send( Message(CCMethods.FALLING, player.x, player.y, player.latest_direction, self.level_layout)) msg = thread_endpoint.recv() if msg.args[0]: player.falling = True message = json.dumps( {"command": ServerMessage.FALL.value}) player.send(message) message = json.dumps( {"command": ServerMessage.FALL_OPPONENT.value}) self.send_to_opponent(message, player) player.y += 5 else: player.falling = False time.sleep(0.02) thread_endpoint.send(Message(CCMethods.KILL_PROCESS)) thread_endpoint.close()
def check_end_of_screen_left(self, index, x): ret_val = False if x <= 0: ret_val = True self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))
def check_gorilla_collision(self, index, g_x, g_y, p_x, p_y): ret_val = True gorilla_y_from = g_y gorilla_pos_y_to = g_y + 55 gorilla_pos_x_from = g_x gorilla_pos_x_to = g_x + 58 player_pos_y_from = p_y player_pos_y_to = p_y + 35 player_pos_x_from = p_x player_pos_x_to = p_x + 26 if ((gorilla_y_from < player_pos_y_from) and (gorilla_pos_y_to < player_pos_y_from)) or ( (gorilla_y_from > player_pos_y_to) and (gorilla_pos_y_to > player_pos_y_to)): ret_val = False if ((gorilla_pos_x_from < player_pos_x_from) and (gorilla_pos_x_to < player_pos_x_from)) or ( (gorilla_pos_x_from > player_pos_x_to) and (gorilla_pos_x_to > player_pos_x_to)): ret_val = False self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))
def check_coin_collision(self, index, c_x, c_y, p_x, p_y): ret_val = True coin_pos_y_from = c_y coin_pos_y_to = c_y + 40 coin_pos_x_from = c_x coin_pos_x_to = c_x + 40 player_pos_y_from = p_y player_pos_y_to = p_y + 35 player_pos_x_from = p_x player_pos_x_to = p_x + 26 if ((coin_pos_y_from < player_pos_y_from) and (coin_pos_y_to < player_pos_y_from)) or ( (coin_pos_y_from > player_pos_y_to) and (coin_pos_y_to > player_pos_y_to)): ret_val = False if ((coin_pos_x_from < player_pos_x_from) and (coin_pos_x_to < player_pos_x_from)) or ( (coin_pos_x_from > player_pos_x_to) and (coin_pos_x_to > player_pos_x_to)): ret_val = False self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))
def check_princess_collision(self, index, pr_x, pr_y, p_x, p_y): ret_val = True princess_pos_y_from = pr_y princess_pos_y_to = pr_y + 37 princess_pos_x_from = pr_x princess_pos_x_to = pr_x + 34 player_pos_y_from = p_y player_pos_y_to = p_y + 35 player_pos_x_from = p_x player_pos_x_to = p_x + 26 if ((princess_pos_y_from < player_pos_y_from) and (princess_pos_y_to < player_pos_y_from)) or ( (princess_pos_y_from > player_pos_y_to) and (princess_pos_y_to > player_pos_y_to)): ret_val = False if ((princess_pos_x_from < player_pos_x_from) and (princess_pos_x_to < player_pos_x_from)) or ( (princess_pos_x_from > player_pos_x_to) and (princess_pos_x_to > player_pos_x_to)): ret_val = False self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))
def check_climbing_up(self, index, player_x, player_y, layout): # default return value, if it's NONE, don't climb ret_value = ClimbState.NONE # get x center point player_x_center = player_x + 13 # get x of the block the player is in x = int(player_x / SCENE_GRID_BLOCK_WIDTH) # get y of the block the player is in (feet level) y = int((player_y + 34) / SCENE_GRID_BLOCK_HEIGHT) if layout[y][x] == LayoutBlock.Ladder: # get climbable ladder x coordinates (the whole ladder is not climbable) ladder_x_from = x * SCENE_GRID_BLOCK_WIDTH + 5 ladder_x_to = x * SCENE_GRID_BLOCK_WIDTH + 30 # check if the player is between climbable x coordinates if (ladder_x_from < player_x_center) and (ladder_x_to > player_x_center): # get y of the block at player head level y = int((player_y + 3) / SCENE_GRID_BLOCK_HEIGHT) # check if block above the player's head is empty if layout[y][x] is None: ret_value = ClimbState.FINISH else: ret_value = ClimbState.CLIMB self.endpoints[index].send(Message(CCMethods.EMPTY, ret_value))
def check_end_of_screen_right(self, index, x): ret_val = False if x >= SCENE_WIDTH - 26: ret_val = True self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))
def __princess_collision_thread_do_work(self): thread_endpoint = self.__add_cc_endpoint() while not self.__check_thread_kill(): if self.princess_reached is False and self.players[ 0].ready is True and self.players[1].ready is True: for player in self.players: if player.x is not None and player.y is not None: thread_endpoint.send( Message(CCMethods.PRINCESS_COLLISION, self.princess.x, self.princess.y, player.x, player.y)) msg = thread_endpoint.recv() if msg.args[0]: self.princess_reached = True self.__set_current_scene() self.load_scene() time.sleep(0.03) thread_endpoint.send(Message(CCMethods.KILL_PROCESS)) thread_endpoint.close()
def move(self, player: Client, direction: Direction): player.latest_direction = direction if direction == Direction.LEFT: self.cc_endpoint.send(Message(CCMethods.END_OF_SCREEN_L, player.x)) msg = self.cc_endpoint.recv() self.__move_left(player, msg) elif direction == Direction.RIGHT: self.cc_endpoint.send(Message(CCMethods.END_OF_SCREEN_R, player.x)) msg = self.cc_endpoint.recv() self.__move_right(player, msg) elif direction == Direction.UP: self.cc_endpoint.send( Message(CCMethods.CLIMB_UP, player.x, player.y, self.level_layout)) msg = self.cc_endpoint.recv() self.__move_up(player, msg) elif direction == Direction.DOWN: self.cc_endpoint.send( Message(CCMethods.CLIMB_DOWN, player.x, player.y, self.level_layout)) msg = self.cc_endpoint.recv() self.__move_down(player, msg)
def check_falling(self, index, player_x, player_y, direction, layout): ret_value = False y = int((player_y + 35) / SCENE_GRID_BLOCK_HEIGHT) if direction == Direction.LEFT: x = int((player_x + 20) / SCENE_GRID_BLOCK_WIDTH) elif direction == Direction.RIGHT: x = int(player_x / SCENE_GRID_BLOCK_WIDTH) else: return False if layout[y][x] is None: ret_value = True elif layout[y][x] == LayoutBlock.Platform or layout[y][ x] == LayoutBlock.Ladder: if (y * SCENE_GRID_BLOCK_HEIGHT) > player_y + 35: ret_value = True self.endpoints[index].send(Message(CCMethods.EMPTY, ret_value))
def check_barrel_collision(self, index, b_x, b_y, p_x, p_y): ret_val = True barrel_pos_y_from = b_y barrel_pos_y_to = b_y + 29 barrel_pos_x_from = b_x barrel_pos_x_to = barrel_pos_x_from + 29 player_pos_y_from = p_y player_pos_y_to = p_y + 35 player_pos_x_from = p_x player_pos_x_to = p_x + 26 if ((barrel_pos_y_from < player_pos_y_from) and (barrel_pos_y_to < player_pos_y_from)) or ( (barrel_pos_y_from > player_pos_y_to) and (barrel_pos_y_to > player_pos_y_to)): ret_val = False if ((barrel_pos_x_from < player_pos_x_from) and (barrel_pos_x_to < player_pos_x_from)) or \ ((barrel_pos_x_from > player_pos_x_to) and (barrel_pos_x_to > player_pos_x_to)): ret_val = False self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))
def __end(self): self.kill_thread_lock.acquire() self.kill_thread = True self.kill_thread_lock.release() self.ending = True message = json.dumps({"command": ServerMessage.MATCH_ENDED.value}) self.send_to_all(message) if self.barrels_fall_thread.isAlive( ) and threading.current_thread() != self.barrels_fall_thread: self.barrels_fall_thread.join() if self.players_falling_thread.isAlive( ) and threading.current_thread() != self.players_falling_thread: self.players_falling_thread.join() if self.princess_collision_thread.isAlive( ) and threading.current_thread() != self.princess_collision_thread: self.princess_collision_thread.join() if self.gorilla_thread.isAlive( ) and threading.current_thread() != self.gorilla_thread: self.gorilla_thread.join() if self.gorilla_collision_thread.isAlive( ) and threading.current_thread() != self.gorilla_collision_thread: self.gorilla_collision_thread.join() if self.check_end_match_thread.isAlive( ) and threading.current_thread() != self.check_end_match_thread: self.check_end_match_thread.join() if self.coin_thread.isAlive( ) and threading.current_thread() != self.coin_thread: self.coin_thread.join() for player in self.players: player.points = 0 self.cc_endpoint.send(Message(CCMethods.KILL_PROCESS)) if self.__parent__.matches.__contains__(self): self.__parent__.matches.remove(self)
def __add_cc_endpoint(self) -> 'mp.Connection': parent, child = mp.Pipe() self.__parent__.cc_endpoint.send(Message(CCMethods.ADD_ENDPOINT, child)) return parent
def __barrels_fall_thread_do_work(self): thread_endpoint = self.__add_cc_endpoint() while not self.__check_thread_kill(): if not self.princess_reached and self.players[ 0].ready and self.players[1].ready: for index, barrel in enumerate(self.barrels): if barrel.drawn: thread_endpoint.send( Message(CCMethods.END_OF_SCREEN_V, barrel.y)) msg = thread_endpoint.recv() # barrel reached end of screen, remove it if msg.args[0]: message = json.dumps({ "command": ServerMessage.REMOVE_BARREL.value, "index": index }) self.send_to_all(message) barrel.drawn = False # barrel has not reached end of screen, fall else: move_barrel = True for player in self.players: if barrel.x is not None and barrel.y is not None and player.x is not None and player.y is not None: thread_endpoint.send( Message(CCMethods.BARREL_COLLISION, barrel.x, barrel.y, player.x, player.y)) msg = thread_endpoint.recv() if msg.args[0]: self.__reset_player_pos(player) player.lose_life() message = json.dumps({ "command": ServerMessage.HIT.value, "index": index, "lives": player.lives }) player.send(message) message = json.dumps({ "command": ServerMessage.OPPONENT_HIT.value, "index": index, "lives": player.lives }) self.send_to_opponent(message, player) barrel.drawn = False move_barrel = False if move_barrel: message = json.dumps({ "command": ServerMessage.MOVE_BARREL.value, "index": index }) self.send_to_all(message) barrel.y += 5 time.sleep(self.barrel_speed) thread_endpoint.send(Message(CCMethods.KILL_PROCESS)) thread_endpoint.close()
def check_end_of_screen_vertical(self, index, y): ret_val = False if y >= SCENE_HEIGHT - 60: ret_val = True self.endpoints[index].send(Message(CCMethods.EMPTY, ret_val))