"""一個簡單的演示八大行星公轉的動畫,採用arcade街機遊戲模塊製作,安裝Arcade請用pip install arcade --user。
A simple animation to simulate the rotation of eight planets is made by using Arcade module. To install Arcade, use PIP install arcade--user.
"""
__author__ = "lixingqiu"
__date__ = "2019/3/8"
import time
import math
import arcade
import random
SCREEN_WIDTH = 1350
SCREEN_HEIGHT = 780
SCREEN_TITLE = "eight planet 八大行星"
class Planet(arcade.Sprite):
def __init__(self,image,a,b,angle,speed):
"""image:造型圖片,a:Long axis長半軸,b:semi-minor axis短半軸,angle:初始角度"""
super().__init__(image)
self.center_x = SCREEN_WIDTH / 2
self.center_y = SCREEN_HEIGHT / 2
self.direction = angle # 自定義direction,不用原有屬性angle
self.a = a
self.b = b
self.speed = speed
def update(self):
""" Calculating Initial Coordinates Based on Elliptic Parametric Equation"""
self.direction = self.direction + 365 / self.speed
self.direction = self.direction % 360
x = SCREEN_WIDTH / 2 + self.a * math.cos(math.radians(self.direction)) # 根據橢圓參數方程算起始座標
y = SCREEN_HEIGHT / 2 + self.b * math.sin(math.radians(self.direction))
self.center_x = x
self.center_y = y
super().update()
class MyGame(arcade.Window):
"""
繼承自窗口的MyGame類.
"""
def __init__(self, width, height, title):
super().__init__(width, height, title)
arcade.set_background_color(arcade.color.WHITE)
def setup(self):
""" 這裏是對遊戲中的各個對象進行設置 """
self.planet_list = arcade.SpriteList() # 新建角色列表,以便統一渲染
# 背景角色生成
pass
# 太陽角色生成
pass
# 行星角色生成
pass
for i in range(8): # 生成8個行星
angle = self.angle_list[i]
a,b = self.ab_list[i]
image = self.planets_image[i]
speed = self.days[i]
aplanet = Planet(image,a,b,angle,speed) # 新建行星
self.planet_list.append(aplanet) # 添加到所有行星列表
def update(self, x):
"""每幀更新遊戲內在邏輯"""
self.planet_list.update()
self.sun.update_animation()
def on_draw(self):
"""渲染屏幕 """
arcade.start_render()
# 開始畫背景
self.background.draw()
self.sun.draw()
self.planet_list.draw()
def main():
""" Main method """
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()