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
If you want to control something over time in Pygame you have two options:
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)
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
Example 1: Minimal examples: PYGBAG demo
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: Minimal examples: PYGBAG demo
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()