Merge pull request 'AM_actual_master' (#21) from AM_actual_master into master

Reviewed-on: #21
This commit is contained in:
Aliaksei Kudravets 2022-04-28 16:40:35 +02:00
commit 6d07658c47
5 changed files with 89 additions and 11 deletions

46
game_objects/aiPlayer.py Normal file
View File

@ -0,0 +1,46 @@
import pygame as pg
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':
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.angle)
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()
self.player.update()
self.game.draw()
print(self.player.get_actual_coords())
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 == 'straight':
self.moveAiPlayer()
print(f'ROT IS {self.player.rot}')
if action == 'right':
self.turn_right()
if action == 'left':
self.turn_left()

View File

@ -44,4 +44,9 @@ class Player(pg.sprite.Sprite):
utils.collide_with_walls(self, self.game.wallTiles, 'x')
self.hit_rect.centery = self.pos.y
utils.collide_with_walls(self, self.game.wallTiles, 'y')
self.rect.center = self.hit_rect.center
self.rect.center = self.hit_rect.center
def get_actual_coords(self):
# return (self.rect.x / 64, self.rect.y / 64)
return (self.rect.x, self.rect.y)

27
main.py
View File

@ -4,15 +4,15 @@ import pygame as pg
import sys
from os import path
import math
from map import *
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
class Game():
def __init__(self):
@ -22,8 +22,11 @@ class Game():
pg.display.set_caption("Trashmaster")
self.load_data()
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
def init_game(self):
# initialize all variables and do all the setup for a new game
@ -75,8 +78,14 @@ class Game():
def run(self):
# 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.dt = self.clock.tick(FPS) / 1000.0
self.events()
self.update()
self.draw()
@ -129,8 +138,10 @@ class Game():
pass
# create the game object
g = Game()
g.show_start_screen()
while True:
if __name__ == "__main__":
g = Game()
g.show_start_screen()
g.run()
g.show_go_screen()
g.show_go_screen()

View File

@ -12,6 +12,6 @@ def load_img(path):
def get_pattern():
return {
0: load_img(ROAD_DIR+"GTA2_TILE_257.bmp"),
1: load_img(BUILDING_DIR+"GTA2_TILE_187.bmp"),
0: load_img("resources/textures/road/GTA2_TILE_257.bmp"),
1: load_img("resources/textures/buliding/GTA2_TILE_187.bmp"),
}

View File

@ -1 +1,17 @@
astroid==2.11.2
autopep8==1.6.0
dill==0.3.4
flake8==4.0.1
isort==5.10.1
lazy-object-proxy==1.7.1
mccabe==0.6.1
platformdirs==2.5.1
pycodestyle==2.8.0
pyflakes==2.4.0
pygame==2.1.2
pylint==2.13.5
PyTMX==3.31
toml==0.10.2
tomli==2.0.1
typing_extensions==4.1.1
wrapt==1.14.0