From ffffa3a308bec88741263a39273d1c55059919a5 Mon Sep 17 00:00:00 2001 From: czorekk Date: Thu, 28 Apr 2022 20:12:46 +0200 Subject: [PATCH] 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"]