簡體   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