Advertisement
Korotkodul

snake_v4

Dec 9th, 2023 (edited)
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. import pygame, sys, random
  2. from pygame.math import Vector2
  3.  
  4. class SNAKE:
  5.     def __init__(self):
  6.         self.body = [Vector2(5,10), Vector2(6,10), Vector2(7,10)]
  7.         self.direction = Vector2(-1, 0)
  8.         self.new_block = False
  9.  
  10.     def draw_snake(self):
  11.         for block in self.body:
  12.             #create a rect
  13.             x_pos = int(block.x * cell_size)
  14.             y_pos = int(block.y * cell_size)
  15.             block_rect = pygame.Rect(x_pos, y_pos,cell_size,cell_size)
  16.             #draw the rectangle
  17.             pygame.draw.rect(screen, (255, 0, 0), block_rect)
  18.  
  19.     def move_snake(self):
  20.         if self.new_block == False:
  21.             body_copy = self.body[:-1]
  22.             body_copy.insert(0, body_copy[0] + self.direction)
  23.             self.body = body_copy
  24.         else:
  25.             body_copy = self.body[:]
  26.             body_copy.insert(0, body_copy[0] + self.direction)
  27.             self.body = body_copy
  28.             self.new_block = False
  29.  
  30.     def add_block(self):
  31.         self.new_block = True
  32.  
  33. class FRUIT:
  34.     def __init__(self):
  35.         #create x and y position
  36.         self.x = random.randint(0, cell_number - 1)
  37.         self.y = random.randint(0, cell_number - 1)
  38.         self.pos = Vector2(self.x, self.y)
  39.     def draw_fruit(self):
  40.         #create rectangle
  41.         fruit_rect = pygame.Rect(int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
  42.         #draw the rectangle
  43.         pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
  44.     def randomize(self):
  45.         self.x = random.randint(0, cell_number - 1)
  46.         self.y = random.randint(0, cell_number - 1)
  47.         self.pos = Vector2(self.x, self.y)
  48.  
  49. class MAIN:
  50.     def __init__(self):
  51.         self.snake = SNAKE()
  52.         self.fruit = FRUIT()
  53.     def update(self):
  54.         self.snake.move_snake()
  55.         self.check_collision()
  56.         self.check_fail()
  57.     def draw_elements(self):
  58.         self.snake.draw_snake()
  59.         self.fruit.draw_fruit()
  60.     def check_collision(self):
  61.         if self.fruit.pos == self.snake.body[0]:
  62.             print('snack')
  63.             #reposition fruit
  64.             self.fruit.randomize()
  65.             #add a head
  66.             self.snake.add_block()
  67.     def check_fail(self):
  68.         if not 0 <= self.snake.body[0].x < cell_number:
  69.             self.game_over()
  70.         if not 0 <= self.snake.body[0].y < cell_number:
  71.             self.game_over()
  72.         for block in self.snake.body[1:]:
  73.             if block == self.snake.body[0]:
  74.                 self.game_over()
  75.     def game_over(self):
  76.         pygame.quit()
  77.         sys.exit()
  78. pygame.init()
  79. cell_size = 40
  80. cell_number = 20
  81. screen = pygame.display.set_mode((cell_number * cell_size, cell_number * cell_size))
  82. clock = pygame.time.Clock()
  83.  
  84.  
  85.  
  86. SCREEN_UPDATE = pygame.USEREVENT
  87. pygame.time.set_timer(SCREEN_UPDATE, 150)
  88.  
  89. main_game = MAIN()
  90.  
  91. while True:
  92.     for event in pygame.event.get():
  93.         if event.type == pygame.QUIT:
  94.             pygame.quit()
  95.             sys.exit()
  96.         if event.type == SCREEN_UPDATE:
  97.             main_game.update()
  98.         if event.type == pygame.KEYDOWN:
  99.             if event.key == pygame.K_UP:
  100.                 if main_game.snake.direction.y != 1:
  101.                     main_game.snake.direction = Vector2(0, -1)
  102.             elif event.key == pygame.K_DOWN:
  103.                 if main_game.snake.direction.y != -1:
  104.                     main_game.snake.direction = Vector2(0, 1)
  105.             elif event.key == pygame.K_RIGHT:
  106.                 if main_game.snake.direction.x != -1:
  107.                     main_game.snake.direction = Vector2(1, 0)
  108.             elif event.key == pygame.K_LEFT:
  109.                 if main_game.snake.direction.x != 1:
  110.                     main_game.snake.direction = Vector2(-1, 0)
  111.  
  112.     screen.fill((175, 250, 70))
  113.     #main_game.fruit.draw_fruit()
  114.     #main_game.snake.draw_snake()
  115.     main_game.draw_elements()
  116.     pygame.display.update()
  117.  
  118.     clock.tick(60)
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement