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