Advertisement
CodeCrusader

Python Snake Game

Apr 2nd, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | Gaming | 0 0
  1. import sys
  2. import pygame
  3. import random
  4.  
  5. # Screen dimensions
  6. WIDTH, HEIGHT = 640, 480
  7. CELL_SIZE = 20
  8.  
  9. # Colors
  10. WHITE = (255, 255, 255)
  11. BLACK = (0, 0, 0)
  12. GREEN = (0, 255, 0)
  13. RED = (255, 0, 0)
  14.  
  15.  
  16. class Snake:
  17.     def __init__(self):
  18.         self.segments = [(CELL_SIZE, CELL_SIZE), (CELL_SIZE * 2, CELL_SIZE)]
  19.         self.direction = (CELL_SIZE, 0)
  20.  
  21.     def move(self):
  22.         head = self.segments[0]
  23.         new_head = (head[0] + self.direction[0], head[1] + self.direction[1])
  24.         self.segments.insert(0, new_head)
  25.         self.segments.pop()
  26.  
  27.     def grow(self):
  28.         tail = self.segments[-1]
  29.         new_tail = (tail[0] - self.direction[0], tail[1] - self.direction[1])
  30.         self.segments.append(new_tail)
  31.  
  32.     def collide_with_itself(self):
  33.         return self.segments[0] in self.segments[1:]
  34.  
  35.     def collide_with_bounds(self):
  36.         head = self.segments[0]
  37.         return head[0] < 0 or head[0] >= WIDTH or head[1] < 0 or head[1] >= HEIGHT
  38.  
  39.  
  40. class Food:
  41.     def __init__(self):
  42.         self.position = self.random_position()
  43.  
  44.     def random_position(self):
  45.         x = random.randrange(0, WIDTH, CELL_SIZE)
  46.         y = random.randrange(0, HEIGHT, CELL_SIZE)
  47.         return (x, y)
  48.  
  49.  
  50. def main():
  51.     pygame.init()
  52.     screen = pygame.display.set_mode((WIDTH, HEIGHT))
  53.     pygame.display.set_caption("Snake Game")
  54.  
  55.     snake = Snake()
  56.     food = Food()
  57.  
  58.     clock = pygame.time.Clock()
  59.  
  60.     while True:
  61.         for event in pygame.event.get():
  62.             if event.type == pygame.QUIT:
  63.                 pygame.quit()
  64.                 sys.exit()
  65.  
  66.             if event.type == pygame.KEYDOWN:
  67.                 if event.key == pygame.K_UP:
  68.                     snake.direction = (0, -CELL_SIZE)
  69.                 elif event.key == pygame.K_DOWN:
  70.                     snake.direction = (0, CELL_SIZE)
  71.                 elif event.key == pygame.K_LEFT:
  72.                     snake.direction = (-CELL_SIZE, 0)
  73.                 elif event.key == pygame.K_RIGHT:
  74.                     snake.direction = (CELL_SIZE, 0)
  75.  
  76.         snake.move()
  77.  
  78.         if snake.collide_with_bounds() or snake.collide_with_itself():
  79.             snake = Snake()
  80.             food = Food()
  81.  
  82.         if snake.segments[0] == food.position:
  83.             snake.grow()
  84.             food.position = food.random_position()
  85.  
  86.         screen.fill(BLACK)
  87.  
  88.         for segment in snake.segments:
  89.             pygame.draw.rect(screen, GREEN, pygame.Rect(segment[0], segment[1], CELL_SIZE, CELL_SIZE))
  90.  
  91.         pygame.draw.rect(screen, RED, pygame.Rect(food.position[0], food.position[1], CELL_SIZE, CELL_SIZE))
  92.  
  93.         pygame.display.flip()
  94.         clock.tick(10)
  95.  
  96.  
  97. if __name__ == "__main__":
  98.     main()
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement