-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (124 loc) · 4.73 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#*Librerias
import pygame
from pygame.math import Vector2
from random import randrange
import os
import config
#*Configurar pantalla
pygame.init()
screen = pygame.display.set_mode((config.SCREEN_SIZE, config.SCREEN_SIZE))
font = pygame.font.SysFont(config.SCORE_FONT, config.SCORE_SIZE)
clock = pygame.time.Clock()
#*Funciones
def read_best_score():
if os.path.exists(best_score_file):
with open(best_score_file, 'r') as file:
return int(file.read())
return 0
def save_best_score(score):
with open(best_score_file, 'w') as file:
file.write(str(score))
#*Archivo de mejor puntaje
best_score_file = 'best_score.txt'
best_score = read_best_score()
current_score = 0
#*Variables
running = True
begin = True
bait = True
message = False
time = None
snake_rect = None
snake_length = None
snake_parts = None
snake_direction = None
food_rect = None
while running:
#*Iniciar el juego
if begin:
begin = False
time = pygame.time.get_ticks()
#*Posicion de la serpiente
snake_rect = pygame.rect.Rect(
[randrange(0, config.SCREEN_SIZE, config.GRID_CELL_SIZE),
randrange(0, config.SCREEN_SIZE, config.GRID_CELL_SIZE),
config.SNAKE_PART_SIZE,
config.SNAKE_PART_SIZE])
#*caracteristicas de la serpiente
snake_length = 1
snake_parts = []
snake_direction = Vector2(0,0)
#*Posicion de la comida
if bait:
bait = False
food_rect = pygame.rect.Rect(
[randrange(0, config.SCREEN_SIZE, config.GRID_CELL_SIZE),
randrange(0, config.SCREEN_SIZE, config.GRID_CELL_SIZE),
config.FOOD_SIZE,
config.FOOD_SIZE])
#*Eventos
for event in pygame.event.get():
#*Salir del juego
if (event.type == pygame.QUIT or
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE)):
running = False
#*Mostrar la puntuacion en la pantalla
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
message = f"Score: {best_score}"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
message = None
#*Mover la serpiente con las flechas
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and not snake_direction.y:
snake_direction = Vector2(0, -config.SNAKE_MOVE_LENGTH)
if event.key == pygame.K_DOWN and not snake_direction.y:
snake_direction = Vector2(0, config.SNAKE_MOVE_LENGTH)
if event.key == pygame.K_LEFT and not snake_direction.x:
snake_direction = Vector2(-config.SNAKE_MOVE_LENGTH, 0)
if event.key == pygame.K_RIGHT and not snake_direction.x:
snake_direction = Vector2(config.SNAKE_MOVE_LENGTH, 0)
time_now = pygame.time.get_ticks()
#*Color de fondo
screen.fill(config.COLOR_BG)
#*Dibujar cuadricula
for i in range(0, config.SCREEN_SIZE, config.GRID_CELL_SIZE):
pygame.draw.line(screen, config.COLOR_GRID, (i, 0), (i, config.SCREEN_SIZE))
pygame.draw.line(screen, config.COLOR_GRID, (0, i), (config.SCREEN_SIZE, i))
#*Mover la serpiente
if time_now - time > config.DELAY:
time = time_now
snake_rect.move_ip(snake_direction)
snake_parts.append(snake_rect.copy())
snake_parts = snake_parts[-snake_length:]
#*Dibujar la comida
pygame.draw.rect(screen, config.FOOD_COLOR, food_rect, 0, 10)
#*Dibujar la serpiente
[pygame.draw.rect(screen, config.SNAKE_COLOR, snake_part, config.SNAKE_PADDING, 1)
for snake_part in snake_parts]
#*Colisiones
if (snake_rect.left < 0 or snake_rect.right > config.SCREEN_SIZE or
snake_rect.top < 0 or snake_rect.bottom > config.SCREEN_SIZE or
snake_rect.collidelist(snake_parts[:-1]) != -1):
begin = True
#*Puntaje
pygame.display.set_caption(f'Snake | Score: {snake_length - 1}')
#*Mejor puntaje
if snake_length - 1 > best_score:
best_score = snake_length - 1
save_best_score(best_score)
#*Mostrar mejor puntaje
pygame.display.set_caption(f'Snake | Score: {snake_length - 1}')
#*Comer la comida
if snake_rect.colliderect(food_rect):
bait = True
snake_length += 1
#*Mostrar mensaje en la pantalla
if message:
text_score = font.render(f"Score: {snake_length - 1} Best Score: {best_score}", True, config.SCORE_COLOR)
screen.blit(text_score, (10, 23))
#*Actualizar la pantalla
pygame.display.flip()
clock.tick(config.FPS)
pygame.quit()