Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import time

import pygame as pg
from pygame import KEYDOWN
Expand All @@ -11,11 +12,37 @@
game_active = True


def game_over():
# TODO
global game_active
game_active = False
print('You lost!')
def game_over(surface: pg.Surface, score: int) -> bool:
surface.fill(BG_RGB)
font = pg.font.SysFont("Comic Sans", 32)
button1 = font.render("Quit", True, (0, 0, 0))
button2 = font.render("Retry", True, (0, 0, 0))
lab1 = font.render("You Lost!", True, (255, 0, 0))
lab2 = font.render("Score: " + str(score), True, (0, 0, 0))
button1_rect = button1.get_rect()
button2_rect = button2.get_rect()
lab1_rect = lab1.get_rect()
lab2_rect = lab2.get_rect()
button1_rect.center = (surface.get_width()/2, surface.get_height()/2 - 25)
button2_rect.center = (surface.get_width()/2, surface.get_height()/2 + 50)
lab1_rect.center = (surface.get_width()/2, surface.get_height()/2 - 175)
lab2_rect.center = (surface.get_width()/2, surface.get_height()/2 - 100)
surface.blit(button1, button1_rect)
surface.blit(button2, button2_rect)
surface.blit(lab1, lab1_rect)
surface.blit(lab2, lab2_rect)

run = True
while run:
for ev in pg.event.get():
if ev.type == pg.QUIT:
pg.quit()
if ev.type == pg.MOUSEBUTTONDOWN:
if button1_rect.collidepoint(pg.mouse.get_pos()):
return False
elif button2_rect.collidepoint(pg.mouse.get_pos()):
return True
pg.display.update()


def main():
Expand All @@ -25,6 +52,8 @@ def main():
pg.display.set_caption("GDSC Dino")
clock = pg.time.Clock()
pg.mouse.set_visible(True)
start = int(time.time_ns())
font = pg.font.SysFont("Comic Sans", 16)

player = Player(screen)
obstacles = []
Expand All @@ -45,6 +74,9 @@ def main():
obstacles.append(Obstacle(screen))
next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX)
screen.fill(BG_RGB)
score = (int(time.time_ns()) - start) // 50000000
text = font.render(str(score), True, (0, 0, 0))
screen.blit(text, (500, 25))
player.show(screen)
player.update_coords(dt)
for obstacle in obstacles:
Expand All @@ -55,7 +87,15 @@ def main():

if player.rect.collidelist(
[obstacle.rect for obstacle in obstacles]) != -1:
game_over()
obstacles.clear()
player.jumping = False
player.rect.update(X_OFFSET, screen.get_height() - PLAYER_HEIGHT,
PLAYER_WIDTH, PLAYER_HEIGHT)
if game_over(screen, score):
start = int(time.time_ns())
else:
game_active = False
next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX)

pg.display.update()

Expand Down