0
import sys

import pygame


def check_events(ship):

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            # Move the ship to the right.
            ship.moving_right = True

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

def update_screen(ai_settings, screen, ship):
    '''Update images on the screen and flip to the new screen.'''

Error:

File "/home/mark/python_work/game_functions.py", line 8
        elif event.type == pygame.KEYDOWN:
           ^
    SyntaxError: invalid syntax
    [Finished in 0.2s with exit code 1]
1
  • 2
    You can't have an elif without an if. Change the first one to an if. Commented Feb 7, 2019 at 2:53

3 Answers 3

2

In your function, your first condition should start with an if.

def check_events(ship):

    if event.type == pygame.KEYDOWN:  # <--- note the change here
        if event.key == pygame.K_RIGHT:
            # Move the ship to the right.
            ship.moving_right = True

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT:
            ship.moving_right = False
Sign up to request clarification or add additional context in comments.

Comments

0

You have an elif without an if preceding it. Just change the first elif to an if.

1 Comment

Okay, Thank you. I will try that and see how it goes.
0

You cannot use elif before an if statement. Elif cannot be used without an if statement. This is the right syntax. import sys

import pygame

def check_events(ship):

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_RIGHT:
        # Move the ship to the right.
        ship.moving_right = True

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

def update_screen(ai_settings, screen, ship): '''Update images on the screen and flip to the new screen.'''

1 Comment

Thank you. Just when you think it could be a difficult task, its something as simple as that! appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.