MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / gameloop

Function gameloop

projects/car game/Game.py:184–454  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

182
183# defining our gameloop function
184def gameloop():
185 ####### music #######
186 pygame.mixer.music.load("BackgroundMusic.mp3")
187 pygame.mixer.music.play()
188 ###### sound effect for collision ######
189 crash_sound = pygame.mixer.Sound("car_crash.wav")
190
191 ####### scoring part ######
192 score_value = 0
193 font1 = pygame.font.Font("freesansbold.ttf", 25)
194
195 def show_score(x, y):
196 score = font1.render("SCORE: " + str(score_value), True, (255, 0, 0))
197 screen.blit(score, (x, y))
198
199 # highscore part
200 with open("highscore.txt", "r") as f:
201 highscore = f.read()
202
203 def show_highscore(x, y):
204 Hiscore_text = font1.render("HISCORE :" + str(highscore), True, (255, 0, 0))
205 screen.blit(Hiscore_text, (x, y))
206 pygame.display.update()
207
208 ###### creating our game over function #######
209
210 def gameover():
211 gameoverImg = pygame.image.load("gameover.png")
212 run = True
213 while run:
214 screen.blit(gameoverImg, (0, 0))
215 time.sleep(0.5)
216 show_score(330, 400)
217 time.sleep(0.5)
218 show_highscore(330, 450)
219 pygame.display.update()
220
221 for event in pygame.event.get():
222 if event.type == pygame.QUIT:
223 run = False
224 pygame.quit()
225 sys.exit()
226 if event.type == pygame.KEYDOWN:
227 if event.key == pygame.K_SPACE:
228 countdown()
229 if event.key == pygame.K_ESCAPE:
230 pygame.quit()
231 sys.exit()
232
233 # setting background image
234 bg = pygame.image.load("bg.png")
235
236 # setting our player
237 maincar = pygame.image.load("main car.png")
238 maincarX = 350
239 maincarY = 495
240 maincarX_change = 0
241 maincarY_change = 0

Callers 1

countdownFunction · 0.85

Calls 5

show_highscoreFunction · 0.85
iscollisionFunction · 0.85
gameoverFunction · 0.85
show_scoreFunction · 0.70
updateMethod · 0.45

Tested by

no test coverage detected