Advertisement
OreganoHauch

Snake game v3

May 4th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import random
  4. from pygame.math import Vector2
  5.  
  6. class SNAKE:
  7. def __init__(self):
  8. self.body = [Vector2(5,10), Vector2(6,10), Vector2(7,10)]
  9. self.direction = Vector2(-1,0)
  10. self.new_block = False
  11.  
  12. def draw_snake(self):
  13. for block in self.body:
  14. x_pos = int(block.x * MAIN.cell_size)
  15. y_pos = int(block.y * MAIN.cell_size)
  16. block_rect = pygame.Rect(x_pos, y_pos, MAIN.cell_size, MAIN.cell_size)
  17. pygame.draw.rect(screen,(0,0,255),block_rect)
  18.  
  19. def move_snake(self):
  20. border_case = False
  21.  
  22. if self.body[0].x == 0 and MAIN.allow_borders and self.direction == Vector2(-1,0):
  23. border_case = True
  24. if self.new_block:
  25. self.body.insert(0, Vector2(MAIN.cell_number-1,self.body[0].y))
  26. self.new_block = False
  27. else:
  28. body_copy = self.body[:-1]
  29. body_copy.insert(0, Vector2(MAIN.cell_number-1,self.body[0].y))
  30. self.body = body_copy
  31. if self.body[0].x == MAIN.cell_number and MAIN.allow_borders and self.direction == Vector2(1,0):
  32. border_case = True
  33. if self.new_block:
  34. self.body.insert(0, Vector2(0,self.body[0].y))
  35. self.new_block = False
  36. else:
  37. body_copy = self.body[:-1]
  38. body_copy.insert(0, Vector2(0,self.body[0].y))
  39. self.body = body_copy
  40. if self.body[0].y == 0 and MAIN.allow_borders and self.direction == Vector2(0,-1):
  41. border_case = True
  42. if self.new_block:
  43. self.body.insert(0, Vector2(self.body[0].x,MAIN.cell_number-1))
  44. self.new_block = False
  45. else:
  46. body_copy = self.body[:-1]
  47. body_copy.insert(0, Vector2(self.body[0].x,MAIN.cell_number-1))
  48. self.body = body_copy
  49. if self.body[0].y == MAIN.cell_number and MAIN.allow_borders and self.direction == Vector2(0,1):
  50. border_case = True
  51. if self.new_block:
  52. self.body.insert(0, Vector2(self.body[0].x,0))
  53. self.new_block = False
  54. else:
  55. body_copy = self.body[:-1]
  56. body_copy.insert(0, Vector2(self.body[0].x,0))
  57. self.body = body_copy
  58. if not border_case:
  59. if self.new_block:
  60. self.body.insert(0, self.body[0] + self.direction)
  61. self.new_block = False
  62. else:
  63. body_copy = self.body[:-1]
  64. body_copy.insert(0, body_copy[0] + self.direction)
  65. self.body = body_copy
  66.  
  67. def add_block(self):
  68. self.new_block = True
  69.  
  70. class FRUIT:
  71. def __init__(self):
  72. self.x = random.randint(0,MAIN.cell_number - 1)
  73. self.y = random.randint(0,MAIN.cell_number - 1)
  74. self.pos = Vector2(self.x, self.y)
  75.  
  76. def draw_fruit(self):
  77. x_pos = int(self.pos.x * MAIN.cell_size)
  78. y_pos = int(self.pos.y * MAIN.cell_size)
  79. fruit_rect = pygame.Rect(x_pos, y_pos, MAIN.cell_size, MAIN.cell_size)
  80. screen.blit(apple,fruit_rect)
  81. #pygame.draw.rect(screen,(255,0,114),fruit_rect)
  82.  
  83. def randomize(self):
  84. self.x = random.randint(0,MAIN.cell_number - 1)
  85. self.y = random.randint(0,MAIN.cell_number - 1)
  86. self.pos = Vector2(self.x, self.y)
  87.  
  88. class MAIN:
  89.  
  90. # general game settings
  91. cell_size = 40
  92. cell_number = 20
  93. framerate = 60
  94. scrsize = cell_size * cell_number
  95. allow_borders = True
  96.  
  97. def __init__(self):
  98. self.fruit = FRUIT()
  99. self.snake = SNAKE()
  100.  
  101. def update(self):
  102. self.snake.move_snake()
  103. self.check_collision()
  104. self.check_fail()
  105.  
  106. def draw_elements(self):
  107. self.fruit.draw_fruit()
  108. self.snake.draw_snake()
  109.  
  110. def check_collision(self):
  111. if self.fruit.pos == self.snake.body[0]:
  112. self.fruit.randomize()
  113. self.snake.add_block()
  114.  
  115. def check_fail(self):
  116. if not MAIN.allow_borders:
  117. if not 0 <= self.snake.body[0].x <= MAIN.cell_number-1 or not 0 <= self.snake.body[0].y <= MAIN.cell_number-1:
  118. self.game_over()
  119. if self.snake.body[0] in self.snake.body[1:]:
  120. self.game_over()
  121.  
  122. def game_over(self):
  123. pygame.quit()
  124. sys.exit()
  125.  
  126. pygame.init()
  127.  
  128. screen = pygame.display.set_mode((MAIN.scrsize,MAIN.scrsize))
  129. clock = pygame.time.Clock()
  130. apple = pygame.image.load("Graphics/apple.png").convert_alpha()
  131. apple = pygame.transform.scale(apple, (MAIN.cell_size, MAIN.cell_size))
  132.  
  133. SCREEN_UPDATE = pygame.USEREVENT
  134. pygame.time.set_timer(SCREEN_UPDATE, 150)
  135.  
  136. main_game = MAIN()
  137.  
  138. while True:
  139. for event in pygame.event.get():
  140. if event.type == pygame.QUIT:
  141. pygame.quit()
  142. sys.exit()
  143. if event.type == SCREEN_UPDATE:
  144. main_game.update()
  145. if event.type == pygame.KEYDOWN:
  146. if event.key in [pygame.K_UP, pygame.K_w]: # GO UP
  147. if main_game.snake.direction.y != 1:
  148. main_game.snake.direction = Vector2(0,-1)
  149. if event.key in [pygame.K_DOWN, pygame.K_s]: # GO DOWN
  150. if main_game.snake.direction.y != -1:
  151. main_game.snake.direction = Vector2(0,1)
  152. if event.key in [pygame.K_LEFT, pygame.K_a]: # GO LEFT
  153. if main_game.snake.direction.x != 1:
  154. main_game.snake.direction = Vector2(-1,0)
  155. if event.key in [pygame.K_RIGHT, pygame.K_d]: # GO RIGHT
  156. if main_game.snake.direction.x != -1:
  157. main_game.snake.direction = Vector2(1,0)
  158.  
  159. screen.fill((175,215,70))
  160. main_game.draw_elements()
  161. pygame.display.update()
  162. clock.tick(MAIN.framerate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement