Indie_Coder

Snake Tutorial - Part 1

Aug 6th, 2022
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import pygame as pg
  2. from pygame.math import Vector2
  3.  
  4. WIDTH = 500
  5. FPS = 60
  6. GRID = 20
  7. CELLSIZE = WIDTH / GRID
  8.  
  9. RIGHT = Vector2(1, 0)
  10. LEFT = Vector2(-1, 0)
  11. UP = Vector2(0, -1)
  12. DOWN = Vector2(0, 1)
  13.  
  14. class Game:
  15.     def __init__(self):
  16.         pg.init()
  17.         self.screen = pg.display.set_mode((WIDTH, WIDTH))
  18.         self.clock = pg.time.Clock()
  19.         pg.display.set_caption('Little Python')
  20.  
  21.         self.apple = Apple(self.screen, 3, 5)
  22.         self.snake = Snake(self.screen)
  23.         self.snake_timer = 500
  24.         self.snake_clk = 0
  25.  
  26.     def draw(self):
  27.         self.screen.fill('light green')
  28.         [pg.draw.rect(self.screen, 'black', (x * CELLSIZE, y * CELLSIZE, CELLSIZE, CELLSIZE), 1) for y in range(GRID) for x in range(GRID)]
  29.         self.apple.draw()
  30.         self.snake.draw()
  31.  
  32.     def control(self):
  33.         keys = pg.key.get_pressed()
  34.         if keys[pg.K_UP]:
  35.             self.snake.change_direction(UP)
  36.         elif keys[pg.K_DOWN]:
  37.             self.snake.change_direction(DOWN)
  38.         elif keys[pg.K_LEFT]:
  39.             self.snake.change_direction(LEFT)
  40.         elif keys[pg.K_RIGHT]:
  41.             self.snake.change_direction(RIGHT)
  42.  
  43.     def update(self):
  44.         pg.display.update()
  45.         self.clock.tick(FPS)
  46.         now = pg.time.get_ticks()
  47.         if now - self.snake_clk > self.snake_timer:
  48.             self.snake_clk = now
  49.             self.snake.move()
  50.  
  51. class Snake:
  52.     def __init__(self, screen):
  53.         self.screen = screen
  54.         self.body = [Vector2(10, 10)]
  55.         self.color = 'blue'
  56.         self.direction = RIGHT
  57.  
  58.     def _get_segment(self, index):
  59.         rect = pg.Rect(self.body[index].x*CELLSIZE, self.body[index].y*CELLSIZE, CELLSIZE, CELLSIZE)
  60.         return rect
  61.  
  62.     def draw(self):
  63.         for i in range(len(self.body)):
  64.             rect = self._get_segment(i)
  65.             pg.draw.rect(self.screen, self.color, rect)
  66.  
  67.     def move(self):
  68.         self.body[0] += self.direction
  69.  
  70.     def change_direction(self, new_dir):
  71.         if new_dir.x == self.direction.x or new_dir.y == self.direction.y:
  72.             return
  73.         self.direction = new_dir
  74.  
  75.  
  76. class Apple:
  77.     def __init__(self, screen, x, y):
  78.         self.screen = screen
  79.         self.x = x
  80.         self.y = y
  81.  
  82.     def draw(self):
  83.         rect = pg.Rect(self.x * CELLSIZE, self.y * CELLSIZE, CELLSIZE, CELLSIZE)
  84.         pg.draw.rect(self.screen, 'red', rect)
  85.  
  86.  
  87. def main():
  88.     game = Game()
  89.     running = True
  90.     while running:
  91.         for event in pg.event.get():
  92.             if event.type == pg.QUIT:
  93.                 running = False
  94.         game.control()
  95.         game.draw()
  96.         game.update()
  97.     pg.quit()
  98.  
  99.  
  100. if __name__ == '__main__':
  101.     main()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment