在Python中使用 Arcade 库绘制一张笑脸
Arcade 是一个用于开发 2D 游戏的Python模块。画一张开心的脸,步骤如下:
- 导入街机库。
import arcade
- 打开窗户。
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
与 Arcade 一起使用的函数是 open_window。此命令打开一个具有给定大小的窗口,即宽度和高度以及屏幕标题。
- 指定使用 open_window 定义的参数值。
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Happy Face GfG "
- 为输出的背景屏幕选择一种颜色。
arcade.set_background_color(arcade.color.BLACK)
- 此命令将告诉 Arcade 库您开始绘图。
arcade.start_render()
- 使用内置函数draw_circle_filled() 函数编写一个函数来绘制面部底部的圆。
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)
- 类似地,使用 draw_circle_filled()函数编写用于绘制脸部左右眼的函数。
# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)
# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)
- 对于幸福的脸,微笑是最重要的部分。使用 draw_arc_outline()函数绘制微笑。
# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height,
arcade.color.BLACK,
start_angle, end_angle, 10)
- 此命令将告诉街机库您已完成绘图。
# Finish drawing
arcade.finish_render()
- 这个命令是保持窗口打开。
# Keep the window open until the user
# hits the 'close' button
arcade.run()
完整的源代码:
Python3
import arcade
# specify the parameters
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SCREEN_TITLE = "Happy Face GfG "
# Open the window. Set the window title and dimensions
arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
# Set the background color
arcade.set_background_color(arcade.color.BLACK)
# start render process
arcade.start_render()
# Draw the face
x = 300
y = 300
radius = 200
arcade.draw_circle_filled(x, y, radius, arcade.color.YELLOW)
# Draw the right eye
x = 370
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)
# Draw the left eye
x = 230
y = 350
radius = 20
arcade.draw_circle_filled(x, y, radius, arcade.color.BLACK)
# Draw the smile
x = 300
y = 280
width = 120
height = 100
start_angle = 190
end_angle = 350
arcade.draw_arc_outline(x, y, width, height, arcade.color.BLACK,
start_angle, end_angle, 10)
# Finish drawing
arcade.finish_render()
# Keep the window open until the user hits
# the 'close' button
arcade.run()
输出: