I have been having trouble making a pause and resume function in my game, i can click the pause button once and it will work and bring up the resume button but when i click the resume button nothing happens, i believe it is caused by my if else statement which locks the game into the paused state and doesn't allow it to come back out but i don't know how to solve this issue.
paused = False
resumed = False
def run_game():
global paused
if pause_button.clicked:
paused = True
resume_button.resume_button_visible = True
resume_button.draw()
print(paused)
else:
boxer_1.update()
boxer_2.update()
draw_health_bar(boxer_1.health, 50, 30)
draw_health_bar(boxer_2.health, 650, 30)
boxer_1.move(SCREEN_WIDTH, screen, boxer_2)
boxer_2.move_enemy(SCREEN_WIDTH, screen, boxer_1)
boxer_1.draw(screen)
boxer_2.draw(screen)
paused = False
resume_button.resume_button_visible = False
pause_button.draw()
print(paused)
run = True
while run:
draw_bg()
clock.tick(FPS)
start_button.draw()
exit_button.draw()
if start_button.clicked or resume_button.clicked:
run_game()
exit_button.visible = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
print('if x button is pressed game will stop')
run = False
sys.exit()
if exit_button.clicked:
run = False
print('game will stop')
pygame.display.update()
sys.exit()
Here is the code for the button
import pygame
# Screen width, and height in pixels
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 500
# Set screen size and caption
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Fighter Game Menu!')
class Button:
def __init__(self, x, y, image, scale):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), (int(height * scale))))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False
self.visible = True
def draw(self):
action = False
pos = pygame.mouse.get_pos()
left_click = pygame.mouse.get_pressed()[0]
if self.visible:
screen.blit(self.image, (self.rect.x, self.rect.y))
if self.rect.collidepoint(pos):
if left_click == 1 and self.clicked == False:
self.clicked = True
self.visible = False
action = True
elif left_click == 0:
self.clicked = False
return action
class Pause(Button):
def __init__(self, x, y, image, scale):
Button.__init__(self, x, y, image, scale)
self.resume_button_visible = False
def draw(self):
pause_action = False
pos = pygame.mouse.get_pos()
left_click = pygame.mouse.get_pressed()[0]
if self.resume_button_visible:
screen.blit(self.image, (self.rect.x, self.rect.y))
if self.rect.collidepoint(pos):
if left_click == 1 and self.clicked == False:
self.clicked = True
pause_action = True
elif left_click == 1 and self.clicked == True:
self.clicked = False
pause_action = False
elif left_click == 0:
self.clicked = False
return pause_action
Any help would be greatly appreciated.