Merge pull request 'integrate_a_star_and_movement' (#22) from integrate_a_star_and_movement into master
Reviewed-on: #22
This commit is contained in:
commit
0f090f9a85
@ -1,46 +1,44 @@
|
|||||||
|
from game_objects.player import Player
|
||||||
import pygame as pg
|
import pygame as pg
|
||||||
|
import math
|
||||||
|
from settings import *
|
||||||
vec = pg.math.Vector2
|
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
|
||||||
self.angle = 0
|
|
||||||
|
|
||||||
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.angle)
|
self.player.pos += vec(1, 0).rotate(self.player.rot)
|
||||||
self.player.rect.center = self.player.pos
|
self.player.rect.center = self.player.pos
|
||||||
# print(f'START COORDS: {x_s, x_bias}; CURRENT AGENT COORDS: {self.player.get_actual_coords()}')
|
# print(f'START COORDS: {x_s, x_bias}; CURRENT AGENT COORDS: {self.player.get_actual_coords()}')
|
||||||
self.game.update()
|
self.game.update()
|
||||||
self.player.update()
|
self.player.update()
|
||||||
self.game.draw()
|
self.game.draw()
|
||||||
print(self.player.get_actual_coords())
|
# print(self.player.get_actual_coords())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def turn_left(self):
|
def turn_left(self):
|
||||||
self.player.rot -= 90
|
change = int(self.player.rotation()) - 1
|
||||||
self.angle -= 90
|
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
|
||||||
self.angle += 90
|
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:
|
||||||
if action == 'straight':
|
if action == 'forward':
|
||||||
self.moveAiPlayer()
|
self.moveAiPlayer()
|
||||||
print(f'ROT IS {self.player.rot}')
|
# print(f'ROT IS {self.player.rot}')
|
||||||
if action == 'right':
|
if action == 'right':
|
||||||
self.turn_right()
|
self.turn_right()
|
||||||
if action == 'left':
|
if action == 'left':
|
||||||
self.turn_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))
|
@ -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,7 +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 = 90
|
||||||
|
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):
|
||||||
self.rot_speed = 0
|
self.rot_speed = 0
|
||||||
@ -35,8 +50,12 @@ class Player(pg.sprite.Sprite):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
self.get_keys()
|
self.get_keys()
|
||||||
self.rot = (self.rot + self.rot_speed * self.game.dt) % 360
|
# must be fix for manual movement
|
||||||
self.image = pg.transform.rotate(self.game.player_img, self.rot)
|
# 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 = self.image.get_rect()
|
||||||
self.rect.center = self.pos
|
self.rect.center = self.pos
|
||||||
self.pos += self.vel * self.game.dt
|
self.pos += self.vel * self.game.dt
|
||||||
|
18
main.py
18
main.py
@ -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
|
||||||
@ -36,7 +36,7 @@ class Game():
|
|||||||
self.agentSprites = pg.sprite.Group()
|
self.agentSprites = pg.sprite.Group()
|
||||||
|
|
||||||
# player obj
|
# player obj
|
||||||
self.player = Player(self, 32, 100)
|
self.player = Player(self, 32, 32)
|
||||||
|
|
||||||
# camera obj
|
# camera obj
|
||||||
self.camera = map_utils.Camera(MAP_WIDTH_PX, MAP_HEIGHT_PX)
|
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
|
# game loop - set self.playing = False to end the game
|
||||||
self.playing = True
|
self.playing = True
|
||||||
|
|
||||||
actions = ['right', 'straight', 'straight', 'left', 'straight'
|
|
||||||
]
|
|
||||||
t = aiPlayer.aiPlayer(self.player, game=self)
|
|
||||||
t.startAiController(actions=actions)
|
|
||||||
|
|
||||||
while self.playing:
|
while self.playing:
|
||||||
self.dt = self.clock.tick(FPS) / 1000.0
|
self.dt = self.clock.tick(FPS) / 1000.0
|
||||||
self.events()
|
self.events()
|
||||||
@ -128,8 +123,13 @@ class Game():
|
|||||||
self.debug_mode = not self.debug_mode
|
self.debug_mode = not self.debug_mode
|
||||||
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)]
|
offset_x, offset_y = self.camera.offset()
|
||||||
print(clicked_coords)
|
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)
|
||||||
|
if (actions != None):
|
||||||
|
t = aiPlayer.aiPlayer(self.player, game=self)
|
||||||
|
t.startAiController(actions)
|
||||||
|
|
||||||
def show_start_screen(self):
|
def show_start_screen(self):
|
||||||
pass
|
pass
|
||||||
|
18
map/map.py
18
map/map.py
@ -4,17 +4,17 @@ import pygame as pg
|
|||||||
from settings import *
|
from settings import *
|
||||||
|
|
||||||
def get_tiles():
|
def get_tiles():
|
||||||
# array = map_utils.generate_map()
|
array = map_utils.generate_map()
|
||||||
array = map_utils.get_blank_map_array()
|
# array = map_utils.get_blank_map_array()
|
||||||
|
|
||||||
array[1][1] = 1
|
# array[1][1] = 1
|
||||||
array[1][2] = 1
|
# array[1][2] = 1
|
||||||
array[1][3] = 1
|
# array[1][3] = 1
|
||||||
array[1][4] = 1
|
# array[1][4] = 1
|
||||||
array[1][5] = 1
|
# array[1][5] = 1
|
||||||
array[1][6] = 1
|
# array[1][6] = 1
|
||||||
|
|
||||||
array[2][5] = 1
|
# array[2][5] = 1
|
||||||
|
|
||||||
pattern = map_pattern.get_pattern()
|
pattern = map_pattern.get_pattern()
|
||||||
tiles = map_utils.get_sprites(array, pattern)
|
tiles = map_utils.get_sprites(array, pattern)
|
||||||
|
@ -48,6 +48,10 @@ class Camera:
|
|||||||
def apply_rect(self, rect):
|
def apply_rect(self, rect):
|
||||||
return rect.move(self.camera.topleft)
|
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):
|
def update(self,target):
|
||||||
x = -target.rect.x + int(WIDTH/2)
|
x = -target.rect.x + int(WIDTH/2)
|
||||||
y = -target.rect.y + int(HEIGHT / 2)
|
y = -target.rect.y + int(HEIGHT / 2)
|
||||||
|
@ -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):
|
||||||
|
|
||||||
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
|
||||||
@ -84,7 +84,7 @@ def search_path(start_x: int, start_y: int, target_x: int, target_y: int, array:
|
|||||||
# if(neighbour not in search_list):
|
# if(neighbour not in search_list):
|
||||||
# search_list.append(neighbour)
|
# search_list.append(neighbour)
|
||||||
|
|
||||||
def trace_path(end_node: utils.Node) -> list[str]:
|
def trace_path(end_node: utils.Node):
|
||||||
path = []
|
path = []
|
||||||
node = end_node
|
node = end_node
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ class Node:
|
|||||||
def f_cost(self):
|
def f_cost(self):
|
||||||
return self.g_cost + self.h_cost
|
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 = []
|
neighbours = []
|
||||||
for offset_x in range (-1, 2):
|
for offset_x in range (-1, 2):
|
||||||
for offset_y 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
|
x = node.x + offset_x
|
||||||
y = node.y + offset_y
|
y = node.y + offset_y
|
||||||
# prevent out of map coords
|
# 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):
|
if(array[y][x] == ROAD_TILE and (x, y) not in searched_list):
|
||||||
neighbour = Node(x, y, Rotation.NONE)
|
neighbour = Node(x, y, Rotation.NONE)
|
||||||
neighbour.rotation = get_needed_rotation(node, neighbour)
|
neighbour.rotation = get_needed_rotation(node, neighbour)
|
||||||
@ -45,7 +45,7 @@ def get_neighbours(node: Node, searched_list: list[Node], array: list[list[int]]
|
|||||||
# move cost schema:
|
# move cost schema:
|
||||||
# - move from tile to tile: 10
|
# - move from tile to tile: 10
|
||||||
# - add extra 10 (1 rotation) if it exists
|
# - 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_x = abs(start_node.x - target_node.x)
|
||||||
distance_y = abs(start_node.y - target_node.y)
|
distance_y = abs(start_node.y - target_node.y)
|
||||||
cost = (distance_x + distance_y) * 10
|
cost = (distance_x + distance_y) * 10
|
||||||
@ -69,13 +69,13 @@ def get_neighbour_cost(start_node: Node, target_node: Node) -> int:
|
|||||||
return 30
|
return 30
|
||||||
|
|
||||||
# translate rotation change to move
|
# 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)
|
rotate_change = get_rotate_change(start_node.rotation, target_node.rotation)
|
||||||
if (rotate_change == 0):
|
if (rotate_change == 0):
|
||||||
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"]
|
||||||
|
@ -27,8 +27,8 @@ PLAYER_WIDTH = 64
|
|||||||
PLAYER_HEIGHT = 32
|
PLAYER_HEIGHT = 32
|
||||||
|
|
||||||
#map settings
|
#map settings
|
||||||
MAP_WIDTH = 20
|
MAP_WIDTH = 16
|
||||||
MAP_HEIGHT = 20
|
MAP_HEIGHT = 12
|
||||||
|
|
||||||
TILE_SIZE_PX = 64
|
TILE_SIZE_PX = 64
|
||||||
MAP_WIDTH_PX = MAP_WIDTH * TILE_SIZE_PX
|
MAP_WIDTH_PX = MAP_WIDTH * TILE_SIZE_PX
|
||||||
|
Loading…
Reference in New Issue
Block a user