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
您有一个前面没有if
的elif
。只需将第一个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,屏幕,船(: "更新屏幕上的图像并翻转到新屏幕。