-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·332 lines (301 loc) · 23.4 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#! /usr/bin/env python
from random import randrange
import pygame
from pygame.locals import *
from pygame import *
from pygame.transform import *
import os
local = os.getcwd()
# P1
# [-26, 205]
# [219, 471]
def button(x,y,w,h,cor):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(screen, cor, (x,y,w,h))
if x+w > mouse[0] > x and y+h > mouse[1] > y and click[0]:
return False
else:
return True
def get_rect(obj):
return Rect(obj['position'][0],
obj['position'][1],
obj['surface'].get_width(),
obj['surface'].get_height())
def get_b_rect(obj):
if (obj['surface'].get_height() < 150):
return Rect(obj['position'][0] + 50,
obj['position'][1] + 20,
obj['surface'].get_width() - 20,
obj['surface'].get_height() - 100)
return Rect(obj['position'][0] + 20,
obj['position'][1] + 100,
obj['surface'].get_width() - 20,
obj['surface'].get_height() - 200)
def bridge_crossed():
person_rect = get_rect(player)
for bridge in bridges:
if person_rect.colliderect(get_b_rect(bridge)):
bridge['crossed'] = True
pygame.init()
# Fonts
pygame.font.init()
font_name = pygame.font.get_default_font()
game_font = pygame.font.SysFont(font_name, 40)
font_over = pygame.font.SysFont("comicsansms", 100)
small_font = pygame.font.SysFont(font_name, 30)
medium_font = pygame.font.SysFont(font_name, 50)
# Color definitions
color_red = (255,0,0)
color_green = (0,255,0)
color_black = (0,0,0)
# Display size
screen = pygame.display.set_mode((1200, 720))
# Load background image
bg_filename = local + '/images/bg_final.png'
background = image.load(bg_filename).convert()
pygame.display.set_caption('Bridges')
map = [
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.............................................................................................................................................",
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.............................................................................................................................................",
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.............................................................................................................................................",
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.............................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"...............................................................................................................................................................AAAAAAAAAAAAA",
"...............................................................................................................................................................AAAAAAAAAAAAA",
"................................................................................................AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..............AAA..........",
"................................................................................................AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA............AAA..........",
"................................................................................................AAA............................................AAAAAA...........AA..........",
"...............................................................................AAAAAAAAAA.......AAA................................................AAA..........AAA.........",
"..............................................................AAAAAAAAAAAAAAAAAAAAAAAAAAA.......AAA.................................................AAA.........AAA.........",
".........................................................AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.......AAA.................................................AAAA.........AA.........",
".....................................................AAAAAAAAA.........................AA.......AAA...................................................AAA........AA.........",
"..................................................AAAAAAA..............................AA.......AAA....................................................AA........AA.........",
"...............................................AAAAA...................................AA.......AAA....................................................AA.........AA........",
".............................................AAAAA.....................................AA.......AAA....................................................AA.........AA........",
"............................................AAA........................................AA.......AAA....................................................AA.........AA........",
"...........................................AAAA........................................AA.......AAA....................................................AA.........AAAAAAAAAA",
"............................................AAA........................................AA.......AAA....................................................AA.........AAAAAAAAAA",
".............................................AAA......................................AAA.......AAA....................................................AA...................",
"..............................................AAA...............................AAAAAAAA........AAAAAAAA..............................................AAA...................",
"..............................................AAA........................AAAAAAAAAAAAAA.........AAAAAAAAAAAA..........................................AA....................",
"...............................................AAA......................AAAAA.......AAA................AAAAAAA.......................................AAA....................",
"..............................AAAAAAAAAA.......AAA.....................AAAA..........AA....................AAAAA..................................AAAAA.....................",
".............................AAAAAAAAAAA........AAA..................AAAA....................................AAAAA................................AAA.......................",
"...........................AAAA.......AAA.......AAA................AAAA........................................AAA................................AAA.......................",
"AAAAAAAAAAAAAAAAAAAAAAAAAAAA...........AAA......AAA.............AAAA...........................................AAAAA..............................AAA.......................",
"AAAAAAAAAAAAAAAAAAAAAAAAAA..............AA.......AAA...........AAAA..............................................AAAA.............................AAA.......................",
"........................................AA.......AAA..........AAA................................................AAAAA............................AAA.......................",
"........................................AA.......AAA.........AA...................................................AAAAAA..........................AAA.......................",
"........................................AA.......AAA........AA.....................................................AAAAAAAAAAAAAAAAAAAAA..........AAA.......................",
"........................................AA.......AAA.......AA......................................................AAAAAAAAAAAAAAAAAAAAAAAA.......AAA.......................",
"........................................AA.......AAA......AA.............................................................................AAAA.....AAA.......................",
"........................................AA.......AAA.....AA.................................................................................AAAA..AAA.......................",
"........................................AA.......AAA....AA....................................................................................AAAAAAA.......................",
"........................................AA.......AAA...AA...................................................................................................................",
"........................................AA.......AAAAAAAA...................................................................................................................",
"........................................AA.......AAAAAAAA...................................................................................................................",
"........................................AA.......AAAAA......................................................................................................................",
"........................................AA.......AAA....................................................................AAAAAAAAAA..........................................",
"........................................AA.......AA..................................................................AAAAAAAAAAAAAAAAAA.....................................",
".......................................AA..........................................................................AAAAA......AAAAAAAAAAAA..................................",
"......................................AA...........................................................................AAAA.............AAAAAAAA................................",
"......................................AA...........................................................................AA.....................AAAA..............................",
".....................................AA............................................................................AA........................AAAA...........................",
"....................................AAA............................................................................AA..........................AAAA.........................",
"AAAAA..............................AA..............................................................................AA............................AA.........................",
"...AAAAA..........................AA.............................................................................AAAA............................AA.........................",
"......AAA.........................AA.............................................................................AAA.............................AA.........................",
".......AA........................AA............................................................................AAAA..............................AA.........................",
"........AA.......................AA..........................................................................AAAA................................AA.........................",
".........AA......................AA......................................................................AAAAAAA.................................AA.........................",
"..........AA.....................AA...................................................................AAAAAAAA...................................AA.........................",
"...........AAA...................AA...................................................................AAAAA......................................AA.........................",
".............AAA.................AA................................................................AAAAA.........................................AA............AAAAAAAAAAAAA",
"...............AAA...............AA.................................................AAAAAA.........AAAA..........................................AA............AAAAAAAAAAAAA",
".................AAA.............AAAA...............................AAAAAAAAAAAAAAAAAAAAAA.........AAAA..........................................AA............AAAA.........",
"...................AA............AAAAAAAAAAAAAAAAAAAAAAAAAAAA.......AAAAAAAAAAAAAAAAAAAAAA.........AAAA..........................................AAAAA.........AAAA.........",
"....................AA..............AAAAAAAAAAAAAAAAAAAAAAAAA.......AA..................AAA........AAAA..........................................AAAAA........AAAA..........",
"....................AA.....................................AA.......AA..................AAA........AAAA..........................................AA..........AAAA...........",
".....................AA....................................AA.......AA..................AAAA........AAA..........................................AA.........AAAA............",
"......................AAAA.................................AA.......AA...................AAA........AAA..........................................AA........AAAA.............",
".........................AAAA..............................AA.......AA....................AAA.......AAA.........................................AAA.......AAAA..............",
"............................AAAAAA.........................AA.......AA.....................AA.......AAA.........................................AA........AAAA..............",
".................................AAAA......................AA.......AA.....................AA.......AAA.........................................AA.......AAAA...............",
"....................................AAAAAAAAAA.............AA.......AA.....................AA........AA........................................AAA.......AAAA...............",
"....................................AAAAAAAAAAAAAAAAA......AA.......AA.....................AA........AA.......................................AAA.......AAAA................",
"..............................................AAAAAAAAAAAAAAA.......AAAAAAAAAAAAAAAAAAAAAAAAA........AA.......................................AA.......AAA..................",
"....................................................AAAAAAAAA.......AAAAAAAAAAAAAAAAAAAAAAAAA........AAAAAAAAAAAAAAAAAAAAA....................AA.......AAA..................",
"...................................................................................................................AAAAAAAAAAAAAAAAAA........AAA.......AAA..................",
"..........................................................................................................................AAAAAAAAAAAAAAAAAAAAAA.......AAA..................",
"..................................................................................................................................AAAAAAAAAAAAAA.......AAA..................",
"........................................................................................................................................AAAAAAAA.......AAA..................",
"............................................................................................................................................AAAA.......AAA..................",
"..............................................................................................................................................AA.......AAA..................",
".......................................................................................................................................................AAAA.................",
"........................................................................................................................................................AAAA................",
".........................................................................................................................................................AAAA............AAA",
"..........................................................................................................................................................AAAA.........AAAAA",
"...........................................................................................................................................................AAAAAAAAAAAAAAA..",
".............................................................................................................................................................AAAAAAAAAAA....",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
"............................................................................................................................................................................",
]
player = {
'surface': image.load(local + '/images/player3.png').convert_alpha(),
'position': [30, 30],
'speed': {
'x': 0,
'y': 0
}
}
bdgf = image.load(local + '/images/bridge.png').convert_alpha()
bdgftr = image.load(local + '/images/bridge_transp.png').convert_alpha()
def brdg():
return [{
'show': True,
'angle': -80,
'flip': False,
'surface': rotate(bdgf,-80),
'position': [250, 170],
'crossed': False },
{
'show': True,
'angle': -90,
'flip': False,
'surface': rotate(bdgf,-90),
'position': [405, 420],
'crossed': False },
{
'show': True,
'angle': -90,
'flip': False,
'surface': rotate(bdgf,-90),
'position': [630, 420],
'crossed': False },
{
'show': True,
'angle':-90,
'flip': False,
'surface': rotate(bdgf,-90),
'position': [600, 70],
'crossed': False },
{
'show': True,
'angle':-60,
'flip': False,
'surface': rotate(bdgf,-60),
'position': [990, 25],
'crossed': False },
{
'show': True,
'angle':-70,
'flip': True,
'surface': flip(rotate(bdgf,-70), True, False),
'position': [990, 440],
'crossed': False },
{
'show': True,
'angle': -4,
'flip': False,
'surface': rotate(bdgf,-4),
'position': [805, 290],
'crossed': False }
]
running = True
clock = pygame.time.Clock()
reset_game = False
bridges = brdg()
player_score = 0
while running:
print(player['position'])
# Pressed or clicked events
pressed_keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT or pressed_keys[K_ESCAPE]:
exit()
# Check for quantity of bridges available
available_bridges = sum([bridge['show'] for bridge in bridges])
if(player_score < available_bridges):
if pressed_keys[K_UP] and player['position'][1] > 0:
player['speed']['y'] = -7
elif pressed_keys[K_DOWN] and player['position'][1] + player['surface'].get_height() < screen.get_size()[1]:
player['speed']['y'] = 7
if pressed_keys[K_LEFT] and player['position'][0] > 0:
player['speed']['x'] = -7
elif pressed_keys[K_RIGHT] and player['position'][0] + player['surface'].get_width() < screen.get_size()[0]:
player['speed']['x'] = 7
if pressed_keys[K_1]:
bridges[0]['show'] = False
if pressed_keys[K_2]:
bridges[1]['show'] = False
if pressed_keys[K_3]:
bridges[2]['show'] = False
if pressed_keys[K_4]:
bridges[3]['show'] = False
if pressed_keys[K_5]:
bridges[4]['show'] = False
if pressed_keys[K_6]:
bridges[5]['show'] = False
if pressed_keys[K_7]:
bridges[6]['show'] = False
screen.blit(pygame.Surface(screen.get_size()), (0, 0))
screen.blit(background, (0, 0))
# Check state of bridges
player_score = 0
bridge_crossed()
for bridge in bridges:
if bridge['show']:
if not bridge['crossed']:
screen.blit(bridge['surface'], bridge['position'])
else:
if bridge['flip']:
bridge['surface'] = flip(rotate(bdgftr,bridge['angle']), True, False)
else:
bridge['surface'] = rotate(bdgftr,bridge['angle'])
screen.blit(bridge['surface'], bridge['position'])
player_score += 1
# Player location
# if (player['position'][0] + player['speed']['x'] < ):
next_x = int((player['position'][0] + player['speed']['x']) / 7)
next_y = int((player['position'][1] + player['speed']['y']) / 7)
if map[next_y][next_x] == '.':
player['position'][0] += player['speed']['x']
player['position'][1] += player['speed']['y']
screen.blit(player['surface'], player['position'])
if player_score >= 6 and available_bridges == 7:
text_surface = font_over.render("GAME OVER", True, color_red)
screen.blit(text_surface , (330,200))
running = button(550,400,100,50,color_red)
quit_surface = game_font.render("QUIT", True, color_black)
screen.blit(quit_surface , (565,415))
elif player_score == available_bridges:
text_surface = font_over.render("GOOD JOB", True, color_black)
screen.blit(text_surface , (320,200))
running = button(550,350,100,50, color_green)
quit_surface = game_font.render("QUIT", True, color_black)
screen.blit(quit_surface , (565,365))
# Show score
player_score_surface = "Pontuação: " + str(player_score)
score_surface = medium_font.render(player_score_surface, False, color_black)
screen.blit(score_surface , (0,0))
#Restart Button
reset_game = not button(1120,0,77,35,color_green)
start_surface = small_font.render("RESET", True, color_black)
screen.blit(start_surface , (1125,10))
if reset_game or pressed_keys[K_r]:
bridges = brdg()
# Restart game
player['speed'] = {'x': 0, 'y': 0}
pygame.display.update()
clock.tick(30)