-
Notifications
You must be signed in to change notification settings - Fork 0
/
cobrinha.py
61 lines (50 loc) · 1.31 KB
/
cobrinha.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
from copy import copy
import pygame
SIZE = W, H = (800, 600)
BLOCK = W / 32
def init():
global screen
screen = pygame.display.set_mode(SIZE)
def process_keys(keys, pos):
if keys[pygame.K_RIGHT]:
pos[0] += BLOCK
if pos[0] >= W:
pos[0] = 0
if keys[pygame.K_LEFT]:
pos[0] -= BLOCK
if pos[0] < 0:
pos[0] = W - BLOCK
if keys[pygame.K_DOWN]:
pos[1] += BLOCK
if pos[1] >= H:
pos[1] = 0
if keys[pygame.K_UP]:
pos[1] -= BLOCK
if pos[1] < 0:
pos[1] = H - BLOCK
def main():
pos = [0, 0]
body = [pos, ]
max_length = 7
while True:
event = pygame.event.get()
for event in event:
if event.type == pygame.QUIT:
return
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
return
process_keys(keys, pos)
body.insert(0, copy(pos))
if len(body) > max_length:
end = body.pop()
pygame.draw.rect(screen, (0, 0, 0), (end[0], end[1], BLOCK, BLOCK))
pygame.draw.rect(screen, (255, 0, 0), (pos[0], pos[1], BLOCK, BLOCK))
old_pos = copy(pos)
pygame.display.flip()
pygame.time.delay(30)
try:
init()
main()
finally:
pygame.quit()