Alan Bagel
Alan Bagel

Reputation: 911

Game crashing when game started

I am trying to make a spaceship game, however, when I added the part which allows you to fire bullets, the game crashed immediately crashed when I run the game.

Here is my current code (Some parts omitted or replaced by --snip--)

class Spaceship(pygame.sprite.Sprite):
    def __init__(self, s, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.screen = s
        self.x, self.y = x, y
        self.image = pygame.image.load("C:/eqodqfe/spaceship.png")
        self.image = pygame.transform.scale(self.image, (175, 175))
        self.rect = self.image.get_rect()
        self.rect.center = (self.x, self.y)

    def update(self):
        self.rect.center = (self.x, self.y)


class Bullet(pygame.sprite.Sprite):
    def __init__(self, s, x, y):
        pygame.sprite.Sprite.__init__(self)

        self.screen = s
        self.x, self.y = x, y
        self.image = pygame.image.load("C:/eqodqfe/bullet.png")
        self.image = pygame.transform.scale(self.image, (100, 100))
        self.rect = self.image.get_rect()
        self.rect.center = (self.x, self.y)

    def update(self):
        self.rect.center = (self.x, self.y)

    def fire(self):
        while self.y <= 490:
            self.y -= 5


spaceship = Spaceship(screen, 400, 400)
bullet = Bullet(screen, 0, 0)

running = True
while running:
    --snip--

    key = pygame.key.get_pressed()
    if key[pygame.K_a]:
        spaceship.x -= 0.5
    elif key[pygame.K_d]:
        spaceship.x += 0.5
    --snip--

    spaceship.update()
    bullet.update()

    screen.fill((255, 255, 255))
    screen.blit(spaceship.image, spaceship.rect)
    screen.blit(bullet.image, bullet.rect)

    bullet.x = spaceship.x
    bullet.y = spaceship.y-20

    if pygame.mouse.get_pressed():
        bullet.fire()

    pygame.display.update()

Does anybody know what went wrong?

Upvotes: 1

Views: 71

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

What do you expect by

while self.y <= 490:
   self.y -= 5

This is an infinite loop.

To make a bullet you have to create an instance of Bullet when mouse button is pressed. However you have to use the MOSUEBUTTONDOWN event. pygame.mouse.get_pressed() returns a list of Boolean values ​​that represent the state (True or False) of all mouse buttons. The state of a button is True as long as a button is held down. The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released.

Move an kill the bullet in update. update is continuously invoked in the application loop:

class Bullet(pygame.sprite.Sprite):
    # [...]

    def update(self):
        self.y -= 5
        self.rect.center = (self.x, self.y)
        if self.y < 0:
            self.kill()        

Create a Bullet when the MOSUEBUTTONDOWN occurs:

bullets = pygame.sprite.Group()

running = True
while running:
    # [...]

    for event in pygame.event.get():
       if event.type == pygame.QUIT:
            running = False
       if event.type == MOSUEBUTTONDOWN
           bullet = Bullet(screen, spaceship.x, spaceship.y-20)
           bullets.add(bullet)

    bullets.update()

    # [...]
    
    bullets.draw()
    pygame.display.update()

See also:

Upvotes: 2

Related Questions