1

I am fairly new to PyGame and I am creating a space shooter game. Part of this game are powerups and when a specific powerup is picked up by the player, I want the player not to be able to shoot for 3 seconds. Shooting is done by mouse click.

I can pick up the powerup, I know what powerup the player last picked up, but I am struggling with the event. How I am thinking of implementing is:

  1. Can't Shoot power up is picked up -> that's done
  2. Block mouse buttons
  3. Wait 3 seconds, while the rest of the game is still running
  4. Unblock mouse buttons.

I am aware that Python functions, such as wait, won't help.

Any ideas/suggestions? Thanks

1 Answers1

-1

When you call clock.tick() it returns time since the last call. So save that time: dt = clock.tick() and then use that variable to count down your seconds.

Example:

dt = clock.tick() # only call once per iteration
if attack_blocked:
    attack_block_count += dt
    if attack_block_count >= 3000: # dt is in ms
        attack_blocked = False

Example 2:

while True:
    dt = clock.tick(60)
    for event....
        if block_attack_power_up:
            attack_blocked = True
            attack_block_count = 0
    if not attack_blocked:
        # do your attack
    if attack_blocked:
        attack_block_count += dt
        if attack_block_count >= 3000:
            attack_blocked = False
Fredrik
  • 1,389
  • 1
  • 14
  • 32
  • Something like: dt = clock.tick() if dt < 3: dt += 1 ? – Adrian Daniel Culea Feb 05 '16 at 22:36
  • Look at added example in my answer. dt is the time since the last call to clock.tick() in milliseconds. So it is a great way to time all kind of things. Another good advice is to give clock.tick(60) an integer parameter which tells pygame to keep the fps at 60 if you give it 60. – Fredrik Feb 05 '16 at 22:40
  • The thing is that I have something like this: if powerup.type == 'Cannot Shot': . This condition is true only once, when the powerup is picked. – Adrian Daniel Culea Feb 05 '16 at 22:43
  • Yes, then you set attack_blocked = True inside that condition. And in your code when you execute the attack always check if not blocked first. – Fredrik Feb 05 '16 at 22:45
  • Put the dt = clock.tick(60) and remove the second one. Only call clock.tick() once per iteration! Important. Then where the magic happens just set a variable like attack_blocked = True and use that variable to check so it's False before you do attacks. – Fredrik Feb 05 '16 at 22:54
  • Won't dt keep updating? Is there a way to "reset" it to 0 after I have unblocked the mouse? – Adrian Daniel Culea Feb 05 '16 at 23:02
  • dt is always the time since your last call to clock.tick(), you use anorher variable like I showed in the example to do the actual counting with help from the dt variable... – Fredrik Feb 05 '16 at 23:04
  • Check my new example for a full example how to do – Fredrik Feb 05 '16 at 23:10
  • Can't figure it out, sorry... Might be because I'm frustrated by this as well, I don't know. http://pastebin.com/Gg0Wu85d Look here please and add some lines of code that you think will work. Thanks so much for helping me – Adrian Daniel Culea Feb 05 '16 at 23:11
  • Check my new example. – Fredrik Feb 05 '16 at 23:17
  • I have added some code here. Could you look over it? – Adrian Daniel Culea Feb 05 '16 at 23:23
  • The only code you should have inside your "if powerup.type" is the blockAttack =True and blockCount=0 the other code should be outside in the mainloop somewhere liks in my second example. I'm sorry but I don't understand what gets you so confused. If you detect attack block, set attackBlock true and count to 0. Every iteration in the main loop, check if attck is blocked and count it down until it gets to 3 seconds, then set it to false – Fredrik Feb 05 '16 at 23:36
  • Ofcourse the mose events never gets blocked you only block them after the 3 seconds now and then unblock them directly after. However this is not the ideal way to prevent the attack from happening. So instead of blocking the mouse events just check the blockAttack boolean before you actually do an attack, and only attack if that is False. No need to mess with set_blocked. You have a working blockAttack boolean, use that instead! Also I see a typo, it should be blockCount >= 3000 – Fredrik Feb 06 '16 at 00:00
  • I don't have a method to "do an attack" method, that's why I was using the set_blocked events. Also, I don't understand what you mean by block after 3 seconds and then unblock directly after... – Adrian Daniel Culea Feb 06 '16 at 00:08
  • I genuinely have absolutely no clue whatsoever of how to make this work and I really need a working piece of code until morning (6hrs from now) – Adrian Daniel Culea Feb 06 '16 at 00:09
  • Ofcourse you have a line of code somewhere that does your attack, and that's where you should check with the boolean. – Fredrik Feb 06 '16 at 00:11
  • I am checking if event.type == MOUSEBUTTONDOWN I play some sounds, create some bullet sprites etc etc – Adrian Daniel Culea Feb 06 '16 at 00:12
  • Yes and inside that if put "if not attackBlock" before you create the sprites etc – Fredrik Feb 06 '16 at 00:14
  • OMG IT WORKS! I was doing something REALLY REALLY stupid somewhere else. Thanks so much for helping me! – Adrian Daniel Culea Feb 06 '16 at 00:15