-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
113 lines (83 loc) · 2.85 KB
/
main.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
import pygame, random
# variables
COLOR_SNAKE = (55,220,10)
COLOR_FONDO = (0,0,0)
COLOR_FOOD = (169,6,6)
COLOR_SCORE = (200,60,80)
GRIS = (50,50,50)
NX = 50
NY = 50
DIM = 15
ANCHURA = NX*DIM
ALTURA = NY*DIM
# inicio del juego
pygame.init()
play_surface = pygame.display.set_mode((ANCHURA, ALTURA))
pygame.display.set_caption("Snake. Creado por: Javier Abollado")
font = pygame.font.Font(None, 30)
FPS = pygame.time.Clock()
# funciones
def food():
x_pos = random.randint(0,NX-1)*DIM
y_pos = random.randint(0,NY-1)*DIM
food_pos = [x_pos, y_pos]
return food_pos
def actualizar_screen(snake_body, food_pos, score, fps):
play_surface.fill(COLOR_FONDO)
for i in range(NY+1):
pygame.draw.rect(play_surface, GRIS, pygame.Rect(0, i*DIM-1, ANCHURA, 2))
for i in range(NY+1):
pygame.draw.rect(play_surface, GRIS, pygame.Rect(i*DIM-1, 0, 2, ALTURA))
for pos in snake_body:
pygame.draw.rect(play_surface, COLOR_SNAKE, pygame.Rect(pos[0], pos[1], DIM, DIM))
pygame.draw.rect(play_surface, COLOR_FOOD, pygame.Rect(food_pos[0], food_pos[1], DIM, DIM))
text = font.render(str(score),0, COLOR_SCORE)
play_surface.blit(text, (ANCHURA-40,20))
FPS.tick(fps)
def main():
snake_pos = [10*DIM, 5*DIM]
snake_body = [[10*DIM, 5*DIM]]
change = "RIGHT"
run = True
food_pos = food()
score = 0
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_KP6:
change = "RIGHT"
if event.key == pygame.K_KP4:
change = "LEFT"
if event.key == pygame.K_KP8:
change = "UP"
if event.key == pygame.K_KP5:
change = "DOWN"
# movimiento de la serpiente
if change == "RIGHT":
snake_pos[0] += DIM
if change == "LEFT":
snake_pos[0] -= DIM
if change == "UP":
snake_pos[1] -= DIM
if change == "DOWN":
snake_pos[1] += DIM
if snake_pos in snake_body:
run = False
# actualizar movimientos
snake_body.insert(0, list(snake_pos))
if snake_pos == food_pos:
food_pos = food()
score += 1
else:
snake_body.pop()
# colorear todos los objetos de la pantalla
actualizar_screen(snake_body, food_pos, score, 10)
# condiciones para finalizar
if snake_pos[0] <= 0 or snake_pos[0] >= ANCHURA or snake_pos[1] <= 0 or snake_pos[1] >= ALTURA:
run = False
pygame.display.flip()
if __name__ == "__main__":
main()
pygame.quit()