开发者

Animate pygame sprite in elliptical path

开发者 https://www.devze.com 2022-12-30 22:01 出处:网络
This is pygame 1.9 on python 2.6.. Here is a screenshot of wh开发者_如何学Cat is currently being drawn in my \"game\" to give some context. Here is the code.

This is pygame 1.9 on python 2.6..

Here is a screenshot of wh开发者_如何学Cat is currently being drawn in my "game" to give some context. Here is the code.

It's supposed to be the moon orbiting around the earth (I'm not trying to make a real simulation or anything, I'm just using the setting to play around and learn pygame). It's 2 circles, and the moons elliptical orbit around the earth. My end game is to have the moon follow it's orbit around the earth, but I want to later use keyboard controls to adjust the shape of the moons orbit.

I really just need help with figuring out how to make the moon follow the path, I could probably figure the rest out.


Well here is how you generate points along an ellipse:

for degree in range(360):
    x = cos(degree * 2 * pi / 360) * radius * xToYratio
    y = sin(degree * 2 * pi / 360) * radius

(x,y) will follow an ellipse centered at (0,0), with the y radius being radius and the x radius being xToYratio. In your case, you probably want degree to be related to time passing somehow.

EDIT: you can also do this:

for degree in range(360):
    x = cos(degree * 2 * pi / 360) * xRadius
    y = sin(degree * 2 * pi / 360) * yRadius

where xRadius is half of your rect's width, and yRadius is half of your rects height. Visualize it intuitively - you have a circle, and you're stretching it out (i.e. scaling it, i.e. multiplying it) so that it's as big as the rect horizontally and vertically.

0

精彩评论

暂无评论...
验证码 换一张
取 消