简体   繁体   English

Python / Pygame运动增量

[英]Python/Pygame movement incrementation

I have a board of 11 spaces. 我有一个11个席位的木板。 I have a counter and a dice also. 我也有柜台和骰子。 I want the player to hit "a" and it takes the "coordY - 72 * diceRoll" This will move them for example 2 spaces up the board if they roll a 2. When it's their turn to roll again and say for example they roll a 3 this time, in total they would have moved 5 spaces up the board from the starting position. 我希望玩家点击“ a”,然后接受“ coordY-72 * diceRoll”,如果他们掷出2,这会将他们例如在板上移动2个空格。轮到他们再次掷出并说例如他们掷出这次为3,则他们总共将从起始位置向上移动了5个空格。 the problem is when they roll for example a two they move two spaces up, and then when they roll for example a 3 it only moves to space 3 not 5. So it doesn't save the previous position and move it from there. 问题是当他们滚动例如2时,它们向上移动两个空间,然后滚动例如3时,它仅移动到空间3,而不是5。因此,它不保存先前的位置并从那里移动。 I have no idea how i'd do this. 我不知道我该怎么做。 Here's my current code: 这是我当前的代码:

    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")

If I have correctly understood your problem... 如果我正确理解了您的问题...
then you need to make countY as global but inside the movement() function where it is updated not outside. 那么您需要将countY为全局countY ,但要在countY movement()函数内部,而不是在外部进行更新。
Also if countY changes by (countY - 72 * diceRoll) every time a dice is rolled so you need to update it which you obviously are missing. 同样,如果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")

this is when all players score is combined and updated. 这是所有玩家得分合并并更新的时候。 but if you want each players own score to be updated when He/She rolls a dice then you can use separate variables for each of them 但是,如果您希望每个玩家在掷骰子时更新自己的得分,则可以为每个变量使用单独的变量

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

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