Python Forum
[PyGame] Ammo is random, but it shouldn't
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Ammo is random, but it shouldn't
#4
(Oct-04-2019, 02:32 PM)Piethon Wrote: I've debugged it and it watched the self.BLOWPIPE_AMMO
At the beginning it was 20.
After shooting once it was 15
After shooting twice it was 9
After shooting three times it was 3.
I could only shoot 3 times. I have no idea why. I'll look through my code and look if I'll find something. I've not, I'll put it on Github.

Then this probably is the cause:
    def get_keys(self):
                if keys[pg.K_SPACE]:
                    if self.weapon == 'shurikan' and self.shoot_ammo == True:
                        self.SHURIKAN_AMMO -= 1
Is this ran in the event phase or every frame (update phase)? Is keys all pressed keys? If so then you need to switch it to only under the event of space bar pressed so it only does it once. If it is all keys pressed, then even for a fraction of a keyboard press this is executing multiple times.

Your game is running lets say at 60 frames per second. If you run that code every frame is it running 60 times per second. Even if you have tapped it quickly, will result in running numerous times (5-6) which seems to be your case. So i am assuming you did not put that in the event loop? For example this code should be under
for event in pygame.event.get():
    if event == pygame.KEYDOWN:
        if event.key == pygame.SPACE:
            shoot()
This will restrict it to doing it only once instead of being held down.

If your not sure what i mean. What is the code where get_keys is executed? And what is keys defined as?
Recommended Tutorials:
Reply


Messages In This Thread
Ammo is random, but it shouldn't - by Piethon - Oct-04-2019, 10:29 AM
RE: Ammo is random, but it shouldn't - by metulburr - Oct-04-2019, 11:12 AM
RE: Ammo is random, but it shouldn't - by Piethon - Oct-04-2019, 02:43 PM
RE: Ammo is random, but it shouldn't - by metulburr - Oct-04-2019, 02:54 PM
RE: Ammo is random, but it shouldn't - by Piethon - Oct-05-2019, 08:57 AM
RE: Ammo is random, but it shouldn't - by metulburr - Oct-05-2019, 11:36 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020