From eec3b64849d4e3f72f12dfe7b917a7920bf5ce0d Mon Sep 17 00:00:00 2001 From: s464947 Date: Thu, 28 Apr 2022 17:21:35 +0200 Subject: [PATCH 1/6] integrate movement and a* --- game_objects/aiPlayer.py | 4 ++-- main.py | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/game_objects/aiPlayer.py b/game_objects/aiPlayer.py index 5f5c588..a3239dd 100644 --- a/game_objects/aiPlayer.py +++ b/game_objects/aiPlayer.py @@ -22,7 +22,7 @@ class aiPlayer(): self.game.update() self.player.update() self.game.draw() - print(self.player.get_actual_coords()) + # print(self.player.get_actual_coords()) @@ -37,7 +37,7 @@ class aiPlayer(): def startAiController(self, actions): for action in actions: - if action == 'straight': + if action == 'forward': self.moveAiPlayer() print(f'ROT IS {self.player.rot}') if action == 'right': diff --git a/main.py b/main.py index 713b383..2cd9212 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from settings import * from map import map from map import map_utils from path_search_algorthms import bfs -# from path_search_algorthms import a_star +from path_search_algorthms import a_star from game_objects import aiPlayer @@ -24,7 +24,7 @@ class Game(): self.init_game() # because dont work without data.txt # self.init_bfs() - # self.init_a_star() + self.init_a_star() self.dt = self.clock.tick(FPS) / 1000.0 @@ -36,7 +36,7 @@ class Game(): self.agentSprites = pg.sprite.Group() # player obj - self.player = Player(self, 32, 100) + self.player = Player(self, 32, 32) # camera obj self.camera = map_utils.Camera(MAP_WIDTH_PX, MAP_HEIGHT_PX) @@ -79,11 +79,6 @@ class Game(): # game loop - set self.playing = False to end the game self.playing = True - actions = ['right', 'straight', 'straight', 'left', 'straight' - ] - t = aiPlayer.aiPlayer(self.player, game=self) - t.startAiController(actions=actions) - while self.playing: self.dt = self.clock.tick(FPS) / 1000.0 self.events() @@ -129,7 +124,10 @@ class Game(): if event.type == pg.MOUSEBUTTONUP: pos = pg.mouse.get_pos() clicked_coords = [math.floor(pos[0] / TILESIZE), math.floor(pos[1] / TILESIZE)] - print(clicked_coords) + actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), clicked_coords[0], clicked_coords[1], self.mapArray) + print(actions) + t = aiPlayer.aiPlayer(self.player, game=self) + t.startAiController(actions=actions) def show_start_screen(self): pass -- 2.20.1 From ab4b404ba419480679149e8d5b65f2e01e4f6856 Mon Sep 17 00:00:00 2001 From: s464947 Date: Thu, 28 Apr 2022 18:43:15 +0200 Subject: [PATCH 2/6] delete angle property --- game_objects/aiPlayer.py | 14 +++++++------- main.py | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/game_objects/aiPlayer.py b/game_objects/aiPlayer.py index a3239dd..160596e 100644 --- a/game_objects/aiPlayer.py +++ b/game_objects/aiPlayer.py @@ -1,11 +1,12 @@ import pygame as pg +import math +from settings import * vec = pg.math.Vector2 class aiPlayer(): def __init__(self, player, game): self.player = player self.game = game - self.angle = 0 def rotateAiPlayer(self, d: str): if d == 'left': @@ -16,7 +17,7 @@ class aiPlayer(): def moveAiPlayer(self): for i in range(64 * 1): - self.player.pos += vec(1, 0).rotate(self.angle) + self.player.pos += vec(1, 0).rotate(self.player.rot) self.player.rect.center = self.player.pos # print(f'START COORDS: {x_s, x_bias}; CURRENT AGENT COORDS: {self.player.get_actual_coords()}') self.game.update() @@ -28,19 +29,18 @@ class aiPlayer(): def turn_left(self): self.player.rot -= 90 - self.angle -= 90 def turn_right(self): self.player.rot += 90 - self.angle += 90 def startAiController(self, actions): - for action in actions: if action == 'forward': self.moveAiPlayer() - print(f'ROT IS {self.player.rot}') + # print(f'ROT IS {self.player.rot}') if action == 'right': self.turn_right() if action == 'left': - self.turn_left() \ No newline at end of file + self.turn_left() + print(f'ROT: {self.player.rot}') + print("Agent pos: ", math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE)) \ No newline at end of file diff --git a/main.py b/main.py index 2cd9212..66e175d 100644 --- a/main.py +++ b/main.py @@ -24,7 +24,7 @@ class Game(): self.init_game() # because dont work without data.txt # self.init_bfs() - self.init_a_star() + # self.init_a_star() self.dt = self.clock.tick(FPS) / 1000.0 -- 2.20.1 From ffffa3a308bec88741263a39273d1c55059919a5 Mon Sep 17 00:00:00 2001 From: czorekk Date: Thu, 28 Apr 2022 20:12:46 +0200 Subject: [PATCH 3/6] init --- game_objects/aiPlayer.py | 20 ++++++++++---------- game_objects/player.py | 16 ++++++++++++++++ main.py | 6 +++--- path_search_algorthms/a_star.py | 4 ++-- path_search_algorthms/a_star_utils.py | 2 +- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/game_objects/aiPlayer.py b/game_objects/aiPlayer.py index 160596e..62683f9 100644 --- a/game_objects/aiPlayer.py +++ b/game_objects/aiPlayer.py @@ -1,3 +1,4 @@ +from game_objects.player import Player import pygame as pg import math from settings import * @@ -5,16 +6,9 @@ vec = pg.math.Vector2 class aiPlayer(): def __init__(self, player, game): - self.player = player + self.player : Player = player self.game = game - def rotateAiPlayer(self, d: str): - if d == 'left': - print('in left') - self.direction -= 90 - if d == 'right': - self.direction += 90 - def moveAiPlayer(self): for i in range(64 * 1): self.player.pos += vec(1, 0).rotate(self.player.rot) @@ -28,10 +22,16 @@ class aiPlayer(): def turn_left(self): - self.player.rot -= 90 + change = int(self.player.rotation()) - 1 + if(change == -1): + change = 3 + self.player.set_rotation(change) def turn_right(self): - self.player.rot += 90 + change = int(self.player.rotation()) + 1 + if(change == 4): + change = 0 + self.player.set_rotation(change) def startAiController(self, actions): for action in actions: diff --git a/game_objects/player.py b/game_objects/player.py index 615922d..cc4bd3d 100644 --- a/game_objects/player.py +++ b/game_objects/player.py @@ -1,3 +1,4 @@ +from path_search_algorthms import a_star_utils import pygame as pg from settings import * from game_objects import utils @@ -17,6 +18,21 @@ class Player(pg.sprite.Sprite): self.vel = vec(0, 0) self.pos = vec(x, y) self.rot = 0 + self.__rotation = a_star_utils.Rotation.RIGHT + + def rotation(self) -> a_star_utils.Rotation: + return self.__rotation + + def set_rotation(self, rotation): + self.__rotation = rotation + if (rotation == a_star_utils.Rotation.UP or rotation == int(a_star_utils.Rotation.UP)): + self.rot = 90 + elif (rotation == a_star_utils.Rotation.RIGHT or rotation == int(a_star_utils.Rotation.RIGHT)): + self.rot = 0 + elif (rotation == a_star_utils.Rotation.DOWN or rotation == int(a_star_utils.Rotation.DOWN)): + self.rot = 270 + elif (rotation == a_star_utils.Rotation.LEFT or rotation == int(a_star_utils.Rotation.LEFT)): + self.rot = 180 def get_keys(self): diff --git a/main.py b/main.py index 66e175d..aea7a81 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ from settings import * from map import map from map import map_utils from path_search_algorthms import bfs -from path_search_algorthms import a_star +from path_search_algorthms import a_star, a_star_utils from game_objects import aiPlayer @@ -124,10 +124,10 @@ class Game(): if event.type == pg.MOUSEBUTTONUP: pos = pg.mouse.get_pos() clicked_coords = [math.floor(pos[0] / TILESIZE), math.floor(pos[1] / TILESIZE)] - actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), clicked_coords[0], clicked_coords[1], self.mapArray) + actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray) print(actions) t = aiPlayer.aiPlayer(self.player, game=self) - t.startAiController(actions=actions) + t.startAiController(actions) def show_start_screen(self): pass diff --git a/path_search_algorthms/a_star.py b/path_search_algorthms/a_star.py index 2fbe15b..b2e4601 100644 --- a/path_search_algorthms/a_star.py +++ b/path_search_algorthms/a_star.py @@ -1,9 +1,9 @@ from data_structures.heap import Heap from path_search_algorthms import a_star_utils as utils -def search_path(start_x: int, start_y: int, target_x: int, target_y: int, array: list[list[int]]) -> list[str]: +def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array: list[list[int]]) -> list[str]: - start_node = utils.Node(start_x, start_y, utils.Rotation.RIGHT) + start_node = utils.Node(start_x, start_y, agent_rotation) target_node = utils.Node(target_x, target_y, utils.Rotation.NONE) # heap version diff --git a/path_search_algorthms/a_star_utils.py b/path_search_algorthms/a_star_utils.py index 4510a91..b757be6 100644 --- a/path_search_algorthms/a_star_utils.py +++ b/path_search_algorthms/a_star_utils.py @@ -75,7 +75,7 @@ def get_move(start_node: Node, target_node: Node) -> list[str]: return ["forward"] if (abs(rotate_change) == 2): return ["right", "right", "forward"] - if (rotate_change < 0 or rotate_change == 3): + if (rotate_change == -1 or rotate_change == 3): return ["right", "forward"] else: return ["left", "forward"] -- 2.20.1 From e6448b61cd37bb64aaf9ef0ef53f54cd1d30d161 Mon Sep 17 00:00:00 2001 From: czorekk Date: Thu, 28 Apr 2022 21:28:03 +0200 Subject: [PATCH 4/6] [integrate_a_star_and_movement] added camera position fix --- game_objects/aiPlayer.py | 6 ++---- game_objects/player.py | 13 ++++++++----- main.py | 3 ++- map/map_utils.py | 4 ++++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/game_objects/aiPlayer.py b/game_objects/aiPlayer.py index 62683f9..e79482e 100644 --- a/game_objects/aiPlayer.py +++ b/game_objects/aiPlayer.py @@ -18,8 +18,6 @@ class aiPlayer(): self.player.update() self.game.draw() # print(self.player.get_actual_coords()) - - def turn_left(self): change = int(self.player.rotation()) - 1 @@ -42,5 +40,5 @@ class aiPlayer(): self.turn_right() if action == 'left': self.turn_left() - print(f'ROT: {self.player.rot}') - print("Agent pos: ", math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE)) \ No newline at end of file + # print(f'ROT: {self.player.rot}') + # print("Agent pos: ", math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE)) \ No newline at end of file diff --git a/game_objects/player.py b/game_objects/player.py index cc4bd3d..ce88e6d 100644 --- a/game_objects/player.py +++ b/game_objects/player.py @@ -26,14 +26,13 @@ class Player(pg.sprite.Sprite): def set_rotation(self, rotation): self.__rotation = rotation if (rotation == a_star_utils.Rotation.UP or rotation == int(a_star_utils.Rotation.UP)): - self.rot = 90 + self.rot = -90 elif (rotation == a_star_utils.Rotation.RIGHT or rotation == int(a_star_utils.Rotation.RIGHT)): self.rot = 0 elif (rotation == a_star_utils.Rotation.DOWN or rotation == int(a_star_utils.Rotation.DOWN)): - self.rot = 270 + self.rot = 90 elif (rotation == a_star_utils.Rotation.LEFT or rotation == int(a_star_utils.Rotation.LEFT)): self.rot = 180 - def get_keys(self): self.rot_speed = 0 @@ -51,8 +50,12 @@ class Player(pg.sprite.Sprite): def update(self): self.get_keys() - self.rot = (self.rot + self.rot_speed * self.game.dt) % 360 - self.image = pg.transform.rotate(self.game.player_img, self.rot) + # must be fix for manual movement + # self.rot = (self.rot + self.rot_speed * self.game.dt) % 360 + image_rotation = self.rot + if(abs(image_rotation) == 90): + image_rotation *= -1 + self.image = pg.transform.rotate(self.game.player_img, image_rotation) self.rect = self.image.get_rect() self.rect.center = self.pos self.pos += self.vel * self.game.dt diff --git a/main.py b/main.py index aea7a81..5410883 100644 --- a/main.py +++ b/main.py @@ -123,7 +123,8 @@ class Game(): self.debug_mode = not self.debug_mode if event.type == pg.MOUSEBUTTONUP: pos = pg.mouse.get_pos() - clicked_coords = [math.floor(pos[0] / TILESIZE), math.floor(pos[1] / TILESIZE)] + offset_x, offset_y = self.camera.offset() + clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y] actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray) print(actions) t = aiPlayer.aiPlayer(self.player, game=self) diff --git a/map/map_utils.py b/map/map_utils.py index adf40e0..f889e2c 100644 --- a/map/map_utils.py +++ b/map/map_utils.py @@ -47,6 +47,10 @@ class Camera: def apply_rect(self, rect): return rect.move(self.camera.topleft) + + def offset(self): + x, y = self.camera.topleft + return x//TILE_SIZE_PX, y//TILE_SIZE_PX def update(self,target): x = -target.rect.x + int(WIDTH/2) -- 2.20.1 From 37375d4f1d9876b91a44bfc74cf6d6fd0a41d80b Mon Sep 17 00:00:00 2001 From: czorekk Date: Fri, 29 Apr 2022 11:01:16 +0200 Subject: [PATCH 5/6] [integrate_a_star_and_movement] little fixes --- main.py | 5 +++-- map/map.py | 18 +++++++++--------- path_search_algorthms/a_star_utils.py | 4 ++-- settings.py | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 5410883..598f5b4 100644 --- a/main.py +++ b/main.py @@ -127,8 +127,9 @@ class Game(): clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y] actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray) print(actions) - t = aiPlayer.aiPlayer(self.player, game=self) - t.startAiController(actions) + if (actions != None): + t = aiPlayer.aiPlayer(self.player, game=self) + t.startAiController(actions) def show_start_screen(self): pass diff --git a/map/map.py b/map/map.py index fa26b71..b4c332c 100644 --- a/map/map.py +++ b/map/map.py @@ -4,17 +4,17 @@ import pygame as pg from settings import * def get_tiles(): - # array = map_utils.generate_map() - array = map_utils.get_blank_map_array() + array = map_utils.generate_map() + # array = map_utils.get_blank_map_array() - array[1][1] = 1 - array[1][2] = 1 - array[1][3] = 1 - array[1][4] = 1 - array[1][5] = 1 - array[1][6] = 1 + # array[1][1] = 1 + # array[1][2] = 1 + # array[1][3] = 1 + # array[1][4] = 1 + # array[1][5] = 1 + # array[1][6] = 1 - array[2][5] = 1 + # array[2][5] = 1 pattern = map_pattern.get_pattern() tiles = map_utils.get_sprites(array, pattern) diff --git a/path_search_algorthms/a_star_utils.py b/path_search_algorthms/a_star_utils.py index b757be6..c0f98ce 100644 --- a/path_search_algorthms/a_star_utils.py +++ b/path_search_algorthms/a_star_utils.py @@ -26,7 +26,7 @@ class Node: def f_cost(self): return self.g_cost + self.h_cost -def get_neighbours(node: Node, searched_list: list[Node], array: list[list[int]]) -> list[Node]: +def get_neighbours(node: 'Node', searched_list: list['Node'], array: list[list[int]]) -> list['Node']: neighbours = [] for offset_x in range (-1, 2): for offset_y in range (-1, 2): @@ -35,7 +35,7 @@ def get_neighbours(node: Node, searched_list: list[Node], array: list[list[int]] x = node.x + offset_x y = node.y + offset_y # prevent out of map coords - if (x >= 0 and x <= MAP_WIDTH and y >= 0 and y <= MAP_HEIGHT): + if (x >= 0 and x < MAP_WIDTH and y >= 0 and y < MAP_HEIGHT): if(array[y][x] == ROAD_TILE and (x, y) not in searched_list): neighbour = Node(x, y, Rotation.NONE) neighbour.rotation = get_needed_rotation(node, neighbour) diff --git a/settings.py b/settings.py index 6bb0ab2..72c250a 100644 --- a/settings.py +++ b/settings.py @@ -27,8 +27,8 @@ PLAYER_WIDTH = 64 PLAYER_HEIGHT = 32 #map settings -MAP_WIDTH = 20 -MAP_HEIGHT = 20 +MAP_WIDTH = 16 +MAP_HEIGHT = 12 TILE_SIZE_PX = 64 MAP_WIDTH_PX = MAP_WIDTH * TILE_SIZE_PX -- 2.20.1 From 29a11547f42ef73a7e2aaddfc2ba3d695a0c77a1 Mon Sep 17 00:00:00 2001 From: aliaksei-kudravets Date: Fri, 6 May 2022 12:27:18 +0200 Subject: [PATCH 6/6] a star1.0 --- path_search_algorthms/a_star.py | 4 ++-- path_search_algorthms/a_star_utils.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/path_search_algorthms/a_star.py b/path_search_algorthms/a_star.py index b2e4601..c3c1efe 100644 --- a/path_search_algorthms/a_star.py +++ b/path_search_algorthms/a_star.py @@ -1,7 +1,7 @@ from data_structures.heap import Heap from path_search_algorthms import a_star_utils as utils -def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array: list[list[int]]) -> list[str]: +def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, target_x: int, target_y: int, array): start_node = utils.Node(start_x, start_y, agent_rotation) target_node = utils.Node(target_x, target_y, utils.Rotation.NONE) @@ -84,7 +84,7 @@ def search_path(start_x: int, start_y: int, agent_rotation: utils.Rotation, targ # if(neighbour not in search_list): # search_list.append(neighbour) -def trace_path(end_node: utils.Node) -> list[str]: +def trace_path(end_node: utils.Node): path = [] node = end_node diff --git a/path_search_algorthms/a_star_utils.py b/path_search_algorthms/a_star_utils.py index c0f98ce..20caee1 100644 --- a/path_search_algorthms/a_star_utils.py +++ b/path_search_algorthms/a_star_utils.py @@ -26,7 +26,7 @@ class Node: def f_cost(self): return self.g_cost + self.h_cost -def get_neighbours(node: 'Node', searched_list: list['Node'], array: list[list[int]]) -> list['Node']: +def get_neighbours(node, searched_list, array): neighbours = [] for offset_x in range (-1, 2): for offset_y in range (-1, 2): @@ -45,7 +45,7 @@ def get_neighbours(node: 'Node', searched_list: list['Node'], array: list[list[i # move cost schema: # - move from tile to tile: 10 # - add extra 10 (1 rotation) if it exists -def get_h_cost(start_node: Node, target_node: Node) -> int: +def get_h_cost(start_node: Node, target_node: Node): distance_x = abs(start_node.x - target_node.x) distance_y = abs(start_node.y - target_node.y) cost = (distance_x + distance_y) * 10 @@ -69,7 +69,7 @@ def get_neighbour_cost(start_node: Node, target_node: Node) -> int: return 30 # translate rotation change to move -def get_move(start_node: Node, target_node: Node) -> list[str]: +def get_move(start_node: Node, target_node: Node): rotate_change = get_rotate_change(start_node.rotation, target_node.rotation) if (rotate_change == 0): return ["forward"] -- 2.20.1