Advertisement
Guest User

snake.py

a guest
Nov 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. import pygame,random
  2. pygame.init()
  3. gameDisplay= pygame.display.set_mode((800,600))
  4. surface = pygame.Surface(gameDisplay.get_size())
  5. surface = surface.convert()
  6. surface.fill((255,255,255))
  7. gameDisplay.blit(surface,(0,0))
  8. pygame.display.set_caption('Snake')
  9. clock = pygame.time.Clock()
  10.  
  11. RECTANGLE_SIZE = 10
  12. FPS = 24
  13. UP = [0,-1]
  14. DOWN = [0,1]
  15. LEFT = [-1,0]
  16. RIGHT = [1,0]
  17.  
  18. class Snake():
  19.     def __init__(self):
  20.         self.positions = [[400,200]]
  21.         self.lastMove = random.choice([UP,DOWN,LEFT,RIGHT])
  22.         self.length = 1
  23.    
  24.     def setDirection(self,direction):
  25.         if len(self.positions) > 1 and [direction[0] * -1, direction[1] * -1] == self.lastMove:
  26.             return
  27.         else:
  28.             self.lastMove = direction
  29.    
  30.     def move(self):
  31.         head = self.positions[0]
  32.         newHead = [(head[0] + self.lastMove[0]*RECTANGLE_SIZE) % 800, (head[1] + self.lastMove[1]*RECTANGLE_SIZE) % 600]
  33.  
  34.         if len(self.positions) > 2 and newHead in self.positions[2:]:
  35.             self.gameOver()
  36.         elif newHead[0] in [0,800] or newHead[1] in [0,600]:
  37.             self.gameOver()
  38.         else:
  39.             self.positions.insert(0,newHead)
  40.             if len(self.positions) > self.length:
  41.                 self.positions.pop()
  42.  
  43.  
  44.     def draw(self,surface):
  45.         for position in self.positions:
  46.             rectangle = pygame.Rect((position[0], position[1]), (RECTANGLE_SIZE, RECTANGLE_SIZE))
  47.             pygame.draw.rect(surface, (0,0,0), rectangle)
  48.  
  49.        
  50.     def gameOver(self):
  51.         self.positions = [[400,200]]
  52.         self.lastMove = random.choice([UP,DOWN,LEFT,RIGHT])
  53.         self.length = 1
  54.  
  55. class Food():
  56.     def __init__(self):
  57.         position_x = random.randint(1,(800/RECTANGLE_SIZE)-1) * RECTANGLE_SIZE
  58.         position_y = random.randint(1,(600/RECTANGLE_SIZE)-1) * RECTANGLE_SIZE
  59.         self.position = [position_x,position_y]
  60.  
  61.     def newPosition(self):
  62.         position_x = random.randint(1,(800/RECTANGLE_SIZE)-1) * RECTANGLE_SIZE
  63.         position_y = random.randint(1,(600/RECTANGLE_SIZE)-1) * RECTANGLE_SIZE
  64.         self.position = [position_x,position_y]
  65.  
  66.     def draw(self,surface):
  67.         rectangle = pygame.Rect((self.position[0], self.position[1]), (RECTANGLE_SIZE, RECTANGLE_SIZE))
  68.         pygame.draw.rect(surface, (255,0,0), rectangle)
  69.  
  70.     def hasBeenEaten(self, snake):
  71.         if self.position == snake.positions[0]:
  72.             snake.length += 1
  73.             self.newPosition()
  74.  
  75. snake = Snake()
  76. food = Food()
  77. running = True
  78. while running:
  79.  
  80.     for event in pygame.event.get():
  81.         if event.type == pygame.QUIT:
  82.             running = False
  83.         elif event.type == pygame.KEYDOWN:
  84.             if event.key == pygame.K_UP:
  85.                 snake.setDirection(UP)
  86.             elif event.key == pygame.K_DOWN:
  87.                 snake.setDirection(DOWN)
  88.             elif event.key == pygame.K_LEFT:
  89.                 snake.setDirection(LEFT)
  90.             elif event.key == pygame.K_RIGHT:
  91.                 snake.setDirection(RIGHT)
  92.  
  93.     surface.fill((255,255,255))
  94.     snake.move()
  95.     food.hasBeenEaten(snake)
  96.     snake.draw(surface)
  97.     food.draw(surface)
  98.     pygame.display.update()
  99.     gameDisplay.blit(surface, (0,0))
  100.     clock.tick(FPS)
  101.  
  102. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement