python - In PyGame, how to move an image every 3 seconds without using the sleep function? -
recently i've learned basic python, writing game using pygame enhance programming skills.
in game, want move image of monster every 3 seconds, @ same time can aim mouse , click mouse shoot it.
at beginning tried use time.sleep(3), turned out pause whole program, , can't click shoot monster during 3 seconds.
so have solution this?
thanks in advance! :)
finally solved problem of guys. thank much! here part of code:
import random, pygame, time x = 0 t = time.time() while true: screen = pygame.display.set_mode((1200,640)) screen.blit(bg,(0,0)) if time.time() > t + 3: x = random.randrange(0,1050) t = time.time() screen.blit(angel,(x,150)) pygame.display.flip()
pygame has clock class can used instead of python time module.
here example usage:
clock = pygame.time.clock() time_counter = 0 while true: time_counter = clock.tick() if time_counter > 3000: enemy.move() time_counter = 0
Comments
Post a Comment