Add moving based on directions

This commit is contained in:
s450026 2020-04-26 18:26:55 +02:00
parent 74dd8fac0b
commit 84abac95e7
11 changed files with 43 additions and 9 deletions

View File

@ -10,10 +10,11 @@ if __name__ == "__main__":
fps = 40
graphics = Graphics()
waiter = Waiter(graphics)
direction = 0
# init functions
graphics.drawBackground(waiter.matrix)
graphics.update(waiter.X, waiter.Y)
graphics.update(waiter.X, waiter.Y, waiter.direction)
while True:
for event in pygame.event.get():
@ -29,7 +30,7 @@ if __name__ == "__main__":
break
graphics.clear(waiter.X, waiter.Y)
waiter.update(event, graphics)
graphics.update(waiter.X, waiter.Y)
waiter.update(event, graphics, direction)
graphics.update(waiter.X, waiter.Y, waiter.direction)
pygame.display.flip()
clock.tick(fps)

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -10,6 +10,10 @@ class Graphics:
'bar_floor': pygame.image.load('../resources/images/waiter.png'), #
'table': pygame.image.load('../resources/images/table3.png'),
'waiter': pygame.image.load('../resources/images/waiter.png'),
'waiter-down': pygame.image.load('../resources/images/waiter-down.png'),
'waiter-left': pygame.image.load('../resources/images/waiter-left.png'),
'waiter-right': pygame.image.load('../resources/images/waiter-right.png'),
'waiter-up': pygame.image.load('../resources/images/waiter-up.png'),
'table2': pygame.image.load('../resources/images/table2.png'),
'table3': pygame.image.load('../resources/images/table3.png'),
'chair': pygame.image.load('../resources/images/waiter.png'),
@ -35,5 +39,13 @@ class Graphics:
def clear(self, x, y):
self.screen.blit(self.image['floor'], (x * self.block_size, y * self.block_size))
def update(self, x, y):
def update(self, x, y, direction):
if direction == 1:
self.screen.blit(self.image['waiter-up'], (x * self.block_size, y * self.block_size))
if direction == 2:
self.screen.blit(self.image['waiter-right'], (x * self.block_size, y * self.block_size))
if direction == 3:
self.screen.blit(self.image['waiter-down'], (x * self.block_size, y * self.block_size))
if direction == 4:
self.screen.blit(self.image['waiter-left'], (x * self.block_size, y * self.block_size))
self.screen.blit(self.image['waiter'], (x * self.block_size, y * self.block_size))

View File

@ -9,6 +9,7 @@ class Waiter(pygame.sprite.Sprite):
pygame.sprite.Sprite.__init__(self)
self.X = 0
self.Y = 0
self.direction = 0
self.frame = 0
self.matrix = Matrix(graphics=graphics)
@ -19,14 +20,34 @@ class Waiter(pygame.sprite.Sprite):
self.X += x
self.Y += y
def update(self, event, graphics):
def arrow(self, direction):
self.direction = direction
def update(self, event, graphics, direction):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move(-1, 0, graphics)
self.arrow(4)
#self.move(-1, 0, graphics)
if event.key == pygame.K_RIGHT:
self.move(1, 0, graphics)
self.arrow(2)
#self.move(1, 0, graphics)
if event.key == pygame.K_UP:
self.move(0, -1, graphics)
self.arrow(1)
#self.move(0, -1, graphics)
if event.key == pygame.K_DOWN:
self.arrow(3)
#self.move(0, 1, graphics)
if event.key == pygame.K_SPACE:
#up
if self.direction == 1:
self.move(0, -1, graphics)
#righ
if self.direction == 2:
self.move(1, 0, graphics)
#down
if self.direction == 3:
self.move(0, 1, graphics)
#left
if self.direction == 4:
self.move(-1, 0, graphics)
print(self.X, self.Y)