286 lines
11 KiB
Python
286 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 = []
|
|
self.allPath = []
|
|
|
|
|
|
def findWaiter(self, grid):
|
|
for y in range(10):
|
|
for x in range(10):
|
|
if grid[y][x].type =="waiter":
|
|
#print(x,y)
|
|
return [x, y]
|
|
|
|
|
|
def moveLeft(self, grid):
|
|
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
if positionX != 0:
|
|
collisionObject = grid[positionY][positionX - 1]
|
|
if collisionObject.type == "gridElement":
|
|
grid[positionY][positionX - 1].x += 50
|
|
grid[positionY][positionX].x -= 50
|
|
grid[positionY][positionX - 1], \
|
|
grid[positionY][positionX] = \
|
|
grid[positionY][positionX], \
|
|
grid[positionY][positionX - 1]
|
|
|
|
#print(self.positionX)
|
|
return grid
|
|
else:
|
|
pass
|
|
def moveRight(self, grid):
|
|
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
if positionX != 9:
|
|
collisionObject = grid[positionY][positionX + 1]
|
|
if collisionObject.type == "gridElement":
|
|
grid[positionY][positionX + 1].x -= 50
|
|
grid[positionY][positionX].x += 50
|
|
grid[positionY][positionX + 1], \
|
|
grid[positionY][positionX] = \
|
|
grid[positionY][positionX], \
|
|
grid[positionY][positionX + 1]
|
|
|
|
# print(self.positionX)
|
|
return grid
|
|
else:
|
|
pass
|
|
|
|
def moveUp(self, grid):
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
if positionY != 0:
|
|
collisionObject = grid[positionY - 1][positionX]
|
|
if collisionObject.type == "gridElement":
|
|
grid[positionY - 1][positionX].y += 50
|
|
grid[positionY][positionX].y -= 50
|
|
grid[positionY - 1][positionX], \
|
|
grid[positionY][positionX] = \
|
|
grid[positionY][positionX], \
|
|
grid[positionY - 1][positionX]
|
|
return grid
|
|
else:
|
|
pass
|
|
|
|
def moveDown(self, grid):
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
if positionY != 9:
|
|
collisionObject = grid[positionY + 1][positionX]
|
|
if collisionObject.type == "gridElement":
|
|
grid[positionY + 1][positionX].y -= 50
|
|
grid[positionY][positionX].y += 50
|
|
grid[positionY + 1][positionX], \
|
|
grid[positionY][positionX] = \
|
|
grid[positionY][positionX], \
|
|
grid[positionY + 1][positionX]
|
|
return grid
|
|
else:
|
|
pass
|
|
|
|
|
|
|
|
def move(self, game):
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
self.moveLeft(game.grid)
|
|
if keys[pygame.K_RIGHT]:
|
|
self.moveRight(game.grid)
|
|
if keys[pygame.K_UP]:
|
|
self.moveUp(game.grid)
|
|
if keys[pygame.K_DOWN]:
|
|
self.moveDown(game.grid)
|
|
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, grid):
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
|
|
if move == "Left":
|
|
if positionX == 0:
|
|
return False
|
|
if move == "Right":
|
|
if positionX == 9:
|
|
return False
|
|
if move == "Up":
|
|
if positionY == 0:
|
|
return False
|
|
if move == "Down":
|
|
if positionY == 9:
|
|
return False
|
|
return True
|
|
|
|
def checkPoss(self, grid, lastOperation):
|
|
|
|
stackMove = []
|
|
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
|
|
if len(lastOperation) == 0:
|
|
if self.isMoveInRange("Up", grid):
|
|
collisionObjectUp = grid[positionY - 1][positionX]
|
|
if collisionObjectUp.type == "gridElement":
|
|
stackMove.append("Up")
|
|
if self.isMoveInRange("Down", grid):
|
|
collisionObjectDown = grid[positionY + 1][positionX]
|
|
if collisionObjectDown.type == "gridElement":
|
|
stackMove.append("Down")
|
|
if self.isMoveInRange("Left", grid):
|
|
collisionObjectLeft = grid[positionY][positionX - 1]
|
|
if collisionObjectLeft.type == "gridElement":
|
|
stackMove.append("Left")
|
|
if self.isMoveInRange("Right", grid):
|
|
collisionObjectRight = grid[positionY][positionX + 1]
|
|
if collisionObjectRight.type == "gridElement":
|
|
stackMove.append("Right")
|
|
return stackMove
|
|
else:
|
|
last = lastOperation[-1]
|
|
if last == "Left":
|
|
if self.isMoveInRange("Up", grid):
|
|
collisionObjectUp = grid[positionY - 1][positionX]
|
|
if collisionObjectUp.type == "gridElement":
|
|
stackMove.append("Up")
|
|
if self.isMoveInRange("Down", grid):
|
|
collisionObjectDown = grid[positionY + 1][positionX]
|
|
if collisionObjectDown.type == "gridElement":
|
|
stackMove.append("Down")
|
|
if self.isMoveInRange("Left", grid):
|
|
collisionObjectLeft = grid[positionY][positionX - 1]
|
|
if collisionObjectLeft.type == "gridElement":
|
|
stackMove.append("Left")
|
|
|
|
return stackMove
|
|
|
|
if last == "Right":
|
|
if self.isMoveInRange("Up", grid):
|
|
collisionObjectUp = grid[positionY - 1][positionX]
|
|
if collisionObjectUp.type == "gridElement":
|
|
stackMove.append("Up")
|
|
if self.isMoveInRange("Down", grid):
|
|
collisionObjectDown = grid[positionY + 1][positionX]
|
|
if collisionObjectDown.type == "gridElement":
|
|
stackMove.append("Down")
|
|
|
|
if self.isMoveInRange("Right", grid):
|
|
collisionObjectRight = grid[positionY][positionX + 1]
|
|
if collisionObjectRight.type == "gridElement":
|
|
stackMove.append("Right")
|
|
return stackMove
|
|
|
|
if last == "Up":
|
|
if self.isMoveInRange("Up", grid):
|
|
collisionObjectUp = grid[positionY - 1][positionX]
|
|
if collisionObjectUp.type == "gridElement":
|
|
stackMove.append("Up")
|
|
|
|
if self.isMoveInRange("Left", grid):
|
|
collisionObjectLeft = grid[positionY][positionX - 1]
|
|
if collisionObjectLeft.type == "gridElement":
|
|
stackMove.append("Left")
|
|
if self.isMoveInRange("Right", grid):
|
|
collisionObjectRight = grid[positionY][positionX + 1]
|
|
if collisionObjectRight.type == "gridElement":
|
|
stackMove.append("Right")
|
|
return stackMove
|
|
|
|
if last == "Down":
|
|
|
|
if self.isMoveInRange("Down", grid):
|
|
collisionObjectDown = grid[positionY + 1][positionX]
|
|
if collisionObjectDown.type == "gridElement":
|
|
stackMove.append("Down")
|
|
if self.isMoveInRange("Left", grid):
|
|
collisionObjectLeft = grid[positionY][positionX - 1]
|
|
if collisionObjectLeft.type == "gridElement":
|
|
stackMove.append("Left")
|
|
if self.isMoveInRange("Right", grid):
|
|
collisionObjectRight = grid[positionY][positionX + 1]
|
|
if collisionObjectRight.type == "gridElement":
|
|
stackMove.append("Right")
|
|
return stackMove
|
|
|
|
|
|
def dfsFind(self, grid, currentOperation, idTable):
|
|
|
|
print("Sprawdzam czy stolik")
|
|
position = self.findWaiter(grid)
|
|
positionX = position[0]
|
|
positionY = position[1]
|
|
|
|
if self.isMoveInRange("Up", grid):
|
|
if grid[positionY - 1][positionX].type == "table":
|
|
if grid[positionY - 1][positionX].id == idTable:
|
|
self.allPath.append(currentOperation)
|
|
return 0
|
|
if self.isMoveInRange("Down", grid):
|
|
if grid[positionY + 1][positionX].type == "table":
|
|
if grid[positionY + 1][positionX].id == idTable:
|
|
self.allPath.append(currentOperation)
|
|
return 0
|
|
if self.isMoveInRange("Left", grid):
|
|
if grid[positionY][positionX - 1].type == "table":
|
|
if grid[positionY][positionX - 1].id == idTable:
|
|
self.allPath.append(currentOperation)
|
|
return 0
|
|
if self.isMoveInRange("Right", grid):
|
|
if grid[positionY][positionX + 1].type == "table":
|
|
if grid[positionY][positionX + 1].id == idTable:
|
|
self.allPath.append(currentOperation)
|
|
return 0
|
|
print("Sprawdzilem nie jest to stolik")
|
|
steps = []
|
|
steps.append(self.checkPoss(grid, currentOperation))
|
|
print("Naszymi mozliwosciami sa ", steps)
|
|
step = steps[-1]
|
|
print("Wybieram : ", step[-1])
|
|
if step[-1] == "Left":
|
|
self.moveLeft(grid)
|
|
currentOperation.append("Left")
|
|
if step[-1] == "Right":
|
|
self.moveRight(grid)
|
|
currentOperation.append("Right")
|
|
if step[-1] == "Up":
|
|
self.moveUp(grid)
|
|
currentOperation.append("Up")
|
|
if step[-1] == "Down":
|
|
self.moveDown(grid)
|
|
currentOperation.append("Down")
|
|
|
|
self.dfsFind(grid[:], currentOperation[:], idTable)
|