-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
217 lines (166 loc) · 5.92 KB
/
module.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
import pygame
import pygame.freetype
from pygame.sprite import Sprite, RenderUpdates
from enum import Enum
from os.path import join
from random import randint
from sys import exit
BLUE = (102, 178, 255)
BLACK = (0, 0, 0)
# general functions and classes
def text_surface(digit, font_size=60, text_rgb=BLACK, bg_rgb=BLUE):
font = pygame.freetype.SysFont("Consolas", font_size, bold=True)
surface, _ = font.render(text=digit, fgcolor=text_rgb, bgcolor=bg_rgb)
return surface.convert_alpha()
class Button(Sprite):
def __init__(self, center_position, path_default, path_highlighted, action=None):
super().__init__()
self.mouse_over = False
default_image = pygame.image.load(path_default)
highlighted_image = pygame.image.load(path_highlighted)
self.images = [default_image, highlighted_image]
self.rects = [default_image.get_rect(center=center_position),
highlighted_image.get_rect(center=center_position)]
self.action = action
@property
def image(self):
return self.images[1] if self.mouse_over else self.images[0]
@property
def rect(self):
return self.rects[1] if self.mouse_over else self.rects[0]
def update(self, mouse_pos, mouse_up):
if self.rect.collidepoint(mouse_pos):
self.mouse_over = True
if mouse_up:
return self.action
else:
self.mouse_over = False
def draw(self, surface):
surface.blit(self.image, self.rect)
class State(Enum):
QUIT = -1
TITLE = 0
NEWGAME = 1
NEXT_LEVEL = 2
INPUT = 3
EVAL = 4
END = 5
def game_loop(screen, buttons, obj=None, obj_pos=None):
while True:
mouse_up = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
mouse_up = True
screen.fill(BLUE)
if obj is not None:
screen.blit(obj, obj_pos)
for button in buttons:
action = button.update(pygame.mouse.get_pos(), mouse_up)
if action is not None:
return action
buttons.draw(screen)
pygame.display.flip()
# functions for actual states
def title_screen(screen):
start = Button(
center_position=(400, 300),
path_default=join("images", "icons", "start1.png"),
path_highlighted=join("images", "icons", "start2.png"),
action=State.NEWGAME
)
buttons = RenderUpdates(start)
return game_loop(screen, buttons)
def display(screen, level, pattern):
screen.fill(BLUE)
screen.blit(text_surface("Level " + str(level)), (280, 250))
pygame.display.flip()
pygame.time.delay(1000)
screen.fill(BLUE)
screen.blit(text_surface("Level " + str(level), 30), (20, 20))
pygame.display.flip()
pygame.time.delay(2000)
for digit in pattern:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
obj = text_surface(str(digit), 90)
screen.fill(BLUE)
screen.blit(text_surface("Level " + str(level), 30), (20, 20))
screen.blit(obj, (randint(90, 620), randint(90, 420)))
pygame.display.flip()
pygame.time.delay(1000)
return State.INPUT
def input_page(screen, inp):
numbers = []
x = 75
for i in range(10):
x += 60
numbers.append(Button(
center_position=(x, 300),
path_default=join("images", "input", str(i) + "_1.png"),
path_highlighted=join("images", "input", str(i) + "_2.png"),
action=State.INPUT
))
clear = Button(
center_position=(325, 400),
path_default=join("images", "input", "clear_1.png"),
path_highlighted=join("images", "input", "clear_2.png"),
action=State.INPUT
)
done = Button(
center_position=(475, 400),
path_default=join("images", "input", "done_1.png"),
path_highlighted=join("images", "input", "done_2.png"),
action=State.EVAL
)
obj = text_surface("".join(list(map(str, inp))), 30) if inp else None
obj_rect = obj.get_rect(center=(400, 150)) if obj is not None else None
while True:
mouse_up = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
mouse_up = True
screen.fill(BLUE)
if obj is not None:
screen.blit(obj, obj_rect)
for i, number in enumerate(numbers):
action = number.update(pygame.mouse.get_pos(), mouse_up)
if action is not None:
inp.append(i)
return action
action = clear.update(pygame.mouse.get_pos(), mouse_up)
if action is not None:
if inp:
inp.pop()
return action
action = done.update(pygame.mouse.get_pos(), mouse_up)
if action is not None:
return action
buttons = RenderUpdates(numbers + [clear, done])
buttons.draw(screen)
pygame.display.flip()
def end_screen(screen):
close = Button(
center_position=(500, 400),
path_default=join("images", "icons", "exit1.png"),
path_highlighted=join("images", "icons", "exit2.png"),
action=State.QUIT
)
restart = Button(
center_position=(300, 400),
path_default=join("images", "icons", "restart1.png"),
path_highlighted=join("images", "icons", "restart2.png"),
action=State.NEWGAME
)
buttons = RenderUpdates(restart, close)
img = pygame.image.load(join("images", "game_over.png"))
img = pygame.transform.scale(img, (300, 300))
return game_loop(screen, buttons, img, (250, 25))