add automovement to actual master branch
This commit is contained in:
parent
e3eacaaa01
commit
300edcb889
47
game_objects/aiPlayer.py
Normal file
47
game_objects/aiPlayer.py
Normal 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()
|
@ -44,4 +44,9 @@ class Player(pg.sprite.Sprite):
|
|||||||
utils.collide_with_walls(self, self.game.wallTiles, 'x')
|
utils.collide_with_walls(self, self.game.wallTiles, 'x')
|
||||||
self.hit_rect.centery = self.pos.y
|
self.hit_rect.centery = self.pos.y
|
||||||
utils.collide_with_walls(self, self.game.wallTiles, '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
25
main.py
@ -4,15 +4,15 @@ import pygame as pg
|
|||||||
import sys
|
import sys
|
||||||
from os import path
|
from os import path
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from map import *
|
from map import *
|
||||||
from settings import *
|
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
|
||||||
|
|
||||||
|
|
||||||
|
from game_objects import aiPlayer
|
||||||
class Game():
|
class Game():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -22,8 +22,11 @@ class Game():
|
|||||||
pg.display.set_caption("Trashmaster")
|
pg.display.set_caption("Trashmaster")
|
||||||
self.load_data()
|
self.load_data()
|
||||||
self.init_game()
|
self.init_game()
|
||||||
|
# because dont work without data.txt
|
||||||
# self.init_bfs()
|
# self.init_bfs()
|
||||||
self.init_a_star()
|
# self.init_a_star()
|
||||||
|
|
||||||
|
self.dt = self.clock.tick(FPS) / 1000.0
|
||||||
|
|
||||||
def init_game(self):
|
def init_game(self):
|
||||||
# initialize all variables and do all the setup for a new game
|
# initialize all variables and do all the setup for a new game
|
||||||
@ -75,8 +78,12 @@ class Game():
|
|||||||
def run(self):
|
def run(self):
|
||||||
# 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
|
||||||
|
|
||||||
|
t = aiPlayer.aiPlayer(self.player, game=self)
|
||||||
|
t.startAiController()
|
||||||
|
|
||||||
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()
|
||||||
self.update()
|
self.update()
|
||||||
self.draw()
|
self.draw()
|
||||||
@ -129,8 +136,10 @@ class Game():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# create the game object
|
# create the game object
|
||||||
g = Game()
|
|
||||||
g.show_start_screen()
|
if __name__ == "__main__":
|
||||||
while True:
|
g = Game()
|
||||||
|
g.show_start_screen()
|
||||||
|
|
||||||
g.run()
|
g.run()
|
||||||
g.show_go_screen()
|
g.show_go_screen()
|
@ -12,6 +12,6 @@ def load_img(path):
|
|||||||
|
|
||||||
def get_pattern():
|
def get_pattern():
|
||||||
return {
|
return {
|
||||||
0: load_img(ROAD_DIR+"GTA2_TILE_257.bmp"),
|
0: load_img("resources/textures/road/GTA2_TILE_257.bmp"),
|
||||||
1: load_img(BUILDING_DIR+"GTA2_TILE_187.bmp"),
|
1: load_img("resources/textures/buliding/GTA2_TILE_187.bmp"),
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user