Advertisement
Korotkodul

snake_v2

Dec 4th, 2023 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. import pygame, sys, random
  2. from pygame.math import Vector2
  3.  
  4. class FRUIT:
  5.     def __init__(self):
  6.         #create x and y position
  7.         self.x = random.randint(0, cell_number - 1)
  8.         self.y = random.randint(0, cell_number - 1)
  9.         self.pos = Vector2(self.x, self.y)
  10.     def draw_fruit(self):
  11.         #create rectangle
  12.         fruit_rect = pygame.Rect(int(self.pos.x * cell_size), int(self.pos.y * cell_size), cell_size, cell_size)
  13.         #draw the rectangle
  14.         pygame.draw.rect(screen, (126, 166, 114), fruit_rect)
  15.  
  16. pygame.init()
  17. cell_size = 40
  18. cell_number = 20
  19. screen = pygame.display.set_mode((cell_number * cell_size, cell_number * cell_size))
  20. clock = pygame.time.Clock()
  21.  
  22. fruit = FRUIT()
  23.  
  24.  
  25. while True:
  26.     for event in pygame.event.get():
  27.         if event.type == pygame.QUIT:
  28.             pygame.quit()
  29.             sys.exit()
  30.     screen.fill((175, 250, 70))
  31.     fruit.draw_fruit()
  32.     pygame.display.update()
  33.  
  34.     clock.tick(60)
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement