Reputation: 455
I am trying to have a sprite (enemy) move in random directions around the screen. The enemy will move x,y for 500 frames then move to another random direction.
My problem is trying to get the object to hold its direction for each frame until the counter reaches 500. Instead, the object changes direction every frame.
if e_move_count <= 500:
e_xMove = random.randint(-1,1)
e_yMove = random.randint(-1,1)
self.enemy.move(e_xMove, e_yMove)
e_move_count += 1
if e_move_count >= 500:
e_xMove = random.randint(-1,1)
e_yMove = random.randint(-1,1)
self.enemy.move(e_xMove, e_yMove)
e_move_count = 0
I know exactly what my problem is, but I can't think of a workaround. How do I stay in one direction without redefining x,y on each frame? I am open to using timers if that's possible in Python?
Upvotes: 0
Views: 77
Reputation: 71
Try changing it to this:
if e_move_count <= 500:
self.enemy.move(e_xMove, e_yMove)
e_move_count += 1
if e_move_count >= 500:
e_xMove = random.randint(-1,1)
e_yMove = random.randint(-1,1)
self.enemy.move(e_xMove, e_yMove)
e_move_count = 0
This way, the direction will only change when e_move_count is >= 500
Upvotes: 2