Dec-08-2020, 05:41 PM
I'm trying to make a basic alien invasion game but at the moment my ship won't move left to right. I'm not sure why as it was moving before. Can anyone tell me what I'm doing wrong? Here is my code for the ship:
import pygame
from settings import Settings
class Ship:
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialise the ship and its starting position."""
self.screen = ai_game.screen
self.settings = ai_game.settings
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load(r'C:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\Images\ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
# Store a decimal value for the ship's horizontal position.
self.x = float(self.rect.x)
# Movement flag
self.moving_right = False
self.moving_left = False
def update(self):
"""Update ships position based on movement flags."""
# Update the ships x value not the rect.
if self.moving_right and self.rect.right < self.screen_rect.right:
self.rect.x += self.settings.ship_speed
if self.moving_left and self.rect.left > 0:
self.rect.x -= self.settings.ship_speed
# Update rect object from self.x
self.rect.x = self.x
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)Here is my main code: import sys
import pygame # contains functionality to make a game
from settings import Settings
from ship import Ship
#from alien import Alien
class AlienInvasion:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialise the game and create game resources."""
pygame.init() # initialise the background settings
# which the game needs to work properly
self.settings = Settings()
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
# creates display window with these dimensions
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
#self.alien = Alien(self)
def run_game(self):
"""Start the main loop for the game."""
while True:
self.check_events()
self.ship.update()
self.update_screen()
def check_events(self):
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move ship 1 to the right.
self.ship.moving_right = True
elif event.key == pygame.K_LEFT:
self.ship.moving_left = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
self.ship.moving_right = False
elif event.key == pygame.K_LEFT:
self.ship.moving_left = False
def update_screen(self):
"""Update images on the screenand flip to the new screen."""
# redraw the screen during each pass through the loop
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
#self.alien.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == "__main__":
# make a game instance and run the game
ai = AlienInvasion()
ai.run_game()and here are the settings I'm using:class Settings:
"""A class to store all settings for Alien Invasion"""
def __init__(self):
"""Initialise the games settings."""
self.screen_width = 1000
self.screen_height = 600
self.bg_color = (230, 230, 230)
# Ship settings
self.ship_speed = 1.5
