Python / Pygame运动增量

时间:2014-02-02 15:14:09

标签: python pygame

我有一个11个空格的板。我还有一个柜台和一个骰子。我希望玩家能够击中“a”而它需要“coordY - 72 * diceRoll”这将使他们在棋盘上移动2个空格,如果他们掷出2个。当轮到他们再次滚动并说例如他们滚动时这次总共3次他们将从起始位置向上移动5个空格。问题是当他们滚动例如两个他们向上移动两个空格时,然后当他们滚动例如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")

1 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题......
然后你需要将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")

这是所有球员得分合并和更新的时候。 但是如果你想让每个球员在他/她掷骰子时更新得分,那么你可以为每个球员使用单独的变量