如果未在pygame中按下箭头键,则继续循环

2 python pygame

我正在尝试创建此功能,以便如果按下除任何箭头键之外的任何键,则循环继续,直到按下箭头键.程序每次都崩溃,不显示错误.知道为什么吗?

def speed(self, key):
    # Figure out if it was an arrow key. If so
    # adjust speed

    n = 'go'
    while n == 'go':
        if key == pygame.K_LEFT:
            x_speed=-5
            y_speed=0
            return x_speed, y_speed
            n = 'stop'
        if key == pygame.K_RIGHT:
            x_speed = 5
            y_speed = 0
            return x_speed, y_speed
            n = 'stop'
        if key == pygame.K_UP:
            y_speed = -5
            x_speed = 0
            return x_speed, y_speed
            n = 'stop'
        if key == pygame.K_DOWN:
            y_speed = 5
            x_speed = 0
            return x_speed, y_speed
            n = 'stop'
        else:
            continue
Run Code Online (Sandbox Code Playgroud)

Ell*_*Ell 5

我从来没有在生活中使用过pygame,也没有写过一篇关于python的单词,但这是我的猜测:

def speed(self, key):
    # Figure out if it was an arrow key. If so
    # adjust speed
    if key == pygame.K_LEFT:
        x_speed=-5
        y_speed=0
        return x_speed, y_speed

    if key == pygame.K_RIGHT:
        x_speed = 5
        y_speed = 0
        return x_speed, y_speed

    if key == pygame.K_UP:
        y_speed = -5
        x_speed = 0
        return x_speed, y_speed

    if key == pygame.K_DOWN:
        y_speed = 5
        x_speed = 0
        return x_speed, y_speed
Run Code Online (Sandbox Code Playgroud)

你的代码不起作用的原因是因为当没有按下箭头键时你实际上是这样做的:

n = "go"
while n == "go":
    continue
Run Code Online (Sandbox Code Playgroud)

为语法错误道歉,如果我错了,也是.