使用街机库Python绘制太阳
您可能已经使用著名的Python模块(如turtle)绘制了 Sun,但在这里我们将讨论如何使用 Arcade 模块实现相同的方法。 Arcade 库是一个现代Python模块,广泛用于开发具有引人入胜的图形和声音的 2D 视频游戏。 Arcade 是一个面向对象的库。它可以像任何其他Python包一样安装。
安装
要安装此模块,只需在命令提示符下运行以下命令:
pip install arcade
方法:
以下步骤说明了如何使用 Arcade 模块创建基本绘图:
- 导入模块。
- 指定输出屏幕的参数,如宽度、高度等。
- 使用 Arcade 中内置的 open_window() 打开窗口。此命令打开一个具有给定大小的窗口,即宽度和高度以及屏幕标题。
- 设置背景颜色(可选)。可以使用内置在街机中的 set_background_color() 方法来完成
- 告诉您的模块使用 start_render() 命令开始绘图,该命令再次内置到街机中。
- 开始设计,您可以使用 Arcade 已有的功能来完成。
- 使用finish_render() 告诉街机模块你已经完成了绘图。
- 使用 run() 运行您的代码。
Python3
# import module
import arcade
# set parameters
Width= 700
Height=700
Title="SUN"
# open window
arcade.open_window(Width, Height, Title)
# Set the background color
arcade.set_background_color(arcade.csscolor.BLUE)
# Get ready to draw
arcade.start_render()
# Draw a sun
arcade.draw_circle_filled(500, 550, 40, arcade.color.YELLOW)
# Rays to the left, right, up, and down
arcade.draw_line(500, 550, 400, 550, arcade.color.YELLOW, 3)
arcade.draw_line(500, 550, 600, 550, arcade.color.YELLOW, 3)
arcade.draw_line(500, 550, 500, 450, arcade.color.YELLOW, 3)
arcade.draw_line(500, 550, 500, 650, arcade.color.YELLOW, 3)
# Diagonal ray
arcade.draw_line(500, 550, 550, 600, arcade.color.YELLOW, 3)
arcade.draw_line(500, 550, 550, 500, arcade.color.YELLOW, 3)
arcade.draw_line(500, 550, 450, 600, arcade.color.YELLOW, 3)
arcade.draw_line(500, 550, 450, 500, arcade.color.YELLOW, 3)
# Finish drawing
arcade.finish_render()
# Keep the window up until someone closes it.
arcade.run()
输出: