-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_state.py
143 lines (119 loc) · 5.32 KB
/
game_state.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
import random
import pygame
from utils import COLORS, get_sprite_surface
SNAKE_HEAD = [[0, 0, 0, 3, 3, 0, 0, 0],
[0, 0, 3, 2, 2, 3, 0, 0],
[0, 3, 2, 2, 2, 2, 3, 0],
[0, 3, 3, 2, 2, 3, 3, 0],
[3, 1, 1, 3, 3, 1, 1, 3],
[3, 1, 1, 3, 3, 1, 1, 3],
[0, 3, 3, 2, 2, 3, 3, 0],
[0, 0, 3, 3, 3, 3, 0, 0]]
SNAKE_BODY = [[0, 3, 3, 0],
[3, 2, 2, 3],
[3, 2, 2, 3],
[0, 3, 3, 0]]
APPLE = [[0, 3, 3, 0],
[3, 3, 3, 3],
[3, 3, 3, 3],
[0, 3, 3, 0]]
class GameState(object):
def __init__(self, width, height):
self.head_sprite = get_sprite_surface(SNAKE_HEAD)
self.head_sprite.set_colorkey(COLORS[0])
self.body_sprite = get_sprite_surface(SNAKE_BODY)
self.body_sprite.set_colorkey(COLORS[0])
self.apple_sprite = get_sprite_surface(APPLE)
self.apple_sprite.set_colorkey(COLORS[0])
self.score_font = pygame.font.Font('fonts/prstart.ttf', 8)
self.width = width
self.height = height
self.game_width = width - 8
self.game_height = height - 16
self.game_x = 4
self.game_y = 12
self.apples = []
self.snake_body = []
self.snake_direction = None
self.last_ticks = 0
self.game_finished = False
self.reset_game()
def randomize_apple(self):
while True:
apple = (
random.randint(0, (self.game_width / 4) - 1), random.randint(0, (self.game_height / 4) - 1))
if not apple in self.snake_body:
self.apples = [apple]
return
def reset_game(self):
self.snake_body = [
(self.game_width / 8, self.game_height / 8),
(self.game_width / 8, self.game_height / 8 + 1),
]
self.snake_direction = (0, -1)
self.last_direction = self.snake_direction
self.randomize_apple()
self.last_ticks = pygame.time.get_ticks()
self.update_ticks = 300
self.score = 0
def get_next_state(self):
if self.game_finished:
surface = pygame.Surface((self.width, self.height))
self.draw(surface)
from lost_state import LostState
return LostState(surface, self.score)
return self
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.last_direction != (1, 0):
self.snake_direction = (-1, 0)
if keys[pygame.K_RIGHT] and self.last_direction != (-1, 0):
self.snake_direction = (1, 0)
if keys[pygame.K_UP] and self.last_direction != (0, 1):
self.snake_direction = (0, -1)
if keys[pygame.K_DOWN] and self.last_direction != (0, -1):
self.snake_direction = (0, 1)
curr_ticks = pygame.time.get_ticks()
if curr_ticks - self.last_ticks > self.update_ticks:
self.last_direction = self.snake_direction
self.updated = True
self.last_ticks = curr_ticks
new_head = (
self.snake_body[0][0] + self.snake_direction[0], self.snake_body[0][1] + self.snake_direction[1])
if new_head in self.snake_body or new_head[0] < 0 or new_head[1] < 0 or new_head[0] >= self.game_width / 4 or new_head[1] >= self.game_height / 4:
self.game_finished = True
return
if new_head in self.apples:
self.snake_body = [new_head] + self.snake_body
self.randomize_apple()
if self.update_ticks > 50:
self.update_ticks -= 50
self.score += 100
else:
self.snake_body = [new_head] + self.snake_body[:-1]
else:
self.updated = False
def draw(self, buffer):
if self.updated:
buffer.fill(COLORS[2])
pygame.draw.rect(buffer, COLORS[0], (self.game_x, self.game_y, self.game_width, self.game_height))
pygame.draw.rect(buffer, COLORS[3],
(self.game_x - 1, self.game_y - 1, self.game_width + 1, self.game_height + 1), 1)
for x, y in self.apples:
buffer.blit(self.apple_sprite, (self.game_x + x * 4,
self.game_y + y * 4))
if self.snake_direction == (1, 0):
head_directed = pygame.transform.rotate(self.head_sprite, 90 * 3)
if self.snake_direction == (-1, 0):
head_directed = pygame.transform.rotate(self.head_sprite, 90)
if self.snake_direction == (0, -1):
head_directed = self.head_sprite
if self.snake_direction == (0, 1):
head_directed = pygame.transform.rotate(self.head_sprite, 90 * 2)
for x, y in self.snake_body[1:]:
buffer.blit(self.body_sprite, (self.game_x + x * 4, self.game_y + y * 4))
buffer.blit(head_directed, (self.game_x + self.snake_body[0][0] * 4 - 2,
self.game_y + self.snake_body[0][1] * 4 - 2))
txt = "SCORE: {0:06d}".format(self.score)
buffer.blit(self.score_font.render(txt, False, COLORS[3]), (self.game_x + 1, 3))
buffer.blit(self.score_font.render(txt, False, COLORS[1]), (self.game_x, 2))