""" 这个动画演示如何展示多个物体的移动,顺便演示了类的继承。 This simple animation example shows how to use classes to animate multiple objects on the screen at the same time. 由于每帧都画所有图形,所以它运行很慢,是低效的。 Because this is redraws the shapes from scratch each frame, this is SLOW and inefficient. 使用缓冲缓图命令要更复杂一些,但速度更快。 Using buffered drawing commands (Vertex Buffer Objects) is a bit more complex, but faster. """ import arcade import random # 设置常量 SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 SCREEN_TITLE = "彩色形状示例" RECT_WIDTH = 50 RECT_HEIGHT = 50 NUMBER_OF_SHAPES = 200 class Shape: def __init__(self, x, y, width, height, angle, delta_x, delta_y, delta_angle, color): self.x = x # x坐标 self.y = y # y坐标 self.width = width # 宽度 self.height = height # 高度 self.angle = angle # 角度 self.delta_x = delta_x # 水平移动速度 self.delta_y = delta_y # 垂直移动速度 self.delta_angle = delta_angle # 每次旋转角度 self.color = color # 颜色 def move(self): self.x += self.delta_x self.y += self.delta_y self.angle += self.delta_angle class Ellipse(Shape): def draw(self): arcade.draw_ellipse_filled(self.x, self.y, self.width, self.height, self.color, self.angle) class Rectangle(Shape): def draw(self): arcade.draw_rectangle_filled(self.x, self.y, self.width, self.height, self.color, self.angle) class MyGame(arcade.Window): """ Main application class. """ def __init__(self): super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE) self.shape_list = None def setup(self): """ 设置游戏及初始化变量. """ self.shape_list = [] # 只是单纯的一个列表 for i in range(NUMBER_OF_SHAPES): # 随机生成一些形状 x = random.randrange(0, SCREEN_WIDTH) y = random.randrange(0, SCREEN_HEIGHT) width = random.randrange(10, 30) height = random.randrange(10, 30) angle = random.randrange(0, 360) d_x = random.randrange(-3, 4) d_y = random.randrange(-3, 4) d_angle = random.randrange(-3, 4) red = random.randrange(256) green = random.randrange(256) blue = random.randrange(256) alpha = random.randrange(256) shape_type = random.randrange(2) if shape_type == 0: shape = Rectangle(x, y, width, height, angle, d_x, d_y, d_angle, (red, green, blue, alpha)) else: shape = Ellipse(x, y, width, height, angle, d_x, d_y, d_angle, (red, green, blue, alpha)) self.shape_list.append(shape) def update(self, dt): """ 移动所有形状 """ for shape in self.shape_list: shape.move() def on_draw(self): """ 渲染所有图形 """ arcade.start_render() for shape in self.shape_list: shape.draw() def main(): window = MyGame() window.setup() arcade.run() if __name__ == "__main__": main()