Advertisement
Guest User

Untitled

a guest
Mar 14th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import pygame
  2. from Direction import Direction
  3.  
  4.  
  5. class Snake(pygame.sprite.Sprite):
  6. def __init__(self):
  7. # head's original image
  8. self.original_image = pygame.image.load("images/head.png")
  9. # auxiliary image that will change when the direction changes
  10. self.image = pygame.transform.rotate(self.original_image, 0)
  11. # head coordinates
  12. self.rect = self.image.get_rect(center=(12*32-16, 9*32-16))
  13. # variables responsible for direction and those connected to it
  14. self.direction = Direction.UP
  15. self.new_direction = Direction.UP
  16.  
  17. def change_direction(self, direction):
  18. possible_change = True
  19. if direction == Direction.UP and self.direction == Direction.DOWN:
  20. possible_change = False
  21. if direction == Direction.DOWN and self.direction == Direction.UP:
  22. possible_change = False
  23. if direction == Direction.LEFT and self.direction == Direction.RIGHT:
  24. possible_change = False
  25. if direction == Direction.RIGHT and self.direction == Direction.LEFT:
  26. possible_change = False
  27. if possible_change:
  28. self.new_direction = direction
  29.  
  30. def update(self):
  31. self.direction = self.new_direction
  32. self.image = pygame.transform.rotate(
  33. self.original_image, (self.direction.value*-90))
  34.  
  35. if self.direction == Direction.UP:
  36. self.rect.move_ip(0, -32)
  37. if self.direction == Direction.RIGHT:
  38. self.rect.move_ip(32, 0)
  39. if self.direction == Direction.LEFT:
  40. self.rect.move_ip(-32, 0)
  41. if self.direction == Direction.DOWN:
  42. self.rect.move_ip(0, 32)
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement