Zmiana akcji agenta - obrót prawo/lewo, ruch do przodu

This commit is contained in:
Sara Kowalska 2020-04-26 20:53:57 +02:00
parent ceafb6b12c
commit 86bd4e7973

View File

@ -210,6 +210,9 @@ class Waiter(object):
self.dirny = 1 self.dirny = 1
self.plates = [] #lista niesionych przez agenta talerzy, planowo lista par: (talerz, klient) self.plates = [] #lista niesionych przez agenta talerzy, planowo lista par: (talerz, klient)
self.direction = direction #kierunek, w ktory jest skierowany bot self.direction = direction #kierunek, w ktory jest skierowany bot
self.currentRotation = 0
self.rotate = "forward"
self.rotationNumber = 0
def moveRandomly(self, noWalkable): def moveRandomly(self, noWalkable):
rand = random.randrange(1, 5, 1) #losuje w zakresie 1-4 rand = random.randrange(1, 5, 1) #losuje w zakresie 1-4
@ -316,16 +319,29 @@ class Waiter(object):
plate = self.plates.pop(0) plate = self.plates.pop(0)
client.takePlateAndEat(plate) client.takePlateAndEat(plate)
def rotateRight(self):
self.currentRotation += 90
def rotateLeft(self):
self.currentRotation -= 90
def goForward(self, movex, movey):
self.posx += movex
self.posy += movey
self.pos = (self.posx, self.posy)
def draw(self, surface): def draw(self, surface):
image = pygame.image.load(r'waiter_right.png') image = pygame.image.load(r'waiter_right.png')
image = pygame.transform.scale(image, (sizeBetween - 1, sizeBetween - 1)) image = pygame.transform.scale(image, (sizeBetween - 1, sizeBetween - 1))
if self.direction == "right": if self.rotate == "right":
image = pygame.transform.rotate(image, 270) self.rotateRight()
elif self.direction == "left": elif self.rotate == "left":
image = pygame.transform.rotate(image, 90) self.rotateLeft()
elif self.direction == "up": elif self.direction == "forward":
image = pygame.transform.rotate(image, 180) pass
image = pygame.transform.rotate(image, self.currentRotation)
i = self.pos[0] i = self.pos[0]
j = self.pos[1] j = self.pos[1]
@ -338,19 +354,41 @@ class Waiter(object):
for i in range(lenght): for i in range(lenght):
movex = positionList[i + 1][0] - positionList[i][0] movex = positionList[i + 1][0] - positionList[i][0]
movey = positionList[i + 1][1] - positionList[i][1] movey = positionList[i + 1][1] - positionList[i][1]
self.posx += movex
self.posy += movey
self.pos = (self.posx, self.posy)
prevDirection = self.direction
if movex == -1 and movey == 0: if movex == -1 and movey == 0:
self.direction = "left" self.direction = 1 #"left"
elif movex == 1 and movey == 0: elif movex == 1 and movey == 0:
self.direction = "right" self.direction = 3 #"right"
elif movey == -1 and movex == 0: elif movey == 1 and movex == 0:
self.direction = "down" self.direction = 4 #"down"
else: else:
self.direction = "up" self.direction = 2 #"up"
howToRotate = prevDirection - self.direction #działa!
print(prevDirection)
print(self.direction)
print(howToRotate)
if howToRotate < 0:
self.rotate = "left"
self.rotationNumber = abs(howToRotate)
elif howToRotate > 0:
self.rotate = "right"
self.rotationNumber = abs(howToRotate)
elif howToRotate == 0:
self.rotate = "forward"
self.rotationNumber = 0
print(self.rotate)
for i in range(self.rotationNumber):
redrawWindow(window)
time.sleep(0.5)
self.rotate = "forward"
self.rotationNumber -= 0
self.goForward(movex, movey)
redrawWindow(window) redrawWindow(window)
time.sleep(0.5) time.sleep(0.5)
@ -398,7 +436,7 @@ def main():
rows = 15 rows = 15
sizeBetween = width // rows #wielkość pojedynczej kratki sizeBetween = width // rows #wielkość pojedynczej kratki
window = pygame.display.set_mode((width, width)) window = pygame.display.set_mode((width, width))
bot = Waiter((255, 0, 0), (12, 8), "right") bot = Waiter((255, 0, 0), (12, 8), 2)
tables = [] tables = []
tables.append(Table((0, 3), 1)) tables.append(Table((0, 3), 1))
@ -432,10 +470,10 @@ def main():
pygame.time.delay(100) pygame.time.delay(100)
clock.tick(60) clock.tick(60)
redrawWindow(window) redrawWindow(window)
#bot.moveRandomly(list) #bot.moveRandomly()
goal = (1, 5) goal = (1, 5)
bot.goByAStar(goal) bot.goByAStar(goal)
time.sleep(120) time.sleep(60)
flag = False flag = False