如何使用我的 elif,if 语句更正语法错误.我做错了什么


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.'''

错误:

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]

在你的函数中,你的第一个条件应该以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

您有一个前面没有ifelif。只需将第一个elif更改为if即可。

不能在 if 语句前使用 elif。如果没有 if 语句,则不能使用 Elif。这是正确的语法。导入系统

导入游戏

定义check_events(船(:

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

定义update_screen(ai_settings,屏幕,船(: "更新屏幕上的图像并翻转到新屏幕。

最新更新