简体   繁体   中英

Python/Pygame movement incrementation

I have a board of 11 spaces. 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. 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. 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.
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=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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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