Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a BUTTON for Pause/Play #7

Closed
wants to merge 5 commits into from
Closed
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
129 changes: 80 additions & 49 deletions runner_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from sys import exit
from random import randint, choice

def toggle_pause():
global game_paused
game_paused = not game_paused
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -42,7 +45,7 @@ def update(self):
class Obstacle(pygame.sprite.Sprite):
def __init__(self,type):
super().__init__()

if type == 'fly':
fly_1 = pygame.image.load('graphics/fly/fly1.png').convert_alpha()
fly_2 = pygame.image.load('graphics/fly/fly2.png').convert_alpha()
Expand Down Expand Up @@ -85,13 +88,15 @@ def collision_sprite():
return False
else: return True


pygame.init()
screen = pygame.display.set_mode((800,400))

# Create the game window
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 50)
game_active = False
game_paused = False
start_time = 0
score = 0

Expand All @@ -104,57 +109,83 @@ def collision_sprite():
ground_surface = pygame.image.load('graphics/ground.png').convert()

player_stand = pygame.image.load('graphics/player/player_stand.png').convert_alpha()
player_stand = pygame.transform.rotozoom(player_stand,0,2)
player_stand_rect = player_stand.get_rect(center = (400,200))
player_stand = pygame.transform.rotozoom(player_stand, 0, 2)
player_stand_rect = player_stand.get_rect(center=(400, 200))

game_name = test_font.render('Snail Hurdle Hop',False,(111,196,169))
game_name_rect = game_name.get_rect(center = (400,80))
game_name = test_font.render('Snail Hurdle Hop', False, (111, 196, 169))
game_name_rect = game_name.get_rect(center=(400, 80))

game_message = test_font.render('Press space to run',False,(111,196,169))
game_message_rect = game_message.get_rect(center = (400,330))
game_message = test_font.render('Press space to run', False, (111, 196, 169))
game_message_rect = game_message.get_rect(center=(400, 330))

obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer,1500)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()

if game_active:
if event.type == obstacle_timer:
obstacle_group.add(Obstacle(choice(['fly','snail','snail','snail'])))

else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
game_active = True
start_time = int(pygame.time.get_ticks() / 1000)


if game_active:
screen.blit(sky_surface,(0,0))
screen.blit(ground_surface,(0,300))
score = display_score()

player.draw(screen)
player.update()
pygame.time.set_timer(obstacle_timer, 1500)

obstacle_group.draw(screen)
obstacle_group.update()
# Create a transparent button surface
button_surface = pygame.Surface((80, 40), pygame.SRCALPHA)
pygame.draw.rect(button_surface, (255, 0, 0, 128), pygame.Rect(0, 0, 80, 40)) # Transparent red background

game_active = collision_sprite()
else:
screen.fill((94,129,162))
screen.blit(player_stand,player_stand_rect)
# Draw the button text on the transparent surface
pause_font = pygame.font.Font(None, 36)
pause_text = pause_font.render("Pause", True, (255, 255, 255))
text_rect = pause_text.get_rect(center=(button_surface.get_width() // 2, button_surface.get_height() // 2))
button_surface.blit(pause_text, text_rect)

score_message = test_font.render(f'Your score: {score}',False,(111,196,169))
score_message_rect = score_message.get_rect(center = (400,330))
screen.blit(game_name,game_name_rect)
# Define the position of the pause button
pause_button_rect = button_surface.get_rect(topleft=(700, 20))

if score == 0: screen.blit(game_message,game_message_rect)
else: screen.blit(score_message,score_message_rect)

pygame.display.update()
clock.tick(60)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()

if game_active:
if event.type == obstacle_timer:
obstacle_group.add(Obstacle(choice(['fly', 'snail', 'snail', 'snail'])))
else:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
game_active = True
start_time = int(pygame.time.get_ticks() / 1000)

# Check for mouse clicks on the pause button
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if pause_button_rect.collidepoint(event.pos):
toggle_pause()

if game_active and not game_paused:
screen.blit(sky_surface, (0, 0))
screen.blit(ground_surface, (0, 300))
score = display_score()

player.draw(screen)
player.update()

obstacle_group.draw(screen)
obstacle_group.update()

game_active = collision_sprite()

else:
screen.fill((94, 129, 162))
screen.blit(player_stand, player_stand_rect)

score_message = test_font.render(f'Your score: {score}', False, (111, 196, 169))
score_message_rect = score_message.get_rect(center=(400, 330))
screen.blit(game_name, game_name_rect)

if score == 0:
screen.blit(game_message, game_message_rect)
else:
screen.blit(score_message, score_message_rect)

if game_paused:
pause_message = test_font.render('Paused', False, (255, 0, 0))
pause_message_rect = pause_message.get_rect(center=(400, 200))
screen.blit(pause_message, pause_message_rect)

# Draw the transparent button surface
screen.blit(button_surface, pause_button_rect.topleft)

pygame.display.update()
clock.tick(60)