Click here to Skip to main content
15,881,764 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Doing a space invaders 2d game in pygame using pycharm. Got to a part where the ship moves correctly part of the time and then doesn't. don't know if it's the code or my computer.
Moves correctly on KEYDOWN part of the time or have to press it several times to get it to move, when released will stop part of the time but if continued to press and then released won't stop moving on KEYUP. here's the code, I need it to only move on KEYDOWN and continuously if held but stop on KEYUP. right now it's very unreliable. im using a bmp image for the ship, dont know if png would work better maybe. Theres a lot of code, 3 different files that import to alien_invasion.py, the main.py file.

alien_invasion.py
Python
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf


def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    ship = Ship(ai_settings, screen)

    while True:
        gf.check_events(ship)
        ship.update()
        gf.update_screen(ai_settings, screen, ship)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        screen.fill(ai_settings.bg_color)
        ship.blitme()
        pygame.display.flip()


run_game()

-----------------

game_functions.py
Python
import sys
import pygame


def check_events(ship):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                ship.moving_left = True

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = False
            elif event.key == pygame.K_LEFT:
                ship.moving_left = False



def update_screen(ai_settings, screen, ship):
    screen.fill(ai_settings.bg_color)
    ship.blitme()
    pygame.display.flip()

--------

ship.py
Python
import pygame


class Ship():
    def __init__(self, ai_settings, screen):
        self.screen = screen
        self.ai_settings = ai_settings

        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
        self.center = float(self.rect.centerx)

        self.moving_right = False
        self.moving_left = False

    def update(self):
        if self.moving_right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left:
            self.center -= self.ai_settings.ship_speed_factor
        self.rect.centerx = self.center
    def blitme(self):
        self.screen.blit(self.image, self.rect)

------------

settings.py
Python
class Settings():

    def __init__(self):
        self.ship_speed_factor = 1.5
        self.screen_width = 1000
        self.screen_height = 600
        self.bg_color = (230, 230, 230)


What I have tried:

reworking through the book and the code
Posted
Updated 19-Jan-22 20:17pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900