0

I've been following along with the pygame project in 'Python Crash Course.' Copying the exact code because I am very new to python. When I run my code, the game window opens up, and the image appears in the correct initial place. I cannot get the image to move as it is supposed to. When I use the code that is commented out in the while loop to move the image, it works. However, as I continue to follow the steps in the book, it moves that functionality into separate files that are imported into the main file. I have copied it exactly, but the image will not move.

import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
def run_game():
    # main game loop
    ai_settings = Settings()
    pygame.init()
    screen = pygame.display.set_mode((ai_settings.screen_width,
                                      ai_settings.screen_height))
    ship = Ship(screen)

    pygame.display.set_caption("Alien Invasion!")

    while True:
        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.rect.centerx += 1
            elif event == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    ship.moving_right = True
            elif event == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    ship.moving_right = False

            gf.check_events(ship)
            ship.update()
            gf.update_screen(ai_settings, ship, screen)
run_game()
Onic Team
  • 1,620
  • 5
  • 26
  • 37
Luc Larson
  • 13
  • 1
  • 1
  • 3
  • There are many things missing in your example, but it looks like you just have to change `event` to `event.type` in the `elif event == pygame.KEYDOWN:` and `elif event == pygame.KEYUP:` lines. There might be more errors, but to find them you have to post a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). – skrx Jul 14 '17 at 07:47
  • Did you get this figured out yet? – japhyr Jul 28 '17 at 13:18

0 Answers0