Python Arcade库基础教程基本绘图方法使用(三)

4 篇文章 0 订阅
1 篇文章 0 订阅

Arcade基本绘图方法

在绘图之前首先说明下在arcade中的坐标,arcade中的坐标是按数学中的直角坐标系来进行计算,是以窗口左下角为坐标原点进行计算。
在这里插入图片描述

  • draw_arc_filled

绘制实心圆弧。
参数:

  • center_x (float):圆弧中心点距离窗口左边的距离(不包括窗口边框)
  • center_y (float):圆弧中心点距离窗口下边的距离(不包括窗口边框)
  • width (float):实心圆弧的宽度
  • height (float):实心圆弧的高度
  • color (arcade.Color):颜色,以RGBRGBA格式
  • start_angle (float):实心圆弧的起始角度,以度为单位
  • end_angle (float):实心圆弧的结束角度,以度为单位
  • tilt_angle (float):实心圆弧倾斜度数
  • num_segments (float):用于绘制圆弧的线段数,数值越大越接近实心圆弧,越高质量越好,但是渲染时间越慢
import arcade
WIDTH = 1900
HEIGHT = 980
TITLE = "窗体测试标题"

class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(10, 30)

    def on_draw(self):
        arcade.start_render()
        # 宽高不同,开始度数为0,结束度数为360,倾斜度数为0时图形是一个标准实心椭圆
        arcade.draw_arc_filled(center_x=55, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 0, num_segments=360)
        # 宽高不同,存在开始度数与结束度数,倾斜度为0时图形是一个不标准实心扇形
        arcade.draw_arc_filled(center_x=200, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=30, end_angle=180, tilt_angle= 0, num_segments=360)
        # 宽高不同,存在开始度数为0结束度数为360倾斜度时图形是一个倾斜指定角度的实心椭圆
        arcade.draw_arc_filled(center_x=350, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 36, num_segments=360)
        # 宽高不同,存在开始度数为0结束度数为360绘画线条数很少时图形是一个实心多边形
        arcade.draw_arc_filled(center_x=500, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 0, num_segments=5)
        # 宽高相同,开始度数0结束度数360时图形是一个标准实心圆
        arcade.draw_arc_filled(center_x=700, center_y=850, width=200, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 0, num_segments=810)
        # 宽高相同,开始度数90结束度数180时图形是一个标准实心扇形
        arcade.draw_arc_filled(center_x=950, center_y=850, width=200, height=200,
        color=arcade.color.RED, start_angle=90, end_angle=180, tilt_angle= 0, num_segments=810)
        # 宽高相同,开始度数90结束度数180倾斜度时图形是一个倾斜指定角度的标准实心扇形
        arcade.draw_arc_filled(center_x=1100, center_y=850, width=200, height=200,
        color=arcade.color.RED, start_angle=90, end_angle=180, tilt_angle= 30, num_segments=810)
        # 开始度数大于结束度数时图形不绘制
        arcade.draw_arc_filled(center_x=1300, center_y=750, width=300, height=300,
        color=arcade.color.RED, start_angle=30, end_angle=20, tilt_angle= 0, num_segments=810)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_arc_outline

绘制圆弧的外边缘,绘制曲线很有用,与draw_arc_filled方法唯一区别就是不是实心图形。
参数:

  • center_x (float):弧中心点距离窗口左边的距离(不包括窗口边框)
  • center_y (float):弧中心点距离窗口下边的距离(不包括窗口边框)
  • width (float):弧的宽度
  • height (float):弧的高度
  • color (arcade.Color):颜色,以RGBRGBA格式
  • start_angle (float):弧的起始角度,以度为单位
  • end_angle (float):弧的结束角度,以度为单位
  • tilt_angle (float):倾斜度数
  • num_segments (float):用于绘制弧的线段数,数值越大越接近圆弧,越高质量越好,但是渲染时间越慢
import arcade
WIDTH = 1900
HEIGHT = 980
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(10, 30)
    def on_draw(self):
        arcade.start_render()
        # 宽高不同,开始度数为0,结束度数为360,倾斜度数为0时图形是一个标准椭圆圆环
        arcade.draw_arc_outline(center_x=55, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 0, num_segments=360)
        # 宽高不同,存在开始度数与结束度数,倾斜度为0时图形是一个不标准曲线
        arcade.draw_arc_outline(center_x=200, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=30, end_angle=180, tilt_angle= 0, num_segments=360)
        # 宽高不同,存在开始度数为0结束度数为360倾斜度时图形是一个倾斜指定角度的标准椭圆圆环
        arcade.draw_arc_outline(center_x=350, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 36, num_segments=360)
        # 宽高不同,存在开始度数为0结束度数为360绘画线条数很少时图形是一个多边形环
        arcade.draw_arc_outline(center_x=500, center_y=850, width=100, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 0, num_segments=5)
        # 宽高相同,开始度数0结束度数360时图形是一个标准圆圆环
        arcade.draw_arc_outline(center_x=700, center_y=850, width=200, height=200,
        color=arcade.color.RED, start_angle=0, end_angle=360, tilt_angle= 0, num_segments=810)
        # 宽高相同,开始度数90结束度数180时图形是一个标准曲线
        arcade.draw_arc_outline(center_x=950, center_y=850, width=200, height=200,
        color=arcade.color.RED, start_angle=90, end_angle=180, tilt_angle= 0, num_segments=810)
        # 宽高相同,开始度数90结束度数180倾斜度时图形是一个倾斜指定角度的标准曲线
        arcade.draw_arc_outline(center_x=1100, center_y=850, width=200, height=200,
        color=arcade.color.RED, start_angle=90, end_angle=180, tilt_angle= 30, num_segments=810)
        # 开始度数大于结束度数时图形不绘制
        arcade.draw_arc_outline(center_x=1300, center_y=750, width=300, height=300,
        color=arcade.color.RED, start_angle=30, end_angle=20, tilt_angle= 0, num_segments=810)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_circle_filled

画一个实心圆。
参数:

  • center_x (float):实心圆圆心点距离窗口左边的距离(不包括窗口边框)
  • center_y (float):实心圆圆心点距离窗口下边的距离(不包括窗口边框)
  • radius (float):圆的半径
  • color (arcade.Color):颜色,以RGBRGBA格式
  • num_segments (int):组成圆的形段数量,越高质量越好,但是渲染时间越慢,默认值-1表示自动根据圆的大小计算合理数量的线段
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"

class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)

    def on_draw(self):
        arcade.start_render()
        arcade.draw_circle_filled(center_x=100, center_y=300, radius=64, color=arcade.color.RED)
        arcade.draw_circle_filled(center_x=250, center_y=300, radius=64,
        color=arcade.color.RED, num_segments=8)
        arcade.draw_circle_filled(center_x=390, center_y=300, radius=64,
        color=arcade.color.RED, num_segments=1000)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_circle_outline

画一个标准圆环。
参数:

  • center_x (float):圆环圆心点距离窗口左边的距离(不包括窗口边框)
  • center_y (float):圆环圆心点距离窗口下边的距离(不包括窗口边框)
  • radius (float):圆环的半径
  • color (arcade.Color):颜色,以RGBRGBA格式
  • border_width (float):圆环轮廓的宽度,以像素为单位
  • num_segments (int):组成圆环的形段数量,越高质量越好,但是渲染时间越慢,默认值-1表示自动根据圆的大小计算合理数量的线段
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)

    def on_draw(self):
        arcade.start_render()
        arcade.draw_circle_outline(center_x=100, center_y=300, radius=64,
        color=arcade.color.RED, border_width=36)
        # 圆环宽度大于半径时会变成实心圆
        arcade.draw_circle_outline(center_x=250, center_y=300, radius=64,
        color=arcade.color.RED, border_width=64)
        arcade.draw_circle_outline(center_x=390, center_y=300, radius=64,
        color=arcade.color.RED, border_width=9, num_segments=1000)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_ellipse_filled

绘制一个实心椭圆
参数:

  • center_x (float):实心椭圆圆心点距离窗口左边的距离(不包括窗口边框)
  • center_y (float):实心椭圆圆心点距离窗口下边的距离(不包括窗口边框)
  • width (float):椭圆的宽度
  • height (float):椭圆的高度
  • color (arcade.Color):颜色,以RGBRGBA格式
  • tilt_angle (浮动):椭圆倾斜度数
  • num_segments (int):组成实心椭圆的形段数量,越高质量越好,但是渲染时间越慢,默认值-1表示自动根据圆的大小计算合理数量的线段
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)
    def on_draw(self):
        arcade.start_render()
        arcade.draw_ellipse_filled(center_x=50, center_y=300, width=50, height=100,
        color=arcade.color.RED)
        # 宽高相同时其实是一个标准实心圆
        arcade.draw_ellipse_filled(center_x=150, center_y=300, width=100, height=100,
        color=arcade.color.RED)
        arcade.draw_ellipse_filled(center_x=300, center_y=300, width=50, height=100,
        color=arcade.color.RED, tilt_angle=30)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_ellipse_outline

绘制一个椭圆圆环
参数:

  • center_x (float):椭圆圆环圆心点距离窗口左边的距离(不包括窗口边框)
  • center_y (float):椭圆圆环圆心点距离窗口下边的距离(不包括窗口边框)
  • width (float):椭圆圆环的宽度
  • height (float):椭圆圆环的高度
  • color (arcade.Color):颜色,以RGBRGBA格式
  • border_width (float):椭圆圆环的宽度,以像素为单位。
  • tilt_angle (浮动):椭圆圆环倾斜度数
  • num_segments (int):组成椭圆圆环的形段数量,越高质量越好,但是渲染时间越慢,默认值-1表示自动根据圆的大小计算合理数量的线段
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)

    def on_draw(self):
        arcade.start_render()
        arcade.draw_ellipse_outline(center_x=50, center_y=300, width=50, height=100,
        color=arcade.color.RED)
        # 宽高相同时其实是一个标准圆环
        arcade.draw_ellipse_outline(center_x=150, center_y=300, width=100, height=100,
        color=arcade.color.RED)
        arcade.draw_ellipse_outline(center_x=300, center_y=300, width=50, height=100,
        color=arcade.color.RED, tilt_angle=30)
        # 当环宽度超过宽高最大值时会变成实心椭圆,且椭圆大小会变
        arcade.draw_ellipse_outline(center_x=500, center_y=300, width=50, height=100,
        color=arcade.color.RED, border_width=100)
        # 椭圆圆环宽高相同,圆环宽度等于宽高时为一个实心圆
        arcade.draw_ellipse_outline(center_x=700, center_y=300, width=100, height=100,
        color=arcade.color.RED, border_width=100)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_line

画一条线。
参数:

  • start_x (float):直线起点距离窗口左边的距离(不包括窗口边框)的x位置
  • start_y (float):线起点距离窗口左边的距离(不包括窗口边框)的y位置
  • end_x (float):线终点距离窗口左边的距离(不包括窗口边框)的x位置
  • end_y (float):线终点距离窗口左边的距离(不包括窗口边框)的y位置
  • color (arcade.Color):颜色,以RGBRGBA格式
  • line_width (float):线的宽度,以像素为单位
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)
    def on_draw(self):
        arcade.start_render()
        arcade.draw_line(start_x=50, start_y=390, end_x=300, end_y=390, color=arcade.color.RED)
        # 变成矩形
        arcade.draw_line(start_x=100, start_y=350, end_x=600, end_y=350,
        color=arcade.color.RED, line_width=30)
        arcade.draw_line(start_x=100, start_y=330, end_x=300, end_y=200,
        color=arcade.color.RED)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_line_strip

画一条多点线,类似于折线。
参数:

  • point_list (arcade.PointList):组成每条线的x,y点的列表
  • color (arcade.Color):线的颜色,以RGBRGBA格式
  • line_width (float):线的宽度
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)
    def on_draw(self):
        arcade.start_render()
        arcade.draw_line_strip(point_list=[(
            1, 1), (899, 499), (899, 0), (0, 499), (15, 300), (200, 100)], color=arcade.color.RED)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_lines

画一组线,在指定的每对点之间画一条线。
参数:

  • point_list (arcade.PointList):组成线的点的列表,每个点都在列表中。
  • color (arcade.Color):线的颜色,以RGBRGBA格式
  • line_width (float):线的宽度
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)
    def on_draw(self):
        arcade.start_render()
        arcade.draw_lines(point_list=[(1, 1), (899, 499), (899, 0), (0, 499)], color=arcade.color.RED)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_lrtb_rectangle_filled

通过指定左,右,上和下边缘来绘制实心矩形。
参数:

  • left (float):实心矩形左边缘距离窗口左边的距离(不包括窗口边框)的x坐标。
  • right (float):实心矩形右边缘距离窗口左边的距离(不包括窗口边框)的x坐标。
  • top (float):实心矩形顶部距离窗口左边的距离(不包括窗口边框)的y坐标。
  • bottom (float):实心矩形底部距离窗口左边的距离(不包括窗口边框)的y坐标。
  • color (arcade.Color):实心矩形的颜色,以RGBRGBA格式
  • left > righttop < bottom ,则引发引发AttributeError错误
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)
    def on_draw(self):
        arcade.start_render()
        arcade.draw_lrtb_rectangle_filled(left=50, right=600, top=390, bottom=300,color=arcade.color.RED)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • draw_lrtb_rectangle_outline

通过指定左,右,上和下边缘来绘制矩形框。
参数:

  • left (float):矩形框左边缘距离窗口左边的距离(不包括窗口边框)的x坐标。
  • right (float):矩形框右边缘距离窗口左边的距离(不包括窗口边框)的x坐标。
  • top (float):矩形框顶部距离窗口左边的距离(不包括窗口边框)的y坐标。
  • bottom (float):矩形框底部距离窗口左边的距离(不包括窗口边框)的y坐标。
  • color (arcade.Color):矩形框的颜色,以RGBRGBA格式
  • border_width (float):边框的宽度,以像素为单位,默认为1
  • left > righttop < bottom ,则引发引发AttributeError错误
import arcade
WIDTH = 900
HEIGHT = 500
TITLE = "窗体测试标题"
class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.background_color = arcade.color.BLACK
        self.set_location(50, 50)
    def on_draw(self):
        arcade.start_render()
        arcade.draw_lrtb_rectangle_outline(left=50, right=600, top=390, bottom=300,
        color=arcade.color.RED)
        arcade.draw_lrtb_rectangle_outline(left=50, right=600, top=280, bottom=200,
        color=arcade.color.RED, border_width=9)
def main():
    game = MyGame(WIDTH, HEIGHT, TITLE)
    arcade.run()
if __name__ == "__main__":
    main()

在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 我可以帮你使用Pythonarcade来创建一个平台类游戏arcade提供了一个简单而强大的API,可以帮助你创建2D游戏。它还有一个文档,可以帮助你开始创建游戏,并且拥有许多示例代码,可以让你学习如何使用这个。 ### 回答2: 使用Pythonarcade可以很方便地制作平台类游戏arcade提供了许多内置函数和类,用于处理游戏循环、精灵、碰撞检测等常见的游戏开发任务。 首先,我们需要创建一个游戏窗口。通过arcade提供的Window类,我们可以创建一个指定大小的窗口,并设置背景颜色。 接下来,我们可以创建角色或者其他游戏中的物体,这些物体被称为精灵。arcade提供了Sprite类用于创建精灵。我们可以设置精灵的外观、位置、大小等属性。 在游戏循环中,我们可以监听用户输入,例如键盘按键、鼠标点击等事件。通过事件处理函数,我们可以根据用户的操作来更新精灵的状态。 在平台类游戏中,通常会涉及到角色的跳跃、移动、碰撞检测等功能。arcade提供了一些内置函数和方法来处理这些功能。例如,我们可以使用移动函数来控制角色的水平和垂直移动。我们还可以使用碰撞检测函数来判断角色是否与其他物体发生了碰撞。 此外,arcade还可以处理音效、动画等高级功能,以及绘制背景、地图等元素。它还提供了一些内置的工具函数,用于计算距离、旋转等常见操作。 总的来说,使用Pythonarcade可以实现一个简单而又有趣的平台类游戏。通过利用arcade提供的丰富功能,可以很方便地创建精灵、处理用户输入、实现碰撞检测等游戏开发任务。无论是新手还是有经验的开发者,都可以轻松上手使用arcade制作自己的平台类游戏。 ### 回答3: 使用Pythonarcade可以很方便地制作平台类游戏。首先,我们需要导入arcade。然后创建一个继承自arcade.Window的类,作为游戏窗口。 在窗口类中,我们可以定义一些属性,比如玩家的位置和角色模型等。然后,我们可以使用arcade提供的绘图函数来渲染游戏界面。 接着,我们可以定义一些事件处理函数,比如按键事件或鼠标事件。根据不同的事件,我们可以更新游戏状态,比如移动玩家或触发一些特殊的效果。 在游戏循环中,我们可以使用arcade提供的定时器函数来设定游戏帧率。每一帧,我们可以调用draw函数来绘制游戏界面,并在需要时更新游戏状态。 此外,我们可以使用arcade提供的物理引擎功能,比如重力或碰撞检测,来增加游戏的真实性和挑战性。 最后,当玩家达到游戏的胜利条件或失败条件时,我们可以显示游戏结束画面,并提供一些重新开始或退出游戏的选项。 总的来说,使用Pythonarcade可以很方便地制作一个平台类游戏。通过定义游戏窗口、绘制游戏界面、处理事件和更新游戏状态等步骤,我们可以创建一个有趣且具有挑战性的游戏体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值