Python-不能使变量的数字更高

问题描述 投票:0回答:1

我正在用python编写游戏乒乓球,每次弹回球棒时,我都希望球不会更快。所以我试图添加

global SPEED
SPEED + 25

进入功能higher_speed,它将在每次弹回蝙蝠事件触发器的球时触发。这里是游戏代码的简短版本:

...

BALL_SIZE = 20
BAT_WIDTH = 10
BAT_HEIGHT = 100
SPEED = 250  # (in pixels per second)
BAT_SPEED = SPEED * 1.5  # (in pixels per second)

...

def higher_speed(SPEED, x):
    x = 25
    global SPEED
    SPEED + x
    return SPEED

    # bounce left
    if ball_position[0] < BAT_WIDTH + BALL_SIZE / 2:
        if bat_min < bat_position[0] < bat_max:
            # bat bounces ball
            BALL_SPEED[0] = abs(BALL_SPEED[0])
            global SPEED
            higher_speed()
        else:
            # bat hadn't bounced the ball, player loses
            score[1] += 1
            reset()

    # bounce right
    if ball_position[0] > WIDTH7777 - (BAT_WIDTH + BALL_SIZE / 2):
        if bat_min < bat_position[1] < bat_max:
            BALL_SPEED[0] = -abs(BALL_SPEED[0])
            higher_speed()
        else:
            score[0] += 1
            reset()

...


请帮助。我感谢您的时间:)。

python variables pong
1个回答
0
投票

这里有几件事:

首先,该值未更改,因为它不是首先分配的。

def higher_speed(SPEED, x):
    global SPEED
    SPEED += 25
© www.soinside.com 2019 - 2024. All rights reserved.