overactive

fdsd

Jul 23rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4.  
  5. snakeColor = (255,255,255)
  6. foodColor = (255,255,0)
  7. width = 600
  8. height = 600
  9. grid = 20
  10. direction = 'up'
  11.  
  12. openWindow = True
  13.  
  14. canvas = pygame.display.set_mode((width,height))
  15. pygame.display.set_caption('Snake')
  16. snake =[]
  17.  
  18. class Square():
  19.     def __init__(self, x, y, color):
  20.         self.x = x
  21.         self.y = y
  22.         self.color = color
  23.  
  24.     def show(self):
  25.         pygame.draw.rect(canvas, self.color, (self.x * grid, self.y * grid, grid, grid), 0)
  26.  
  27. class Food():
  28.     def __init__(self, color):
  29.         self.color = color
  30.  
  31.     def randomizeFood(self):
  32.         x = random.randrange(0, width, grid)
  33.         y = random.randrange(0, height, grid)
  34.  
  35.         return x, y
  36.  
  37.     def showFood(self, x, y):
  38.         pygame.draw.rect(canvas, self.color, (x, y, grid, grid), 0)
  39.  
  40. class Snake():
  41.     def __init__(self):
  42.  
  43.         self.x = 6
  44.         self.y = 6
  45.         self.speed = 1
  46.         self.color = snakeColor
  47.        
  48.         snake.append(Square(self.x, self.y, snakeColor))
  49.         snake.append(Square(self.x, self.y + 1, snakeColor))
  50.         snake.append(Square(self.x, self.y + 2, snakeColor))
  51.  
  52.  
  53.     def updateSnake(self, direction):
  54.         if direction == 'up':
  55.  
  56.             snakeHead = Square(self.x, self.y - self.speed, snakeColor)
  57.             print(direction)
  58.         if direction == 'down':
  59.             snakeHead = Square(self.x, self.y + self.speed, snakeColor)
  60.             print(direction)
  61.         if direction == 'left':
  62.             snakeHead = Square(self.x - self.speed, self.y, snakeColor)
  63.             print(direction)
  64.         if direction == 'right':
  65.             snakeHead = Square(self.x + self.speed, self.y, snakeColor)
  66.             print(direction)
  67.  
  68.         snake.insert(0, snakeHead)
  69.         eatFood()
  70.  
  71.  
  72.     def showSnake(self):
  73.         MAX_X = width/grid
  74.         MAX_Y = height/grid
  75.         if self.x >= MAX_X:
  76.             self.x = MAX_X-1
  77.         if self.x <= 0:
  78.             self.x = 0
  79.  
  80.         if self.y >= MAX_Y:
  81.             self.y = MAX_Y-1
  82.         if self.y <= 0:
  83.             self.y = 0
  84.  
  85.         pygame.draw.rect(canvas, self.color, (self.x*grid, self.y*grid, grid, grid), 0)
  86.         return self.x*grid, self.y*grid
  87.  
  88. def keyPressed():
  89.     keys = pygame.key.get_pressed()
  90.     global direction
  91.  
  92.     if (keys[pygame.K_RIGHT]):
  93.         direction = 'right'
  94.     if (keys[pygame.K_LEFT]):
  95.         direction = 'left'
  96.     if (keys[pygame.K_UP]):
  97.         direction = 'up'
  98.     if (keys[pygame.K_DOWN]):
  99.         direction = 'down'
  100.  
  101.     return direction
  102.  
  103. def eatFood():
  104.     global xFood, yFood
  105.     xSnake, ySnake = s.showSnake()
  106.  
  107.     if xFood == xSnake and yFood == ySnake:
  108.         xFood, yFood = f.randomizeFood()
  109.     else:
  110.         snake.pop()
  111.  
  112.  
  113.     f.showFood(xFood, yFood)
  114.  
  115. f = Food(foodColor)
  116. s = Snake()
  117.  
  118. xFood, yFood = 40,40
  119.  
  120. while openWindow == True:
  121.     canvas.fill((0,0,0))
  122.     snakeDir = keyPressed()
  123.  
  124.     s.updateSnake(snakeDir)
  125.     s.showSnake()
  126.    
  127.    
  128.     pygame.display.flip()
  129.     time.sleep(0.1)
  130.  
  131.     for event in pygame.event.get():
  132.         if event.type == pygame.QUIT:
  133.             openWindow = False
Advertisement
Add Comment
Please, Sign In to add comment