How to shoot infinite bullets in game coded in Python?

429 views Asked by At

I've got a really long code for my game, but I'll paste in the part that counts which is the bullet shooting part. The game is essentially a Mortal Kombat-esque game but with flying robots and bullets.

Before the game loop, I first predefined functions for the bullets and robots:

                def robotr(xr,yr):
                    gameDisplay.blit(robrimg, (xr,yr))

                def robotl(xl,yl):
                    gameDisplay.blit(roblimg, (xl,yl))

                def bulletsr(xbr,ybr):
                    pygame.draw.circle(gameDisplay, THECOLORS['orange'],(xbr,ybr), 10)

                def bulletsl(xbl,ybl):
                    pygame.draw.circle(gameDisplay, THECOLORS['orange'],(xbl,ybl), 10)   

Then the numerous variables for the moving objects:

                xr = 929
                yr = 250
                xl = 250
                yl = 250
                ####
                xbr=950
                ybr=300
                xbl=380
                ybl=300
                #####
                xbr_change = 0
                ybr_change = 0
                ybl_change = 0
                xbl_change = 0 
                ####
                xr_change = 0
                yr_change = 0
                xl_change = 0
                yl_change = 0
                robotr_speed = 3000
                robotl_speed = 3000   

After that, the code went for a really long run (about 300-400 lines more), but to the point where I shot the bullets I had an event like this to represent the bullets "moving" with the robot as it moved with the arrow keys.:

                            if event.type == pygame.KEYDOWN:
                            #-------PLAYER 1 MOVEMENT-------
                            #OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
                            if event.key == pygame.K_a:                                    
                                xr_change = -5
                                xbr_change = -5 #I have to change the position of the bullet as well so that it doesn't stay in one place and rather shoots from the robot (duh.)

And then a similar code to SHOOT the bullets:

                                #-------FIRE CANNON MK1-------
                            #OOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
                            if event.key == pygame.K_LSHIFT:
                                bulletsfx=pygame.mixer.Sound ('boomchickchickmp3.ogg')
                                bulletsfx.play()  
                                bulletsfx.set_volume(0.2)
                                xbl_change = 5
                                if xbl_change == 5:
                                    bulletsl(xbl,ybl)
                                    xbl=xl

Of course, I defined such codes multiple times for the robot on the right as well.

Near the end of my program (this is as a short form of my end as I had extra variables so things wouldn't fall off the screen, but disregard that for now) I had a code like this so I could redefine the changes back to "0" so that the movements could be reused and so forth:

                            if event.type == pygame.KEYUP:
                            #Player 1 events
                            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_RCTRL or event.key == pygame.K_DELETE:  #or event.key == pygame.K_RSHIFT:
                                xr_change = 0
                                yr_change = 0
                                xbr_change= 0
                                ybr_change= 0                                   
                            #Player 2 events
                            if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_w or event.key == pygame.K_s or event.key == pygame.K_LCTRL or event.key == pygame.K_f: #or event.key == pygame.K_LSHIFT:
                                xl_change = 0
                                yl_change = 0      
                                xbl_change= 0
                                ybl_change= 0





                    ##
                    xr += xr_change
                    yr += yr_change
                    xl += xl_change
                    yl += yl_change
                    ##
                    xbr += xbr_change
                    ybr += ybr_change
                    xbl += xbl_change
                    ybl += ybl_change                       
                    ##      
                    bulletsr(xbr,ybr)
                    bulletsl(xbl,ybl)                                
                    robotr(xr,yr)
                    robotl(xl,yl)  

                    pygame.display.update()
                    clock.tick(320)

Now my problem is that when I shoot the bullets, it shoots, but I can only shoot it once until it goes off the screen of my program (It's okay, I made it so it returns to the robots once it goes off the screen as well). Otherwise, if I keep hitting the key to shoot, it just returns the bullet coordinates back to where it originally should be (as in I shoot, and if I shoot again, I can see the bullet disappearing to return to its original location).

There is not an infinite number of bullets and I want to know how I can modify the function and some of the variables (probably) to make it so that happens.

1

There are 1 answers

0
Deepak On

You need to use sprites in your code which is missing. It can be done without sprites also but it will be difficult task.

  1. Make a class of bullets so you can make different objects of this.
  2. After you just make a instance to the bullet object every time shoot button is pressed and keep updating it.
  3. In update method you will make bullet move.
  4. Last step is to destroy the bullet object once its out of your screen. One easy way is to put an if statement to check if certain x or y length is achieved and if so delete that sprite or object (bullet in this case).

Here is an example of bullet class I used. In this the char is assumed to be on bottom of screen and shoot bullets upwards as y += -10

class Bullet(pygame.sprite.Sprite):
def __init__(self):
    super(Bullet, self).__init__()
    self.image = pygame.Surface([10,40])
    self.image.fill(RED)
    self.rect = self.image.get_rect()

def update(self):
    self.rect.y -= 10

if you still need to see the full picture you can check out my code here : https://github.com/deepaksandhu/SpaceTurds/blob/master/game.py

also here is an excellent tutorial of making bullets: http://programarcadegames.com/python_examples/show_file.php?file=bullets.py