-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPyTiles.py
executable file
·268 lines (195 loc) · 8.47 KB
/
PyTiles.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import pygame
import random
import time
pygame.init()
display_width = 600
display_height = 800
black = (0, 0, 0)
white = (255, 255, 255)
red = (200, 0, 0)
green = (0, 200, 0)
blue = (0, 0, 200)
grey = (100, 100, 100)
gap = 2
tile_width = (display_width - 10) / 4
tile_height = display_height * 0.25 - gap # tile_width * 1.5
grid = [gap,
(tile_width + gap * 2),
(tile_width * 2 + gap * 3),
(tile_width * 3 + gap * 4)]
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Py Tiles")
clock = pygame.time.Clock()
class Tiles:
posy = -tile_height
posx = random.choice(grid)
pressed = False
def tile(self):
if self.pressed is True:
color = grey
else:
color = black
pygame.draw.rect(gameDisplay, color, [self.posx, self.posy, tile_width, tile_height])
def quitgame():
pygame.quit()
quit()
def text_objects(text, font, color):
text_surface = font.render(text, True, color)
return text_surface, text_surface.get_rect()
def text(x, y, message, size, type, color):
font = pygame.font.Font(type, size)
text_surf, text_rect = text_objects(message, font, color)
text_rect.center = (x, y)
gameDisplay.blit(text_surf, text_rect)
def button(msg, x, y, width, height, icolor, acolor, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + width > mouse[0] > x and y + height > mouse[1] > y:
pygame.draw.rect(gameDisplay, acolor, (x, y, width, height))
if click[0] == 1: # and action is not None
action()
else:
pygame.draw.rect(gameDisplay, icolor, (x, y, width, height))
text((x + (width / 2)), (y + (height / 2)), msg, 20, "resources/FreeSansBold.ttf", white)
def gameover():
text((display_width / 2), (display_height / 2), "Game Over", 100, "resources/ARCADE.TTF", red)
pygame.display.update()
time.sleep(2)
game()
def start_menu():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitgame()
gameDisplay.fill(white)
text((display_width / 2), (display_height / 2 - 50), "Py Tiles", 115, "resources/ARCADE.TTF", black)
button("Go!", (display_width / 2 - 75), (display_height * 0.65), 150, 50, black, grey, game)
button("Exit", (display_width / 2 - 75), (display_height * 0.75), 150, 50, black, grey, quitgame)
pygame.display.update()
clock.tick(15)
def game():
tile1 = Tiles()
tile2 = Tiles()
tile3 = Tiles()
tile4 = Tiles()
tile5 = Tiles()
tile_list = [tile1, tile2, tile3, tile4, tile5]
mult = 1
accelerator = 1
for tile in tile_list:
tile.posx = random.choice(grid)
tile.posy += -tile_height * mult - gap * (mult + 1)
mult += 1
tile_speed = 6
score = 0
while True:
tileposy_list = []
for tile in tile_list:
if tile.pressed is False:
tileposy_list.append(tile.posy)
tileposy_list.sort()
tileposy_list.reverse()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quitgame()
if event.type == pygame.KEYDOWN:
if not tileposy_list:
pass
else:
if event.key == pygame.K_h:
if tile1.posx == grid[0] and tile1.posy == tileposy_list[0]:
tile1.pressed = True
score += 1
elif tile2.posx == grid[0] and tile2.posy == tileposy_list[0]:
tile2.pressed = True
score += 1
elif tile3.posx == grid[0] and tile3.posy == tileposy_list[0]:
tile3.pressed = True
score += 1
elif tile4.posx == grid[0] and tile4.posy == tileposy_list[0]:
tile4.pressed = True
score += 1
elif tile5.posx == grid[0] and tile5.posy == tileposy_list[0]:
tile5.pressed = True
score += 1
else:
gameover()
if event.key == pygame.K_j:
if tile1.posx == grid[1] and tile1.posy == tileposy_list[0]:
tile1.pressed = True
score += 1
elif tile2.posx == grid[1] and tile2.posy == tileposy_list[0]:
tile2.pressed = True
score += 1
elif tile3.posx == grid[1] and tile3.posy == tileposy_list[0]:
tile3.pressed = True
score += 1
elif tile4.posx == grid[1] and tile4.posy == tileposy_list[0]:
tile4.pressed = True
score += 1
elif tile5.posx == grid[1] and tile5.posy == tileposy_list[0]:
tile5.pressed = True
score += 1
else:
gameover()
if event.key == pygame.K_k:
if tile1.posx == grid[2] and tile1.posy == tileposy_list[0]:
tile1.pressed = True
score += 1
elif tile2.posx == grid[2] and tile2.posy == tileposy_list[0]:
tile2.pressed = True
score += 1
elif tile3.posx == grid[2] and tile3.posy == tileposy_list[0]:
tile3.pressed = True
score += 1
elif tile4.posx == grid[2] and tile4.posy == tileposy_list[0]:
tile4.pressed = True
score += 1
elif tile5.posx == grid[2] and tile5.posy == tileposy_list[0]:
tile5.pressed = True
score += 1
else:
gameover()
if event.key == pygame.K_l:
if tile1.posx == grid[3] and tile1.posy == tileposy_list[0]:
tile1.pressed = True
score += 1
elif tile2.posx == grid[3] and tile2.posy == tileposy_list[0]:
tile2.pressed = True
score += 1
elif tile3.posx == grid[3] and tile3.posy == tileposy_list[0]:
tile3.pressed = True
score += 1
elif tile4.posx == grid[3] and tile4.posy == tileposy_list[0]:
tile4.pressed = True
score += 1
elif tile5.posx == grid[3] and tile5.posy == tileposy_list[0]:
tile5.pressed = True
score += 1
else:
gameover()
gameDisplay.fill(white)
for tile in tile_list:
tile.tile()
tile.posy += tile_speed
text(35, 15, "Score: " + str(score), 15, "resources/FreeSansBold.ttf", red)
for tile in tile_list:
if tile.posy > display_height:
if tile.pressed is not True:
gameover()
else:
tile_index = tile_list.index(tile)
tile.posy = tile_list[tile_index - 1].posy - tile_height - gap
tile.posx = random.choice(grid)
tile.pressed = False
if score < 20:
continue
elif score % 20 == 0:
tile_speed += accelerator
text((grid[0] + (tile_width / 2)), (display_height - 30), "H", 30, "resources/FreeSansBold.ttf", black)
text((grid[1] + (tile_width / 2)), (display_height - 30), "J", 30, "resources/FreeSansBold.ttf", black)
text((grid[2] + (tile_width / 2)), (display_height - 30), "K", 30, "resources/FreeSansBold.ttf", black)
text((grid[3] + (tile_width / 2)), (display_height - 30), "L", 30, "resources/FreeSansBold.ttf", black)
pygame.display.update()
clock.tick(60)
start_menu()