252 lines
11 KiB
Python
252 lines
11 KiB
Python
import pygame
|
|
import time
|
|
from pygame.math import Vector2
|
|
|
|
class Waiter(object):
|
|
|
|
def __init__(self, game, x, y):
|
|
self.game = game
|
|
game.idItem += 1
|
|
self.size= self.game.screen.get_size()
|
|
self.x = x
|
|
self.y = y
|
|
self.positionX = int(x / 50)
|
|
self.positionY = int(y / 50)
|
|
self.image = pygame.image.load("./Images/waiter.png").convert()
|
|
self.image.set_colorkey((255, 255, 255))
|
|
self.type = "waiter"
|
|
self.lastStep = []
|
|
|
|
|
|
def moveLeft(self, game):
|
|
if self.positionX != 0:
|
|
collisionObject = game.grid[self.positionY][self.positionX - 1]
|
|
if collisionObject.type == "gridElement":
|
|
game.grid[self.positionY][self.positionX - 1].x += 50
|
|
game.grid[self.positionY][self.positionX].x -= 50
|
|
game.grid[self.positionY][self.positionX - 1], \
|
|
game.grid[self.positionY][self.positionX] = \
|
|
game.grid[self.positionY][self.positionX], \
|
|
game.grid[self.positionY][self.positionX - 1]
|
|
self.positionX -= 1
|
|
#print(self.positionX)
|
|
self.lastStep.append("Left")
|
|
else:
|
|
pass
|
|
def moveRight(self, game):
|
|
if self.positionX != game.x-1:
|
|
collisionObject = game.grid[self.positionY][self.positionX + 1]
|
|
if collisionObject.type == "gridElement":
|
|
game.grid[self.positionY][self.positionX + 1].x -= 50
|
|
game.grid[self.positionY][self.positionX].x += 50
|
|
game.grid[self.positionY][self.positionX + 1], \
|
|
game.grid[self.positionY][self.positionX] = \
|
|
game.grid[self.positionY][self.positionX], \
|
|
game.grid[self.positionY][self.positionX + 1]
|
|
self.positionX += 1
|
|
#print(self.positionX)
|
|
self.lastStep.append("Right")
|
|
else:
|
|
pass
|
|
|
|
def moveUp(self, game):
|
|
if self.positionY != 0:
|
|
collisionObject = game.grid[self.positionY - 1][self.positionX]
|
|
if collisionObject.type == "gridElement":
|
|
game.grid[self.positionY - 1][self.positionX].y += 50
|
|
game.grid[self.positionY][self.positionX].y -= 50
|
|
game.grid[self.positionY - 1][self.positionX], \
|
|
game.grid[self.positionY][self.positionX] = \
|
|
game.grid[self.positionY][self.positionX], \
|
|
game.grid[self.positionY - 1][self.positionX]
|
|
self.positionY -= 1
|
|
self.lastStep.append("Up")
|
|
else:
|
|
pass
|
|
|
|
def moveDown(self, game):
|
|
if self.positionY != game.y-1:
|
|
collisionObject = game.grid[self.positionY + 1][self.positionX]
|
|
if collisionObject.type == "gridElement":
|
|
game.grid[self.positionY + 1][self.positionX].y -= 50
|
|
game.grid[self.positionY][self.positionX].y += 50
|
|
game.grid[self.positionY + 1][self.positionX], \
|
|
game.grid[self.positionY][self.positionX] = \
|
|
game.grid[self.positionY][self.positionX], \
|
|
game.grid[self.positionY + 1][self.positionX]
|
|
self.positionY += 1
|
|
self.lastStep.append("Down")
|
|
else:
|
|
pass
|
|
|
|
|
|
|
|
def move(self, game):
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
self.moveLeft(game)
|
|
if keys[pygame.K_RIGHT]:
|
|
self.moveRight(game)
|
|
if keys[pygame.K_UP]:
|
|
self.moveUp(game)
|
|
if keys[pygame.K_DOWN]:
|
|
self.moveDown(game)
|
|
if keys[pygame.K_s]:
|
|
game.showGrid(game.grid)
|
|
|
|
def draw(self):
|
|
self.rect1 = pygame.Rect(self.x, self.y, 50, 50)
|
|
pygame.draw.rect(self.game.screen, (0, 150, 255), self.rect1)
|
|
#print(self.lastStep)
|
|
self.game.screen.blit(self.image, (self.x, self.y))
|
|
|
|
|
|
def isMoveInRange(self, move):
|
|
if move == "Left":
|
|
if self.positionX == 0:
|
|
return False
|
|
if move == "Right":
|
|
if self.positionX == 9:
|
|
return False
|
|
if move == "Up":
|
|
if self.positionY == 0:
|
|
return False
|
|
if move == "Down":
|
|
if self.positionY == 9:
|
|
return False
|
|
return True
|
|
|
|
def checkPoss(self, game, lastOperation):
|
|
self.grid = game.grid
|
|
self.lastOperation = lastOperation
|
|
self.stackMove = []
|
|
|
|
if len(self.lastOperation) == 0:
|
|
if self.isMoveInRange("Up"):
|
|
self.collisionObjectUp = self.grid[self.positionY - 1][self.positionX]
|
|
if self.collisionObjectUp.type == "gridElement":
|
|
self.stackMove.append("Up")
|
|
if self.isMoveInRange("Down"):
|
|
self.collisionObjectDown = self.grid[self.positionY + 1][self.positionX]
|
|
if self.collisionObjectDown.type == "gridElement":
|
|
self.stackMove.append("Down")
|
|
if self.isMoveInRange("Left"):
|
|
self.collisionObjectLeft = self.grid[self.positionY][self.positionX - 1]
|
|
if self.collisionObjectLeft.type == "gridElement":
|
|
self.stackMove.append("Left")
|
|
if self.isMoveInRange("Right"):
|
|
self.collisionObjectRight = self.grid[self.positionY][self.positionX + 1]
|
|
if self.collisionObjectRight.type == "gridElement":
|
|
self.stackMove.append("Right")
|
|
return self.stackMove
|
|
else:
|
|
self.last = self.lastOperation[-1]
|
|
if self.last == "Left":
|
|
if self.isMoveInRange("Up"):
|
|
self.collisionObjectUp = self.grid[self.positionY - 1][self.positionX]
|
|
if self.collisionObjectUp.type == "gridElement":
|
|
self.stackMove.append("Up")
|
|
if self.isMoveInRange("Down"):
|
|
self.collisionObjectDown = self.grid[self.positionY + 1][self.positionX]
|
|
if self.collisionObjectDown.type == "gridElement":
|
|
self.stackMove.append("Down")
|
|
if self.isMoveInRange("Left"):
|
|
self.collisionObjectLeft = self.grid[self.positionY][self.positionX - 1]
|
|
if self.collisionObjectLeft.type == "gridElement":
|
|
self.stackMove.append("Left")
|
|
|
|
return self.stackMove
|
|
|
|
if self.last == "Right":
|
|
if self.isMoveInRange("Up"):
|
|
self.collisionObjectUp = self.grid[self.positionY - 1][self.positionX]
|
|
if self.collisionObjectUp.type == "gridElement":
|
|
self.stackMove.append("Up")
|
|
if self.isMoveInRange("Down"):
|
|
self.collisionObjectDown = self.grid[self.positionY + 1][self.positionX]
|
|
if self.collisionObjectDown.type == "gridElement":
|
|
self.stackMove.append("Down")
|
|
|
|
if self.isMoveInRange("Right"):
|
|
self.collisionObjectRight = self.grid[self.positionY][self.positionX + 1]
|
|
if self.collisionObjectRight.type == "gridElement":
|
|
self.stackMove.append("Right")
|
|
return self.stackMove
|
|
|
|
if self.last == "Up":
|
|
if self.isMoveInRange("Up"):
|
|
self.collisionObjectUp = self.grid[self.positionY - 1][self.positionX]
|
|
if self.collisionObjectUp.type == "gridElement":
|
|
self.stackMove.append("Up")
|
|
|
|
if self.isMoveInRange("Left"):
|
|
self.collisionObjectLeft = self.grid[self.positionY][self.positionX - 1]
|
|
if self.collisionObjectLeft.type == "gridElement":
|
|
self.stackMove.append("Left")
|
|
if self.isMoveInRange("Right"):
|
|
self.collisionObjectRight = self.grid[self.positionY][self.positionX + 1]
|
|
if self.collisionObjectRight.type == "gridElement":
|
|
self.stackMove.append("Right")
|
|
return self.stackMove
|
|
|
|
if self.last == "Down":
|
|
|
|
if self.isMoveInRange("Down"):
|
|
self.collisionObjectDown = self.grid[self.positionY + 1][self.positionX]
|
|
if self.collisionObjectDown.type == "gridElement":
|
|
self.stackMove.append("Down")
|
|
if self.isMoveInRange("Left"):
|
|
self.collisionObjectLeft = self.grid[self.positionY][self.positionX - 1]
|
|
if self.collisionObjectLeft.type == "gridElement":
|
|
self.stackMove.append("Left")
|
|
if self.isMoveInRange("Right"):
|
|
self.collisionObjectRight = self.grid[self.positionY][self.positionX + 1]
|
|
if self.collisionObjectRight.type == "gridElement":
|
|
self.stackMove.append("Right")
|
|
return self.stackMove
|
|
|
|
|
|
def dfsFind(self, game, currentOperation, idTable):
|
|
self.game = game
|
|
self.grid = game.grid
|
|
self.currentOperation = currentOperation
|
|
print("Sprawdzam czy stolik")
|
|
if self.isMoveInRange("Up"):
|
|
if self.grid[self.positionY - 1][self.positionX].type == "table":
|
|
if self.grid[self.positionY - 1][self.positionX].id == idTable:
|
|
return self.currentOperation
|
|
if self.isMoveInRange("Down"):
|
|
if self.grid[self.positionY + 1][self.positionX].type == "table":
|
|
if self.grid[self.positionY + 1][self.positionX].id == idTable:
|
|
return self.currentOperation
|
|
if self.isMoveInRange("Left"):
|
|
if self.grid[self.positionY][self.positionX - 1].type == "table":
|
|
if self.grid[self.positionY][self.positionX - 1].id == idTable:
|
|
return self.currentOperation
|
|
if self.isMoveInRange("Right"):
|
|
if self.grid[self.positionY][self.positionX + 1].type == "table":
|
|
if self.grid[self.positionY][self.positionX + 1].id == idTable:
|
|
return self.currentOperation
|
|
print("Sprawdzilem nie stolik")
|
|
steps = []
|
|
steps.append(self.checkPoss(self.game, self.currentOperation))
|
|
step = steps[-1]
|
|
print(step[-1])
|
|
if step[-1] == "Left":
|
|
self.moveLeft(self.game)
|
|
self.currentOperation.append("Left")
|
|
if step[-1] == "Right":
|
|
self.moveRight(self.game)
|
|
self.currentOperation.append("Right")
|
|
if step[-1] == "Up":
|
|
self.moveUp(self.game)
|
|
self.currentOperation.append("Up")
|
|
if step[-1] == "Down":
|
|
self.moveDown(self.game)
|
|
self.currentOperation.append("Down")
|
|
game.draw()
|
|
time.sleep(.500)
|
|
pygame.display.flip()
|
|
self.dfsFind(self.game, self.currentOperation, idTable)
|