[integrate_a_star_and_movement] added camera position fix

This commit is contained in:
czorekk 2022-04-28 21:28:03 +02:00
parent ffffa3a308
commit e6448b61cd
4 changed files with 16 additions and 10 deletions

View File

@ -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))
# print(f'ROT: {self.player.rot}')
# print("Agent pos: ", math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE))

View File

@ -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

View File

@ -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)

View File

@ -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)