在Python中使用 Arcade 绘制三角形
Arcade 是一个用于开发二维游戏的Python库。 Arcade 需要支持 OpenGL 3.3+。在街机中,基本绘图不需要了解如何定义函数或类或如何进行循环,只需我们有用于绘制图元的内置函数。
用于绘制三角形的 Arcade 内置函数:
1: arcade.draw_traingle_outline():该函数用于绘制三角形的轮廓。
Syntax: arcade.draw_triangle_outline(x1 , y1, x2 , y2 , x3 , y3 , color, border_width)
Parameters:
- x1: x value of first coordinate.
- y1: y value of first coordinate.
- x2:x value of second coordinate.
- y2: y value of second coordinate.
- x3: x value of third coordinate.
- y3: y value of third coordinate.
- color: Outline color of the triangle.
- border_width: Width of the border in pixels. Defaults to 1.
让我们看一个例子来更好地理解它的功能。
Python3
#import module
import arcade
# Open the window. Set the window title and
# dimensions (width and height)
arcade.open_window(600, 600, "Draw a triangle for GfG ")
# set background color
arcade.set_background_color(arcade.color.BLACK)
# Start the render process.
arcade.start_render()
# triangle function
arcade.draw_triangle_outline(300, 200,
80, 80,
100, 300,
arcade.color.YELLOW, 1)
# finished drawing
arcade.finish_render()
# display everything
arcade.run()
Python3
# import
import arcade
# Open the window. Set the window title and
# dimensions (width and height)
arcade.open_window(600, 600, "Draw a triangle for GfG ")
# set background color
arcade.set_background_color(arcade.color.BLACK)
# Start the render process.
arcade.start_render()
# draw triangle
arcade.draw_triangle_filled(300, 200,
80, 80,
100, 300,
arcade.color.YELLOW)
# finish drawing
arcade.finish_render()
# display everything
arcade.run()
输出:
2: arcade.draw_triangle_fill():该函数用于绘制一个填充颜色的三角形。
Syntax: arcade.draw_triangle_filled(x1, y1, x2, y2 , x3 , y3, color)
Parameters:
- x1: x value of first coordinate.
- y1: y value of first coordinate.
- x2: x value of second coordinate.
- y2: y value of second coordinate.
- x3: x value of third coordinate.
- y3: y value of third coordinate.
- color: color to be filled in triangle.
例子:
蟒蛇3
# import
import arcade
# Open the window. Set the window title and
# dimensions (width and height)
arcade.open_window(600, 600, "Draw a triangle for GfG ")
# set background color
arcade.set_background_color(arcade.color.BLACK)
# Start the render process.
arcade.start_render()
# draw triangle
arcade.draw_triangle_filled(300, 200,
80, 80,
100, 300,
arcade.color.YELLOW)
# finish drawing
arcade.finish_render()
# display everything
arcade.run()
输出: