1
def playermove(self,dice_status):
        self._position += dice_status
        '''i want to Add some pauses at each step so 
           it doesn't look like it flashed past'''

def playerlocate(self,screen):
    if self._position <= 19:
        screen.blit(self._image, mapCoordinate[self._position])
    elif 19 < self._position <= 30:
        screen.blit(pygame.transform.rotate(self._image,-90),mapCoordinate[self._position])
    elif 30 < self._position <= 48:
        screen.blit(pygame.transform.rotate(self._image, -180), mapCoordinate[self._position])
    elif 48 < self._position <= 54:
        screen.blit(pygame.transform.rotate(self._image, -270), mapCoordinate[self._position])
    else:
        screen.blit(pygame.transform.rotate(self._image, -270), mapCoordinate[self._position-55])
        self._position -= 55

mapCoordinate=[[1184,773],[971,873],...] #I have 55 map grids

#self._position is a attribute of the player,It corresponds to the serial number of each grid on the map.And I also have a function to convert _position to coordinates and draw the player on the map. I tried to add some time control but I don't know how to do it.Please

郑瑜晨
  • 23
  • 3
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Mar 20 '22 at 12:15
  • I tried iterating with for i in range(dice_status): self._position += 1 ; But this structure doesn't seem to slow down the accumulation time. – 郑瑜晨 Mar 20 '22 at 12:40

2 Answers2

2

If you want to control something over time in Pygame you have two options:

  1. Use pygame.time.get_ticks() to measure time and and implement logic that controls the object depending on the time.

    e.g.:

    time_interval = 500 # 500 milliseconds == 0.1 seconds
    next_step_time = 0 
    
    while run:
        # [...]
    
        current_time = pygame.time.get_ticks()
        if current_time > next_step_time :
            next_step_time += time_interval
    
            # move object
            player.playermove(dice_status)
    
  2. Use the timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. Change object states when the event occurs.

    e.g.:

    time_interval = 500 # 500 milliseconds == 0.1 seconds
    timer_event = pygame.USEREVENT+1
    pygame.time.set_timer(timer_event, time_interval)
    
    while run:
        for event in pygame.event.get():
            if event.type == timer_event:
    
                # move object
                player.playermove(dice_status)
    

See also Spawning multiple instances of the same object concurrently in python


Minimal examples:

Example 1:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()
rect = pygame.Rect(0, 80, 40, 40)

time_interval = 500 # 500 milliseconds == 0.1 seconds
next_step_time = 0 

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    current_time = pygame.time.get_ticks()
    if current_time > next_step_time :
        next_step_time += time_interval
    
        rect.x += 40
        if rect.x >= 400:
            rect.x = 0

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()

Example 2:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 200))
clock = pygame.time.Clock()
rect = pygame.Rect(0, 80, 40, 40)

time_interval = 500 # 500 milliseconds == 0.1 seconds
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, time_interval) 

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

        if event.type == timer_event:

            rect.x += 40
            if rect.x >= 400:
                rect.x = 0

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

One way would be to keep a counter in the loop, and based on the value of the counter you can update the position of the object. This can effectively slow down the movement of the object.

You can do this by defining a counter variable at the beginning - before the loop.

Then update the value of the counter in every iteration.

After that, in the place where you update the position of the object, check for the value of the counter.

For example:-

if counter > 5: #you can increase or decrease the value... here I have taken 5
   self.position += dice_status
   counter = 0

You also have to change the value of the counter back to zero in the 'if' block.

Instead of 5 if you take a greater value, it will make the object even more slower.