繁体   English   中英

Python / Pygame运动增量

[英]Python/Pygame movement incrementation

我有一个11个席位的木板。 我也有柜台和骰子。 我希望玩家点击“ a”,然后接受“ coordY-72 * diceRoll”,如果他们掷出2,这会将他们例如在板上移动2个空格。轮到他们再次掷出并说例如他们掷出这次为3,则他们总共将从起始位置向上移动了5个空格。 问题是当他们滚动例如2时,它们向上移动两个空间,然后滚动例如3时,它仅移动到空间3,而不是5。因此,它不保存先前的位置并从那里移动。 我不知道我该怎么做。 这是我当前的代码:

    countY = 750
    count1 = pygame.draw.circle(window, (black),(150, countY), 25, 0)
    count2 = pygame.draw.circle(window, (black),(250, countY), 25, 0)
    count3 = pygame.draw.circle(window, (255, 255, 255),(450, countY), 25, 0)
    count4 = pygame.draw.circle(window, (255, 255, 255),(550, countY), 25, 0)
    print("Should draw start counters")
    pygame.display.flip()



def movement():

    pygame.display.update()

    while True:
        global countY
        game = True
        while game:
            for event in pygame.event.get():
                pygame.event.get()

                #Counter 1 movement
                if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
                    diceRoll = random.randint(1, 4)
                    window.fill(grey)
                    grid()
                    count1 = pygame.draw.circle(window, (black),(150, countY - 72 * diceRoll), 25, 0)
                    countY= countY - 72 * diceRoll # here is where the countY is updated
                    game = False
                    game2 = True
                    print("Test")

如果我正确理解了您的问题...
那么您需要将countY为全局countY ,但要在countY movement()函数内部,而不是在外部进行更新。
同样,如果countY变化为(countY - 72 * diceRoll)则您需要对其进行更新,而这显然是丢失的。

countY=750

def movement
    while True:
        global countY #declare global here
        game = True
        while game:
            for event in pygame.event.get():
                #Counter 1 movement
                if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
                    diceRoll = random.randint(1, 4)
                    window.fill(grey)
                    grid()
                    count1 = pygame.draw.circle(window, (black),(150, countY - 72 * diceRoll), 25, 0)
                    countY= countY - 72 * diceRoll # here is where the countY is updated
                    game = False
                    game2 = True
                    print("Test")

这是所有玩家得分合并并更新的时候。 但是,如果您希望每个玩家在掷骰子时更新自己的得分,则可以为每个变量使用单独的变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM