pygame moving background image

Viewed 28

I added a moving background image into my modified Alien Invasion game from the python crash course. Everything is blitted on the screen but now I can't interact with anything, I can't click the play button or move the ship.

code:

def _update_screen(self):
    """Update images on the screen, and flip to the new screen"""

    i = 0
    runing = True

    while runing:
        self.ship.blitme()
        self.screen.blit(self.bg_img, (i,0))
        self.screen.blit(self.bg_img,(self.settings.screen_width+i, 0))
        if (i == -self.settings.screen_width):
            self.screen.blit(self.bg_img,(self.settings.screen_width+i,0))
            i = 0
        i -= 1
        for event in pygame.event.get():
            if event.type == QUIT:
                runing = False
                exit()

        self.ship.blitme()
        self.ship2.blitme()
        for bullet in self.bulletss.sprites():
            bullet.draw_bullet()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.aliens.draw(self.screen)


        #Draw the score information.
        self.sb.show_score()

        #Draw the play button if the game is inactive.
        if not self.stats.game_active:
            self.play_button.draw_button()
        pygame.display.flip()

game loop:

            """Store the main loop for the game."""
            while True:
                self._check_events()

                if self.stats.game_active:
                    self.ship.update()
                    self.ship2.update() 
                    self._update_bullets()
                    self._update_aliens()

                self._update_screen()

check_events method:

def _check_events(self):
    """Respond to keypresses and mouse events."""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            self._check_keydown_events(event)   
        elif event.type == pygame.KEYUP:
            self._check_keyup_events(event)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_pos = pygame.mouse.get_pos()
            self._check_play_button(mouse_pos)

            

EDIT: I managed to solve the problem. Just in case someone else needs help with something similar I will post what I came up with: Removed the while loop from _update_screen and implemented the code in the main game loop in run_game like this:

def run_game(self):
        """Store the main loop for the game."""

        running = True
        i = 0
        while running:

            self.screen.blit(self.bg_img, [0,i])
            self.screen.blit(self.bg_img, [0, -self.settings.screen_height+i])
            if i == self.settings.screen_height:
                i = 0
            i += 1

            self._check_events()
            
            if self.stats.game_active:
                self.ship.update()
                self.ship2.update() 
                self._update_bullets()
                self._update_aliens()
        
            self._update_screen()
0 Answers
Related