-
Notifications
You must be signed in to change notification settings - Fork 0
/
startGame.py
270 lines (247 loc) · 10.3 KB
/
startGame.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
__author__ = 'raghuram'
import pygame
from pygame.locals import *
from random import randint
import board
import scoreboard
class Game:
def __init__(self, background, quitimage, yes_image, no_image, restart_image):
pygame.font.init()
self.screen = pygame.display.set_mode((1200, 620), DOUBLEBUF)
self.screen.set_alpha(None)
self.clock = pygame.time.Clock()
self.background = pygame.image.load(background)
self.quitimage = pygame.image.load(quitimage)
self.yesimage = pygame.image.load(yes_image)
self.noimage = pygame.image.load(no_image)
self.restart = pygame.image.load(restart_image)
self.background = pygame.transform.scale(self.background, (1200, 620))
self.quitimage = pygame.transform.scale(self.quitimage, (400, 60))
self.yesimage = pygame.transform.scale(self.yesimage, (50, 50))
self.noimage = pygame.transform.scale(self.noimage, (50, 50))
self.restart = pygame.transform.scale(self.restart, (400, 60))
self.screen.blit(self.background, self.background.get_rect())
pygame.mixer.init()
pygame.mixer.music.load("Sound/GameMusic.mp3")
pygame.mixer.music.play()
pygame.display.flip()
def run(self): # The function which runs the game
board1 = board.Board(self.screen,1)
stateright = 0
stateleft = 0
stateup = 0
statedown = 0
jumpstate = 0
timer = 0
jumpspeed = 0
donkeytimer = 0
fireballfrequency = (60, 80)
lim = randint(fireballfrequency[0], fireballfrequency[1])
scoreboard1 = scoreboard.ScoreBoard("Images/scoreboard.png", board1.getPlayerScore(), self.screen,
"Images/liveplayer.png")
while 1:
quitstate = 0
fireballhitme = 0
self.screen.set_alpha(None)
if timer == lim:
board1.createfireball()
timer = 0
lim = randint(fireballfrequency[0], fireballfrequency[1])
timer += 1
prevScore = board1.getPlayerScore()
ladderstate = board1.getLadderCollisions()
collectCoin = board1.getCoinCollisions()
self.clock.tick(30)
pygame.key.set_repeat()
for ev in pygame.event.get():
if ev.type == QUIT:
quitstate = 1
if not hasattr(ev, 'key'):
continue
if ev.type == KEYDOWN and (ev.key == K_RIGHT or ev.key == K_d):
stateright = 1
if ev.type == KEYUP and (ev.key == K_RIGHT or ev.key == K_d):
stateright = 0
if ev.type == KEYDOWN and (ev.key == K_LEFT or ev.key == K_a):
stateleft = 1
if ev.type == KEYUP and (ev.key == K_LEFT or ev.key == K_a):
stateleft = 0
if ev.type == KEYDOWN and (ev.key == K_UP or ev.key == K_w):
stateup = 1
if ev.type == KEYUP and (ev.key == K_UP or ev.key == K_w):
stateup = 0
if ev.type == KEYDOWN and (ev.key == K_DOWN or ev.key == K_s):
statedown = 1
if ev.type == KEYUP and (ev.key == K_DOWN or ev.key == K_s):
statedown = 0
if ev.type == KEYDOWN and ev.key == K_SPACE and jumpstate == 0:
jumpstate = 1
jumpspeed = 10
if ev.key == K_ESCAPE:
quitstate = 1
if quitstate == 1:
if self.confirmquit() == 1:
return -1
board1.updatefireballs()
donkeytimer += 1
if donkeytimer == 10:
donkeytimer = 0
board1.updatedonkey(1)
else:
board1.updatedonkey(0)
if stateup == 1 and ladderstate == 1:
board1.key_pressed(3)
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if statedown == 1 and ladderstate == 1:
board1.key_pressed(4)
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if ladderstate == 1:
board1.checkplayerlevel()
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if stateright == 1:
board1.key_pressed(1)
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if stateleft == 1:
board1.key_pressed(2)
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if stateright == 0 and stateleft == 0 and ladderstate == 0:
board1.setPlayerstraight()
if ladderstate == 1 and jumpstate == 1:
jumpstate = 0
if jumpstate == 1:
if board1.playerjump(jumpspeed) == 1:
jumpstate = 2
jumpspeed = 0
else:
jumpspeed -= 2
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if jumpstate == 2:
if board1.playerjumpdown(jumpspeed) == 1:
jumpstate = 0
jumpspeed = 10
else:
jumpspeed += 2
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
if ladderstate == 0 and jumpstate == 0:
board1.dropplayer()
dead = board1.checkfireballcollision()
if dead == 0:
if self.askforrestart(board1, scoreboard1) == 1:
return -1
else:
return 0
elif dead == 1:
fireballhitme = 1
jumpstate = 0
board1.checkMidAir()
if board1.checkwin() == 1:
board1.respawnPlayer()
board1.initcoins(self.screen)
board1.setplayerlives()
board1.setplayerlives()
collectCoin = 10
lower, upper = fireballfrequency
fireballfrequency = (max(0, lower - 20),max(0, upper - 20))
board1.boostfireball()
board1.killfireballs()
board1.upgradeplayerlevel()
self.screen.blit(self.background, self.background.get_rect())
board1.update(self.screen)
board1.setPlayerScore(max(0, prevScore + collectCoin * 5 - fireballhitme * 25))
scoreboard1.update(board1.getPlayerScore(), self.screen)
scoreboard1.update_lives(self.screen, board1.getPlayerLives())
scoreboard1.update_level(board1.getplayerlevel(), self.screen)
pygame.display.flip()
def confirmquit(self): # Ask if player really wants to quit
while 1:
self.screen.blit(self.quitimage, (400, 200))
yes = self.screen.blit(self.yesimage, (500, 300))
no = self.screen.blit(self.noimage, (650, 300))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if yes.collidepoint(pos):
return 1
elif no.collidepoint(pos):
return 0
pygame.display.flip()
def askforrestart(self, board1, scoreboard1): # Ask if player wants to restart
self.screen.blit(self.background, self.background.get_rect())
scoreboard1.update(board1.getPlayerScore(), self.screen)
board1.update(self.screen)
pygame.display.flip()
while 1:
self.screen.blit(self.restart, (400, 200))
yes = self.screen.blit(self.yesimage, (500, 300))
no = self.screen.blit(self.noimage, (650, 300))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if yes.collidepoint(pos):
return 0
elif no.collidepoint(pos):
return 1
pygame.display.flip()
if __name__ == '__main__':
while 1:
game = Game('Images/background.jpg', 'Images/areyousure.png', 'Images/yes.png', 'Images/no.png',
'Images/restart.png')
status = game.run() # Launch a new game
if status == -1:
break