coding_giants

lesson 10 Snake

Mar 15th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import copy
  3. from Direction import Direction
  4. from Segment import Segment
  5.  
  6.  
  7. class Snake(pygame.sprite.Sprite):
  8. def __init__(self):
  9. # head's original image
  10. self.original_image = pygame.image.load("images/head.png")
  11. # auxiliary image that will change when the direction changes
  12. self.image = pygame.transform.rotate(self.original_image, 0)
  13. # head coordinates
  14. self.rect = self.image.get_rect(center=(12*32+16, 9*32+16))
  15. # variables responsible for direction and those connected to it
  16. self.direction = Direction.UP
  17. self.new_direction = Direction.UP
  18. # last saved snake's position, we'll use it to update the segments
  19. self.last_position = self.rect
  20. # information about adding new segment and their list
  21. self.add_segment = False
  22. self.segments = []
  23.  
  24. def change_direction(self, direction):
  25. possible_change = True
  26. if direction == Direction.UP and self.direction == Direction.DOWN:
  27. possible_change = False
  28. if direction == Direction.DOWN and self.direction == Direction.UP:
  29. possible_change = False
  30. if direction == Direction.LEFT and self.direction == Direction.RIGHT:
  31. possible_change = False
  32. if direction == Direction.RIGHT and self.direction == Direction.LEFT:
  33. possible_change = False
  34. if possible_change:
  35. self.new_direction = direction
  36.  
  37. def collision_check(self):
  38. # biting the tail
  39. for segment in self.segments:
  40. if self.rect.topleft == segment.position.topleft:
  41. return True
  42.  
  43. # moving outside of the display
  44. if self.rect.top < 0 or self.rect.top >= 608:
  45. return True
  46. if self.rect.left < 0 or self.rect.left >= 800:
  47. return True
  48.  
  49. return False
  50.  
  51. def update(self):
  52. self.direction = self.new_direction
  53. self.image = pygame.transform.rotate(
  54. self.original_image, (self.direction.value*-90))
  55.  
  56. self.last_position = copy.deepcopy(self.rect)
  57. if self.direction == Direction.UP:
  58. self.rect.move_ip(0, -32)
  59. if self.direction == Direction.RIGHT:
  60. self.rect.move_ip(32, 0)
  61. if self.direction == Direction.LEFT:
  62. self.rect.move_ip(-32, 0)
  63. if self.direction == Direction.DOWN:
  64. self.rect.move_ip(0, 32)
  65.  
  66. # moving segments
  67. for i in range(len(self.segments)):
  68. if i == 0:
  69. self.segments[i].shift(self.last_position)
  70. else:
  71. self.segments[i].shift(self.segments[i-1].last_position)
  72.  
  73. # adding a new segment
  74. if self.add_segment:
  75. new_segment = Segment()
  76.  
  77. new_position = None
  78. if len(self.segments) > 0:
  79. new_position = copy.deepcopy(self.segments[-1].position)
  80. else:
  81. new_position = copy.deepcopy(self.last_position)
  82. new_segment.position = new_position
  83. self.segments.append(new_segment)
  84. self.add_segment = False
  85.  
  86. def draw_segments(self, display):
  87. for segment in self.segments:
  88. display.blit(segment.image, segment.position)
  89.  
  90. def eat_apple(self):
  91. self.add_segment = True
  92.  
Add Comment
Please, Sign In to add comment