This commit is contained in:
czorekk 2022-04-28 20:12:46 +02:00
parent ab4b404ba4
commit ffffa3a308
5 changed files with 32 additions and 16 deletions

View File

@ -1,3 +1,4 @@
from game_objects.player import Player
import pygame as pg import pygame as pg
import math import math
from settings import * from settings import *
@ -5,16 +6,9 @@ vec = pg.math.Vector2
class aiPlayer(): class aiPlayer():
def __init__(self, player, game): def __init__(self, player, game):
self.player = player self.player : Player = player
self.game = game 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): def moveAiPlayer(self):
for i in range(64 * 1): for i in range(64 * 1):
self.player.pos += vec(1, 0).rotate(self.player.rot) self.player.pos += vec(1, 0).rotate(self.player.rot)
@ -28,10 +22,16 @@ class aiPlayer():
def turn_left(self): 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): 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): def startAiController(self, actions):
for action in actions: for action in actions:

View File

@ -1,3 +1,4 @@
from path_search_algorthms import a_star_utils
import pygame as pg import pygame as pg
from settings import * from settings import *
from game_objects import utils from game_objects import utils
@ -17,6 +18,21 @@ class Player(pg.sprite.Sprite):
self.vel = vec(0, 0) self.vel = vec(0, 0)
self.pos = vec(x, y) self.pos = vec(x, y)
self.rot = 0 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): def get_keys(self):

View File

@ -9,7 +9,7 @@ from settings import *
from map import map from map import map
from map import map_utils from map import map_utils
from path_search_algorthms import bfs 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 from game_objects import aiPlayer
@ -124,10 +124,10 @@ class Game():
if event.type == pg.MOUSEBUTTONUP: if event.type == pg.MOUSEBUTTONUP:
pos = pg.mouse.get_pos() pos = pg.mouse.get_pos()
clicked_coords = [math.floor(pos[0] / TILESIZE), math.floor(pos[1] / TILESIZE)] 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) print(actions)
t = aiPlayer.aiPlayer(self.player, game=self) t = aiPlayer.aiPlayer(self.player, game=self)
t.startAiController(actions=actions) t.startAiController(actions)
def show_start_screen(self): def show_start_screen(self):
pass pass

View File

@ -1,9 +1,9 @@
from data_structures.heap import Heap from data_structures.heap import Heap
from path_search_algorthms import a_star_utils as utils 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) target_node = utils.Node(target_x, target_y, utils.Rotation.NONE)
# heap version # heap version

View File

@ -75,7 +75,7 @@ def get_move(start_node: Node, target_node: Node) -> list[str]:
return ["forward"] return ["forward"]
if (abs(rotate_change) == 2): if (abs(rotate_change) == 2):
return ["right", "right", "forward"] return ["right", "right", "forward"]
if (rotate_change < 0 or rotate_change == 3): if (rotate_change == -1 or rotate_change == 3):
return ["right", "forward"] return ["right", "forward"]
else: else:
return ["left", "forward"] return ["left", "forward"]