Skip to content
Advertisement

How can I rotate this object back and forth forever?

I have a bone arrow image that rotates. If it reaches a certain timer I want it to stop rotating on one side and start rotating the other. If the other side reaches its timer it should goes back and forth, back and forth, like this short gameplay video.

I don’t know why it stops when its timer2 turn at the end.

# in my main loop

if timer < 60:
    timer += 1
    bow.angle += 1
else:
    if timer2 < 80:
        timer2 += 1
        bow.angle -= 1

Advertisement

Answer

Use the muodulo (%) operator to change the direction by an certain interval:

At initialization:

timer = 0
dir = 1

In the application loop:

if timer >= 60 and timer % 20 == 0:
    dir *= -1
bow.angle += dir
timer += 1
if timer >= 100:
    timer = 60

Alternatively set the timer to 60 when it reaches 100:

if timer < 60:
    bow.angle += 1
else:
    bow.angle -= 1
timer += 1
if timer >= 80:
    timer = 40
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement