Skip to content
Advertisement

Pygame How to check for second press of a key

Hi guys i’m making a game in python using pygame. I have a piece of code that shoots lasers from a ship in a space themed game. Here is the code:

JavaScript

Well this piece of code shoots two lasers from the ship,

enter image description here

like this.

But i don’t want that. I want to make if player presses space once shoot the left laser if player presses space again than shoot the right laser and when he presses space again it will shoot left laser again. I hope you did understand.

Advertisement

Answer

You need separate positions and states for the left and the right laser

JavaScript

You will also need a variable that indicates which laser will fire next

JavaScript

Shoot the next laser when SPACE is pressed a nd change the next laser:

JavaScript

Move and draw both lasers dependent on its state

JavaScript

Minimal example:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement