简体   繁体   中英

How do I pause code in an if function in pygame?

How do you make the code stop for a moment before checking for something else? (I'm new to code)

if BonusTime==True and Drop==True:               
    if event.type == pygame.MOUSEBUTTONDOWN:
        window.blit(Fired,(mouseX-12,mouseY-12))
        Cankill=True
        #I want it to delay here
        Cankill=False

There is a cross hair that follows the mouse and when I click it, it fires. The problem is, you can just hold the mouse down and leave the cross hair in one place. Whenever an enemy walks into the cross hair, they die. I need it so even when you hold it will only fire once. I plan to make it delay the if statement, to set "Cankill" to true and then wait a second, after waiting, it should set "Cankill" to false. I've already looked through a lot of other people's questions similar to this and haven't found something I can understand. So if you could please help me find out how to delay it in the way I need.

"Pausing" is not what you want - if you do that, your game will just freeze, and nothing will move (since you are usign the OS mouse pointer, maybe it could move).

Anyway, the function to "pause" inside a game is pygame.time.wait(1000) - the number is in miliseconds.

Waht you actually need is to mark down the time the click was made, continue with the game, and when 1 second has passed, reset the variable back to the other state.

Something along:

last_trigger = 0
while True:
    # game updating code goes here (getting events, and such)
    ...
    if Cankill and pygame.time.get_ticks() - last_trigger > 1000:
        Cankill = False

    if event.type == pygame.MOUSEBUTTONDOWN:
        window.blit(Fired,(mouseX-12,mouseY-12))
        Cankill=True
        last_trigger = pygame.time.get_ticks()

The "get_ticks" call returns the number of miliseconds that passed since pygame was initialized - and is usefull for this time of timers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM