Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have made it so if you click you go to the middle of the screen but i only want that to happen if you are on the death screen wich happens when you leave the screen how do i make it so the click only works if the death screen is showing

What I have tried:

import pygame
import random

SCREEN_WIDTH = 800
SCREEN_HIEGHT = 600

gravity = 4

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HIEGHT))

bg_image = pygame.image.load('Background.png')
bg_rect = bg_image.get_rect()

losing_image = pygame.image.load('losing screen.png')
losing_rect = losing_image.get_rect()

player_image = pygame.image.load('Character.png')
player_rect = player_image.get_rect()
player_rect.x = 30
player_rect.y = 300

dead = False

clock = pygame.time.Clock()
run = True
score = 0
while run:
     
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            player_rect.y = 300
            player_rect.x= 30
        if event.type == pygame.QUIT:
            run = False
             
    key = pygame.key.get_pressed()
    if key[pygame.K_w]:
        player_rect.y -= 7
    
    if player_rect.y < 790:
        player_rect.y += gravity
    
    
    screen.fill((0,0,0))
    screen.blit(bg_image, bg_rect)
    screen.blit(player_image, player_rect, )
    if player_rect.y >= 580:
        dead = True
        screen.blit(losing_image, losing_rect)
    
        

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

pygame.quit()
Posted

1 solution

You cannot control when events are raised. You can only setup a condition where, when your event handler is called, you check your game state (a flag in your case) that the "death screen" is showing, then proceed accordingly. You'll have to set the flag (set to true) when your "death screen" is showing, then reset that flag (set to false) when appropriate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900