From 8666a7dbf9230e8a474a0a91e09cc733274c0171 Mon Sep 17 00:00:00 2001 From: AleksandraMuczynska Date: Thu, 9 Jun 2022 22:29:44 +0200 Subject: [PATCH] actually WORKING!!!!! one-direction attack with no ghost-knights (only knights, no monsters and fortress) --- common/constants.py | 2 +- logic/knights_queue.py | 2 +- logic/level.py | 79 +++++++++++++++++++++++++++++++++++------- ui/stats.py | 2 +- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/common/constants.py b/common/constants.py index d4b40d8..16cb000 100644 --- a/common/constants.py +++ b/common/constants.py @@ -6,7 +6,7 @@ GAME_TITLE = 'WMICraft' WINDOW_HEIGHT = 800 WINDOW_WIDTH = 1360 FPS_COUNT = 60 -TURN_INTERVAL = 500 +TURN_INTERVAL = 100 GRID_CELL_PADDING = 5 GRID_CELL_SIZE = 36 diff --git a/logic/knights_queue.py b/logic/knights_queue.py index d9db7fb..304b06d 100644 --- a/logic/knights_queue.py +++ b/logic/knights_queue.py @@ -10,7 +10,7 @@ class KnightsQueue: def dequeue_knight(self): if self.both_teams_alive(): knight = self.queues[self.team_idx_turn].popleft() - if knight.max_hp <= 0: + if knight.health_bar.current_hp <= 0: return self.dequeue_knight() else: self.queues[self.team_idx_turn].append(knight) diff --git a/logic/level.py b/logic/level.py index df2c462..b14548b 100644 --- a/logic/level.py +++ b/logic/level.py @@ -92,13 +92,7 @@ class Level: self.map[row_index][col_index] = castle self.list_castles.append(castle) - def handle_turn(self): - current_knight = self.knights_queue.dequeue_knight() - knights_list = self.list_knights_red + self.list_knights_blue - print("next turn " + current_knight.team) - knight_pos_x = current_knight.position[0] - knight_pos_y = current_knight.position[1] - positions = [] + def attack_knight(self, knights_list, positions, current_knight): op_pos_1 = current_knight.position[0] - 1, current_knight.position[1] positions.append(op_pos_1) op_pos_2 = current_knight.position[0], current_knight.position[1] - 1 @@ -107,6 +101,53 @@ class Level: positions.append(op_pos_3) op_pos_4 = current_knight.position[0], current_knight.position[1] + 1 positions.append(op_pos_4) + for some_knight in knights_list: + for some_position in positions: + if some_knight.position == some_position: + some_knight.health_bar.take_dmg(1) + if some_knight.health_bar.current_hp == 0: + some_knight.kill() + positions.clear() + + def attack_knight_left(self, knights_list, current_knight): + position_left = current_knight.position[0] - 1, current_knight.position[1] + for some_knight in knights_list: + if some_knight.position == position_left: + some_knight.health_bar.take_dmg(1) + if some_knight.health_bar.current_hp == 0: + some_knight.kill() + + def attack_knight_right(self, knights_list, current_knight): + position_right = current_knight.position[0] + 1, current_knight.position[1] + for some_knight in knights_list: + if some_knight.position == position_right: + some_knight.health_bar.take_dmg(1) + if some_knight.health_bar.current_hp == 0: + some_knight.kill() + + def attack_knight_up(self, knights_list, current_knight): + position_up = current_knight.position[0] , current_knight.position[1] - 1 + for some_knight in knights_list: + if some_knight.position == position_up: + some_knight.health_bar.take_dmg(1) + if some_knight.health_bar.current_hp == 0: + some_knight.kill() + + def attack_knight_down(self, knights_list, current_knight): + position_down = current_knight.position[0], current_knight.position[1] + 1 + for some_knight in knights_list: + if some_knight.position == position_down: + some_knight.health_bar.take_dmg(1) + if some_knight.health_bar.current_hp == 0: + some_knight.kill() + + def handle_turn(self): + current_knight = self.knights_queue.dequeue_knight() + knights_list = self.list_knights_red + self.list_knights_blue + print("next turn " + current_knight.team) + knight_pos_x = current_knight.position[0] + knight_pos_y = current_knight.position[1] + positions = [] goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight, monsters=self.list_monsters, @@ -114,6 +155,9 @@ class Level: if current_knight.team_alias() == 'k_r' else self.list_knights_red, castle=self.list_castles[0]) + if (len(self.list_knights_blue) == 0 or len(self.list_knights_red) == 0): + pygame.quit() + if len(goal_list) == 0: return @@ -126,10 +170,18 @@ class Level: return next_action = action_list.pop(0) - for some_knight in knights_list: - for some_position in positions: - if some_knight.position == some_position: - some_knight.health_bar.take_dmg(1) + + #if current_knight.health_bar.current_hp != 0: + #self.attack_knight(knights_list, positions, current_knight) + + if current_knight.direction.name == UP: + self.attack_knight_up(knights_list, current_knight) + elif current_knight.direction.name == DOWN: + self.attack_knight_down(knights_list, current_knight) + elif current_knight.direction.name == RIGHT: + self.attack_knight_right(knights_list, current_knight) + elif current_knight.direction.name == LEFT: + self.attack_knight_left(knights_list, current_knight) if next_action == TURN_LEFT: self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.') @@ -141,7 +193,7 @@ class Level: current_knight.step_forward() self.map[knight_pos_y][knight_pos_x] = MAP_ALIASES.get("GRASS") - # update knight on map + # update knight on map if current_knight.direction.name == UP: self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.') self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias() @@ -163,3 +215,6 @@ class Level: # update and draw the game self.sprites.draw(self.screen) self.sprites.update() + + + diff --git a/ui/stats.py b/ui/stats.py index d98164a..7cb97ab 100644 --- a/ui/stats.py +++ b/ui/stats.py @@ -51,7 +51,7 @@ class Stats: # texts draw_text('Rycerze: ' + str(len(self.list_knights_blue)), FONT_DARK, self.screen, self.x + 35, self.y + 240, 18) # blue - draw_text('Rycerze: 4', FONT_DARK, self.screen, self.x + 215, self.y + 240, 18) + draw_text('Rycerze: ' + str(len(self.list_knights_red)), FONT_DARK, self.screen, self.x + 215, self.y + 240, 18) # points pygame.draw.rect(self.screen, ORANGE, pygame.Rect(self.x, self.y + 390, 340, 3))