Advertisement
Guest User

Untitled

a guest
Apr 5th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import pygame
  4. import sys
  5. import time
  6. import random
  7.  
  8. from pygame.locals import *
  9.  
  10. FPS = 5
  11. pygame.init()
  12. fpsClock=pygame.time.Clock()
  13.  
  14. SCREEN_WIDTH, SCREEN_HEIGHT = 320, 320
  15. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
  16. surface = pygame.Surface(screen.get_size())
  17. surface = surface.convert()
  18. surface.fill((255,255,255))
  19. clock = pygame.time.Clock()
  20.  
  21. pygame.key.set_repeat(1, 40)
  22.  
  23. GRIDSIZE=10
  24. GRID_WIDTH = SCREEN_WIDTH / GRIDSIZE
  25. GRID_HEIGHT = SCREEN_HEIGHT / GRIDSIZE
  26. UP    = (0, -1)
  27. DOWN  = (0, 1)
  28. LEFT  = (-1, 0)
  29. RIGHT = (1, 0)
  30.  
  31. screen.blit(surface, (0,0))
  32.  
  33. done = False
  34.  
  35. def draw_box(surf, color, pos):
  36.     r = pygame.Rect((pos[0], pos[1]), (GRIDSIZE, GRIDSIZE))
  37.     pygame.draw.rect(surf, color, r)
  38.  
  39. class Snake(object):
  40.     def __init__(self):
  41.         self.lose()
  42.         self.color = (0,0,0)
  43.  
  44.     def get_head_position(self):
  45.         return self.positions[0]
  46.  
  47.     def lose(self):
  48.         self.length = 1
  49.         self.positions =  [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))]
  50.         self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
  51.  
  52.     def point(self, pt):
  53.         if self.length > 1 and (pt[0] * -1, pt[1] * -1) == self.direction:
  54.             return
  55.         else:
  56.             self.direction = pt
  57.  
  58.     def move(self):
  59.         cur = self.positions[0]
  60.         x, y = self.direction
  61.         new = (((cur[0]+(x*GRIDSIZE)) % SCREEN_WIDTH), (cur[1]+(y*GRIDSIZE)) % SCREEN_HEIGHT)
  62.         if len(self.positions) > 2 and new in self.positions[2:]:
  63.             self.lose()
  64.         else:
  65.             self.positions.insert(0, new)
  66.             if len(self.positions) > self.length:
  67.                 self.positions.pop()
  68.  
  69.     def draw(self, surf):
  70.         for p in self.positions:
  71.             draw_box(surf, self.color, p)
  72.  
  73. class Apple(object):
  74.     def __init__(self):
  75.         self.position = (0,0)
  76.         self.color = (255,0,0)
  77.         self.randomize()
  78.  
  79.     def randomize(self):
  80.         self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE, random.randint(0, GRID_HEIGHT-1) * GRIDSIZE)
  81.  
  82.     def draw(self, surf):
  83.         draw_box(surf, self.color, self.position)
  84.  
  85. def check_eat(snake, apple):
  86.     if snake.get_head_position() == apple.position:
  87.         snake.length += 1
  88.         apple.randomize()
  89.  
  90. while done == False:
  91.     snake = Snake()
  92.     apple = Apple()
  93.     while True:
  94.  
  95.         for event in pygame.event.get():
  96.             if event.type == QUIT:
  97.                 done = True
  98.                 pygame.quit()
  99.                 sys.exit()
  100.             elif event.type == KEYDOWN:
  101.                 if event.key == K_UP:
  102.                     snake.point(UP)
  103.                 elif event.key == K_DOWN:
  104.                     snake.point(DOWN)
  105.                 elif event.key == K_LEFT:
  106.                     snake.point(LEFT)
  107.                 elif event.key == K_RIGHT:
  108.                     snake.point(RIGHT)
  109.  
  110.  
  111.         surface.fill((255,255,255))
  112.         snake.move()
  113.         check_eat(snake, apple)
  114.         snake.draw(surface)
  115.         apple.draw(surface)
  116.         font = pygame.font.Font(None, 36)
  117.         text = font.render(str(snake.length), 1, (10, 10, 10))
  118.         textpos = text.get_rect()
  119.         textpos.centerx = 20
  120.         surface.blit(text, textpos)
  121.         screen.blit(surface, (0,0))
  122.  
  123.         pygame.display.flip()
  124.         pygame.display.update()
  125.         fpsClock.tick(FPS + snake.length/3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement