3

Recently I've learned some basic Python, so I am writing a game using PyGame to enhance my programming skills.

In my game, I want to move an image of a monster every 3 seconds, at the same time I can aim it with my mouse and click the mouse to shoot it.

At the beginning I tried to use time.sleep(3), but it turned out that it pause the whole program, and I can't click to shoot the monster during the 3 seconds.

So do you have any solution for this?

Thanks in advance! :)


Finally I solved the problem with the help of you guys. Thank you so much! Here is part of my 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() 
hklel
  • 1,624
  • 23
  • 45
  • Use `time.sleep(3)` in another thread? – Kijewski Jun 08 '14 at 18:26
  • Possible duplicate: http://stackoverflow.com/questions/23982287/is-it-possible-to-show-an-image-for-a-while-and-then-change-it-back/23982495#23982495 – otus Jun 08 '14 at 18:27
  • If your game is running in a loop, then just have a check function in that loop that checks if `current_time = last_saved +3 : move, last_saved=current_time` – 1478963 Jun 08 '14 at 18:40
  • 1
    * `current_time >= last_saved + 3` Just in case there's a delay and it hits `current - last = 4` or similar. – indivisible Jun 08 '14 at 18:51
  • +1 @indivisible didn't fully think it through. @Alex Ling `import datetime datetime.datetime.now()` will get you the current time. – 1478963 Jun 08 '14 at 18:59
  • @Moe I have tried your method but I still can't make it move. I have added my code in the question, could you please give me more instruction? Thank you for your help! – hklel Jun 08 '14 at 19:26
  • Oh, thanks for your help I finally solve the problem! – hklel Jun 08 '14 at 19:43
  • possible duplicate of [Move an object every few seconds in Pygame](http://stackoverflow.com/questions/23368999/move-an-object-every-few-seconds-in-pygame) – sloth Jun 10 '14 at 07:32

2 Answers2

4

Pygame has a clock class that can be used instead of the python time module.

Here is an example usage:

clock = pygame.time.Clock()

time_counter = 0

while True:
    time_counter = clock.tick()
    if time_counter > 3000:
        enemy.move()
        time_counter = 0
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
0

I guess that works, but I feel think it would be more pythonic to do this.

import random, pygame

clock = pygame.time.Clock()
FPS = 26 #Or whatever number you want
loops_num = 0
while True:

    screen = pygame.display.set_mode((1200,640))
    screen.blit(bg,(0,0))

    if loops_num % (FPS * 3) == 0:
        enemy.move()

    screen.blit(angel,(x,150))

    pygame.display.flip()
    loops_num += 1
    clock.tick(FPS)

Or what Bartlomiej Lewandowski said, his answer is great also.

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61