Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

python - Move fire ball up and down smoothly Pygame

I have a fireball that moves up and down but the problem is when its time to move down it has this snap to move back down and it doesn't look like the fireball is actually moving up and down smoothly at all. Is there a way I could make this better?

VIDEO

I want it to fall down like how a fireball would fall down instead of having that snap effect.

Here in my main loop I have the speed and the direction of how my bullets are moving.

for shot in shots:
    if shot.direction == "down": # if the direction is down add to the y
        shot.y += 2
        
    if shot.direction == "up": # if the direction is up minus- to the y
        shot.y -= 2
    if shot.y <= 200: # if the object y is <= 200 then change the direction  and add to the y
        shot.direction = "down"
    if shot.y >= 700:  # if the object y is >= 700 then change the direction and minus the y
        shot.direction = "up"
question from:https://stackoverflow.com/questions/66052243/move-fire-ball-up-and-down-smoothly-pygame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What about using math.sin:

class shot:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.time = 0
        # [...]
for shot in shots:
    shot.y = 700 - math.sin(math.radians(self.time)) * 500
    shot.time += 1
    if shot.time >= 180:
        shot.time = 0

Or math.cos:

for shot in shots:
    shot.y = 200 + math.cos(math.radians(self.time)) * 500
    shot.time += 1
    if shot.time >= 360:
        shot.time = 0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...