-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_states.py
123 lines (102 loc) · 4.42 KB
/
game_states.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
import pygame
from constants import *
from state_machine import StateMachine
from game_objects import SnakePart, Collectible, Text
class BaseState:
def __init__(self):
...
def update(self, clock):
...
def draw(self):
...
class TitleState(BaseState):
def __init__(self):
super().__init__()
self.title_text = Text(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 300, "SNAKE")
self.time_passed = 0
def update(self, clock):
super().update(clock)
self.time_passed += clock.get_time()
if self.time_passed > 10:
try:
self.title_text.change_alpha(-2)
except ValueError:
StateMachine.change_current(PlayState())
self.time_passed = 0
def draw(self):
super().draw()
self.title_text.draw()
class PlayState(BaseState):
def __init__(self):
self.play_area = pygame.Surface((MAP_W * TILE_SIZE, MAP_H * TILE_SIZE))
self.snake = SnakePart((MAP_W-1)/2 * TILE_SIZE, (MAP_H-1)/2 * TILE_SIZE, head=True)
self.move_threshold = 0
self.game_over = False
self.current_collectible = None
self.direction_queue = []
self.overlay_text = None
self.score = 0
def update(self, clock):
keys_pressed = pygame.key.get_pressed()
if len(self.direction_queue) < 2:
if keys_pressed[pygame.K_UP]:
if "u" not in self.direction_queue and self.snake.direction != "u":
self.direction_queue.append("u")
elif keys_pressed[pygame.K_DOWN]:
if "d" not in self.direction_queue and self.snake.direction != "d":
self.direction_queue.append("d")
elif keys_pressed[pygame.K_LEFT]:
if "l" not in self.direction_queue and self.snake.direction != "l":
self.direction_queue.append("l")
elif keys_pressed[pygame.K_RIGHT]:
if "r" not in self.direction_queue and self.snake.direction != "r":
self.direction_queue.append("r")
self.move_threshold += clock.get_time() * self.snake.speed
if self.move_threshold >= 1000 and self.game_over is False:
self.move_threshold = 0
if self.current_collectible:
self.current_collectible.lifetime -= 1
if self.current_collectible.lifetime < 10:
self.current_collectible.color = "blue"
elif self.current_collectible.lifetime <= 0:
self.current_collectible = None
if self.snake.collides(self.current_collectible):
self.snake.spawn_new_part()
self.score += 10
self.current_collectible = None
else:
while True:
self.current_collectible = Collectible.spawn_collectible()
collectible_spawned = True
snake_part = self.snake
while snake_part:
if self.current_collectible.collides(snake_part):
collectible_spawned = False
snake_part = snake_part.attaching
if collectible_spawned:
break
if len(self.direction_queue) > 0:
self.snake.change_direction(self.direction_queue.pop(0))
self.snake.update()
if self.snake.check_collision_with_self():
self.game_over = True
if self.game_over:
self.overlay_text = Text(
SCREEN_WIDTH/2, SCREEN_HEIGHT/2,
150, f"SCORE: {self.score}"
)
else:
self.overlay_text = Text(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 - (TILE_SIZE * MAP_H)/2 - 15, 50, f"SCORE: {self.score}")
def draw(self):
self.play_area.fill((180, 188, 0))
if self.current_collectible:
self.current_collectible.draw(self.play_area)
current_snake_part = self.snake
while True:
current_snake_part.draw(self.play_area)
current_snake_part = current_snake_part.attaching
if current_snake_part is None:
break
pygame.display.get_surface().blit(self.play_area, (SCREEN_WIDTH/2 - (MAP_W * TILE_SIZE / 2), SCREEN_HEIGHT/2 - (MAP_H * TILE_SIZE / 2)))
if self.overlay_text:
self.overlay_text.draw()