Sztuczna_Inteligencja/waiter.py

54 lines
2.3 KiB
Python
Raw Normal View History

2019-04-10 10:31:09 +02:00
import pygame
from pygame.math import Vector2
2019-04-16 13:28:11 +02:00
from gridElement import GridElement
2019-04-10 10:31:09 +02:00
2019-04-16 13:28:11 +02:00
#Klasa Waiter dziedziczy z klasy GridElement ale obiekt Waiter nie należy do listy grid
class Waiter(GridElement):
2019-04-10 10:31:09 +02:00
2019-04-16 13:28:11 +02:00
def __init__(self, x, y, game):
GridElement.__init__(self, x, y, game)
2019-04-14 18:33:30 +02:00
self.image = pygame.image.load("./Images/waiter.png").convert()
self.image.set_colorkey((0, 0, 0))
2019-04-10 10:31:09 +02:00
self.type = "waiter"
#Waiter może wejść tylko na pole które znajduje się na gridzie i ma atrybut type równy "path"
#metoda move dodaje poprzednią pozycję waitera do listy waiterMovesHistory
#aktualna pozycja waitara jest w obiekcie position typu Vector2 (dziedziczonym z GridElement)
2019-04-10 10:31:09 +02:00
def move(self, game):
keys = pygame.key.get_pressed()
lastPosition = Vector2(self.position.x, self.position.y)
2019-04-10 10:31:09 +02:00
if keys[pygame.K_LEFT]:
if int(self.position.x) != 0:
collisionObject = game.grid[int(self.position.y)][int(self.position.x) - 1]
if collisionObject.type == "path":
self.position.x -= 1
else:
pass
2019-04-10 10:31:09 +02:00
if keys[pygame.K_RIGHT]:
if int(self.position.x) != game.x - 1:
collisionObject = game.grid[int(self.position.y)][int(self.position.x) + 1]
if collisionObject.type == "path":
self.position.x += 1
else:
pass
2019-04-10 10:31:09 +02:00
if keys[pygame.K_UP]:
if int(self.position.y) != 0:
collisionObject = game.grid[int(self.position.y) - 1][int(self.position.x)]
if collisionObject.type == "path":
self.position.y -= 1
else:
pass
2019-04-10 10:31:09 +02:00
if keys[pygame.K_DOWN]:
if int(self.position.y) != game.y - 1:
collisionObject = game.grid[int(self.position.y) + 1][int(self.position.x)]
if collisionObject.type == "path":
self.position.y += 1
else:
pass
2019-04-10 10:31:09 +02:00
if keys[pygame.K_s]:
game.showGrid(game.grid)
if(lastPosition.x != self.position.x or lastPosition.y != self.position.y):
game.waiterMovesHistory.append(lastPosition)
print(game.waiterMovesHistory)