I am trying to do the exercise for the sideways shooter exercise in the book called “Python Crash Course”. I dont know how to postion the alien sprites to the right and away from the ship. Can you help? Thanks in advance.
# this is the code for sideways_alien.py
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
"""class to manage Alien"""
def __init__(self, ss2_game):
"""initialize Alien and set its position"""
super().__init__()
self.screen = ss2_game.screen
self.settings = ss2_game.settings
# Load the alien image and set its rect attribute.
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# Start each new alien near the top right of the screen
self.rect.x = 900
self.rect.y = 0
# self.screen.get_rect().right - self.rect.width
# Store the alien's exact vertical position
self.y = float(self.rect.y)
def check_edges(self):
"""Return True if alien is at edge of screen(top and bottom of screen)"""
screen_rect = self.screen.get_rect()
if self.rect.bottom >= screen_rect.bottom or self.rect.top <= 0:
return True
def update(self):
"""Move the alien down or up """
self.y += (self.settings.alien_speed * self.settings.fleet_direction)
self.rect.y = self.y
# this is the code for sideways_bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
"""class to manage bullet fired from ship"""
def __init__(self, ss2_game):
"""initialize bullet and assign to position at ship"""
super().__init__()
self.screen = ss2_game.screen
self.settings = ss2_game.settings
self.color = self.settings.bullet_color
# Create a bullet rect at (0, 0) and then set correct position.
self.rect = pygame.Rect(0, 0, self.settings.bullet_width, self.settings.bullet_height)
self.rect.midleft = ss2_game.ship.rect.midleft
# Store the bullet's position as decimal value
self.x = float(self.rect.x)
def update(self):
"""Move the bullet across the screen"""
# Update the decimal position of the of the bullet
self.x += self.settings.bullet_speed
# Update the rect position of the bullet
self.rect.x = self.x
def draw_bullet(self):
"""Draw the bullets to the screen"""
pygame.draw.rect(self.screen, self.color, self.rect)
# this is the code for sideways_settings.py
class Settings:
"""Settings for the siddeways shooter part 2 game"""
def __init__(self):
# settings for screen
self.screen_width = 1200
self.screen_height = 620
self.rbg_color = (245, 245,245)
# settings for ship
self.ship_speed = 1.6
# settings for bullet
self.bullet_speed = 1.6
self.bullet_width = 8
self.bullet_height = 500
self.bullet_color = (60, 60, 60)
self.bullets_allowed = 4
# settings for alien
self.alien_speed = 0.8
self.fleet_drop_sideways_speed = 5
# fleet direction +1 is down screen and -1 is up screen
self.fleet_direction = 1
# this is the code for sideways_ship.py
import pygame
class Ship:
"""class to manage ship in the sideways shooter 2 game"""
def __init__(self, ss2_game):
"""initialize ship and give it its attributes and methods and set its starting position"""
self.settings = ss2_game.settings
self.screen = ss2_game.screen
self.screen_rect = ss2_game.screen.get_rect()
# load the ship and get its rect
self.image = pygame.image.load("images/ship.bmp")
self.rect = self.image.get_rect()
# start each new ship at the mid left side of the screen
self.rect.midleft = self.screen_rect.midleft
# Store a decimal value for the ships horizontal position
self.y = float(self.rect.y)
# movement flags
self.moving_down = False
self.moving_up = False
def update(self):
"""update the ships position based on its movement flag"""
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.y += self.settings.ship_speed
if self.moving_up and self.y > 0:
self.y -= self.settings.ship_speed
# assign position to its rect
self.rect.y = self.y
def blitme(self):
"""Draw the ship to its current location"""
self.screen.blit(self.image, self.rect)
# this is the main code for sideways_shooter_2_game.py
import sys
import pygame
from sideways_settings import Settings
from sideways_ship import Ship
from sideways_bullet import Bullet
from sideways_alien import Alien
class SidewaysShooter2:
""""overall class to manage Sideways Shooter 2 game assests and behaviour"""
def __init__(self):
"""initialize the game and create game resources"""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption = ("sideways shooter part 2 game")
self.ship = Ship(self)
self.bullets = pygame.sprite.Group()
self.aliens = pygame.sprite.Group()
self._create_fleet()
def run_game(self):
"""Start the main loop for the game"""
while True:
self._check_events()
self.ship.update()
self._update_bullets()
self._update_aliens()
self._update_screen()
def _check_events(self):
"""Check for keyboard events or 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)
def _check_keydown_events(self, event):
"""respond to keypresses"""
if event.key == pygame.K_DOWN:
self.ship.moving_down = True
elif event.key == pygame.K_UP:
self.ship.moving_up = True
elif event.key == pygame.K_q:
sys.exit()
elif event.key == pygame.K_SPACE:
self._fire_bullet()
def _check_keyup_events(self, event):
"""respond when key is up"""
if event.key == pygame.K_DOWN:
self.ship.moving_down = False
elif event.key == pygame.K_UP:
self.ship.moving_up = False
def _fire_bullet(self):
"""Create a new bullet and add it to the bullets group."""
if len(self.bullets) < self.settings.bullets_allowed:
new_bullet = Bullet(self)
self.bullets.add(new_bullet)
def _update_bullets(self):
"""update position of bullets and get rid of old bullets"""
# Update bullet positions.
self.bullets.update()
# Get rid of bullets that have disappeared
for bullet in self.bullets.copy():
if bullet.rect.right >= self.settings.screen_width:
self.bullets.remove(bullet)
self._check_bullet_alien_collisions()
def _check_bullet_alien_collisions(self):
"""Respond to bullet-alien collisions"""
# Remove any bullets and aliens that have collided
collisions = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
if not self.aliens:
# Destroy existing bullets and create new fleet
self.bullets.empty()
self._create_fleet()
def _update_aliens(self):
"""Check if the fleet is at an edge, then update the position of all aliens in the fleet"""
#self._check_fleet_edges()
self._check_fleet_edges()
self.aliens.update()
def _create_fleet(self):
"""Create the fleet of aliens"""
# Create an alien and find the number of aliens in a row
# Spacing between each alien is equal to one alien width
alien = Alien(self)
alien_width, alien_height = alien.rect.size
available_space_y = self.settings.screen_height - (1 * alien_height)
number_aliens_y = available_space_y // (2 * alien_height)
# Determine the number of colums of aliens that fit on the screen
ship_width = self.ship.rect.width
available_space_x = (self.settings.screen_width -
(3 * alien_width) - ship_width)
number_columns = available_space_x // (2 * alien_width)
# Create the full fleet of aliens
for column_number in range(number_columns):
for alien_number in range(number_aliens_y):
self._create_alien(alien_number, column_number)
def _create_alien(self, alien_number, column_number):
"""Create an alien and place it in a row"""
alien = Alien(self)
alien_width, alien_height = alien.rect.size
alien.y = alien_height + 2 * alien_height * alien_number
alien.rect.y = alien.y
alien.rect.x = alien.rect.width + 2 * alien.rect.width * column_number
self.aliens.add(alien)
def _check_fleet_edges(self):
"""Respond appropriately if any aliens have reached an edge"""
for alien in self.aliens.sprites():
if alien.check_edges():
self._change_fleet_direction()
break
def _change_fleet_direction(self):
"""Drop the entire fleet and change fleet direction"""
for alien in self.aliens.sprites():
alien.rect.x -= self.settings.fleet_drop_sideways_speed
self.settings.fleet_direction *= -1
def _update_screen(self):
"""Update images on screen and flip to new screen"""
self.screen.fill(self.settings.rbg_color)
self.ship.blitme()
for bullet in self.bullets.sprites():
bullet.draw_bullet()
self.aliens.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
ss2 = SidewaysShooter2()
ss2.run_game()
# How do I position the fleet of sprites to the right and away from the ship? Thank you!!