Advertisement
SansPapyrus683

Python Snake Game

Nov 23rd, 2022
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | Source Code | 0 0
  1. import arcade
  2. from random import randint
  3.  
  4. SCREEN_WIDTH = 600
  5. SCREEN_HEIGHT = 600
  6. CELL_WIDTH = 30
  7.  
  8.  
  9. class Snake(arcade.Window):
  10.     def __init__(self, width, height):
  11.         super().__init__(width, height)
  12.         arcade.set_background_color(arcade.color.BLACK)
  13.         self.apple_pos = [SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + CELL_WIDTH * 3]
  14.         self.time = 0
  15.         self.dir = 0  # 0, 1, 2, 3 = up, down, left, right, respectively
  16.         self.snake = [[SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2]]
  17.  
  18.     def on_update(self, delta_time):
  19.         self.time += delta_time
  20.         if self.time < .2:
  21.             return
  22.  
  23.         next_pos = self.snake[0].copy()
  24.         if self.dir == 0:
  25.             next_pos[1] += CELL_WIDTH
  26.         elif self.dir == 1:
  27.             next_pos[1] -= CELL_WIDTH
  28.         elif self.dir == 2:
  29.             next_pos[0] -= CELL_WIDTH
  30.         elif self.dir == 3:
  31.             next_pos[0] += CELL_WIDTH
  32.  
  33.         if (next_pos in self.snake[1:] or
  34.                 not (0 <= next_pos[0] < SCREEN_WIDTH) or
  35.                 not (0 <= next_pos[1] < SCREEN_HEIGHT)):
  36.             arcade.close_window()
  37.  
  38.         self.snake.insert(0, next_pos)
  39.         if self.snake[0] == self.apple_pos:
  40.             self.apple_pos = [randint(0, SCREEN_WIDTH // CELL_WIDTH - 1) * CELL_WIDTH,
  41.                               randint(0, SCREEN_HEIGHT // CELL_WIDTH - 1) * CELL_WIDTH]
  42.             while self.apple_pos in self.snake:
  43.                 self.apple_pos = [randint(0, SCREEN_WIDTH // CELL_WIDTH - 1) * CELL_WIDTH,
  44.                                   randint(0, SCREEN_HEIGHT // CELL_WIDTH - 1) * CELL_WIDTH]
  45.         else:
  46.             self.snake.pop()
  47.  
  48.         self.time = 0
  49.  
  50.     def on_key_press(self, symbol, modifiers):
  51.         if symbol in [arcade.key.UP, arcade.key.W]:
  52.             self.dir = 0
  53.         elif symbol in [arcade.key.DOWN, arcade.key.S]:
  54.             self.dir = 1
  55.         elif symbol in [arcade.key.LEFT, arcade.key.A]:
  56.             self.dir = 2
  57.         elif symbol in [arcade.key.RIGHT, arcade.key.D]:
  58.             self.dir = 3
  59.  
  60.     def on_draw(self):
  61.         self.clear()
  62.         for i in self.snake:
  63.             arcade.draw_rectangle_filled(
  64.                 i[0] + CELL_WIDTH // 2, i[1] + CELL_WIDTH // 2,
  65.                 CELL_WIDTH, CELL_WIDTH,
  66.                 arcade.color.GREEN
  67.             )
  68.         arcade.draw_rectangle_filled(
  69.             self.apple_pos[0] + CELL_WIDTH // 2, self.apple_pos[1] + CELL_WIDTH // 2,
  70.             CELL_WIDTH, CELL_WIDTH,
  71.             arcade.color.RED
  72.         )
  73.  
  74.  
  75. Snake(SCREEN_WIDTH, SCREEN_HEIGHT)
  76. arcade.run()
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement