1

I'm making my own space invaders game and so far I've been able to move my ship around using the mouse. However, I still can't shoot. Here's my game loop.

def game_loop():
    x=0
    y=0


    xlist=[]
    ylist=[]



    while True:


     mouseclk=pygame.mouse.get_pressed()

        game_display.fill(white)
        for event in pygame.event.get():

            if event.type==pygame.QUIT:
                pygame.quit()
                quit()


            x, y = pygame.mouse.get_pos()
            xlist.append(x)
            ylist.append(y)


            if x>=display_width-40:
                x=display_width-40

            if y>=display_height-48:
                y=display_height-48



            if pygame.mouse.get_focused()==0:
                game_display.blit(spaceship, (x, y))

            elif pygame.mouse.get_focused()==1:
                game_display.blit(spaceshipflames, (x, y))


            pygame.display.update()


            if pygame.mouse.get_focused()==0:
                pause()


        clock.tick(500)

I've tried using the following code inside my game loop:

if mouseclk[0]==1:
        shoot.play()
        while True:    

            pygame.draw.circle(game_display, white, (x+20, y-2), 5)
            pygame.draw.circle(game_display, red, (x+20, y-7), 5)

            y-=5



            if y<=0:

                break

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

But the end result is very glitchy and doesn't allow me to shoot multiple bullets without making the game choppy.

Is there a way to run both loops at the same time, or a completely different way to implement shooting?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Shell1500
  • 330
  • 1
  • 5
  • 14
  • 1
    Where have you defined `shoot`? You need to post a fully reproducible example. Right now, it's anyone's best guess how your code is actually structured, and they won't be able to point out the issue. – rahlf23 Aug 24 '18 at 19:24
  • Possible duplicate of [How to create bullets in pygame?](https://stackoverflow.com/questions/21567250/how-to-create-bullets-in-pygame) – skrx Aug 24 '18 at 21:14

1 Answers1

3

I'd recommend making use of classes (especially for games) and splitting up your code into smaller functions.

When making a game, each class should represent some type of object in the game, for example a ship or a bullet here. Using classes should help with this problem of multiple bullets causes glitches.

Breaking into smaller functions will make your code much easier to read and update as it grows. Try to stick to the Single Responsibility Principle as much as possible.

How you might implement shooting with these things in mind:

bullets = []

class Bullet:
    def __init__(self, position, speed):
        self.position = position
        self.speed = speed

    def move(self):
        self.position[1] += self.speed

class Ship:
    def __init__(self, position, bullet_speed):
        self.position = position
        self.bullet_speed = bullet_speed

    def shoot(self):
        new_bullet = Bullet(self.position, self.bullet_speed)
        bullets.append(new_bullet)

Where the position variables have the form [x,y]. Then to move your bullets forward, put this line somewhere in your main game loop:

for bullet in bullets:
    bullet.move()

And loop over all the bullets and draw each to the screen to render them.

This isn't the most detailed example, but hopefully it's enough to get you going in the right direction.

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
  • In this code where are you drawing(blitting) the bullet to the screen? – Shell1500 Aug 29 '18 at 13:38
  • I didn't write a method for that, but my advice would be for each class to have a `draw` method and then on each iteration of your main loop, call draw on every object, similar to how I have shown calling `move` on each `bullet`. – Henry Woody Aug 29 '18 at 18:34