Here is the main code for the game. It's basically just a shooter that moves vertically on the left of the screen. However, once I added in the aliens, the game just slowed down a lot, and is basically unplayable. What can I do to make it faster? I believe that the issue started once I added the aliens, maybe I have included something in the While loop that makes it work too hard every single loop? Because when I delete the self.aliens.draw(self.screen) the game works perfectly fine. I also tried changing the image for the aliens and it seems to work fine, could it be that the image is too high quality??
import pygame
import sys
from shooter_bien import Shooter
from shooter_bullet import Bullet
from alien_shooter import Alien
class SidewayShooter():
"""Class to manage game."""
def __init__(self):
"""Initialize game and resources."""
pygame.init()
self.WID = 1200
self.HEI = 700
self.bg = pygame.image.load('bga.jpg')
self.screen = pygame.display.set_mode((self.WID, self.HEI))
pygame.display.set_caption("Sideways Shooter")
self.shooter = Shooter(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._create_grid()
def run_game(self):
"""Main loop for the game."""
while True:
self._check_events()
self._update_screen()
self._update_bullets()
def _check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
sys.exit()
if event.key == pygame.K_UP:
self.shooter.moving_up = True
if event.key == pygame.K_DOWN:
self.shooter.moving_down = True
if event.key == pygame.K_SPACE:
self._create_bullet()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.shooter.moving_up = False
if event.key == pygame.K_DOWN:
self.shooter.moving_down = False
def _create_bullet(self):
"""Create new bullet and add to group."""
bullet = Bullet(self)
self.bullets.add(bullet)
def _update_bullets(self):
"""Update bullets on screen and delete off screen."""
self.bullets.update()
for bullet in self.bullets.copy():
if bullet.rect.right >= self.WID:
self.bullets.remove(bullet)
def _create_grid(self):
"""Create alien grid."""
alien = Alien(self)
alien_width, alien_height = alien.rect.size
amount_aliens_x = (self.WID // alien_width - 5) // 2
amount_aliens_y = (self.HEI // alien_height) // 2
for row_number in range(amount_aliens_x):
for alien_number in range(amount_aliens_y):
self._create_alien(alien_number, row_number)
def _create_alien(self, alien_number, row_number):
"""Create an alien and add to grid."""
alien = Alien(self)
alien_width, alien_height = alien.rect.size
alien.rect.centerx = (row_number + 3) * alien_width * 2
alien.rect.centery = alien_number * alien_height * 2 + alien_height
self.aliens.add(alien)
def _update_screen(self):
"""Update bg and shooter."""
self.screen.blit(self.bg, (0, 0))
self.shooter.blitme()
self.shooter.update()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
ss = SidewayShooter()
ss.run_game()