add automovement to actual master branch

This commit is contained in:
aliaksei-kudravets 2022-04-27 21:49:17 +02:00
parent e3eacaaa01
commit 300edcb889
4 changed files with 72 additions and 11 deletions

47
game_objects/aiPlayer.py Normal file
View File

@ -0,0 +1,47 @@
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 = ['right', 'straight', 'straight', 'left', 'straight'
]
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)

25
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,12 @@ class Game():
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
t = aiPlayer.aiPlayer(self.player, game=self)
t.startAiController()
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 +136,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"),
}