# 绘制Microbit

请根据笔记的写作日期,选择性阅读。

作者: 放羊的大少爷
Home: joynoting.com
写作日期: 2018-06-11
系统: Mac
软件版本: python@3.6.5
arcade@1.3.4
参考:
arcade


# 一、示例代码

偶然看到以前写的代码,使用arcade模块绘制的microbit图形,挺好看的,安利一波。

arcade是一个2d游戏库,当然也可以用来绘制图形,它是基于pyglet而编写的,与传统的2d游戏库一霸pygame相比,个人觉得最突出的就是:

  • 完善的官网文档和大量的示例
  • 友好的api
  • 代码与逻辑相分离的结构

更多与pygame相比的优势,详见官方


import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


def draw_bit():
    # bg
    arcade.draw_rectangle_filled(
        SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 400, 300, arcade.color.BLACK
    )
    # 200, 450
    arcade.draw_triangle_filled(200, 450, 200, 370, 280, 450, arcade.color.PINK)
    arcade.draw_triangle_filled(280, 450, 280, 400, 330, 450, arcade.color.PINK)
    arcade.draw_triangle_filled(330, 450, 330, 420, 360, 450, arcade.color.PINK)

    # 400, 400
    arcade.draw_ellipse_outline(400, 400, 30, 15, arcade.color.PINK, 10)
    arcade.draw_circle_filled(390, 400, 5, arcade.color.PINK)
    arcade.draw_circle_filled(410, 400, 5, arcade.color.PINK)

    # 250, 300
    arcade.draw_rectangle_filled(250, 300, 30, 30, arcade.color.GRAY)
    arcade.draw_rectangle_filled(550, 300, 30, 30, arcade.color.GRAY)
    arcade.draw_circle_filled(250, 300, 10, arcade.color.BLACK)
    arcade.draw_circle_filled(550, 300, 10, arcade.color.BLACK)

    # 200, 150
    for i in range(5):
        arcade.draw_circle_filled(240 + i * 80, 200, 15, arcade.color.WHITE)

    # 340, 260
    for i in range(5):
        for j in range(5):
            arcade.draw_rectangle_filled(
                340 + i * 30, 240 + j * 30, 3, 10, arcade.color.RED
            )


def main():
    """
    This is the main program.
    """

    # Open the window
    arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "microbit")

    arcade.set_background_color(arcade.color.WHITE)

    # Start the render process. This must be done before any drawing commands.
    arcade.start_render()

    draw_bit()

    arcade.finish_render()

    # Keep the window up until someone closes it.
    arcade.run()


if __name__ == "__main__":
    main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

microbit


# 二、可能遇到的错误

在使用 anaconda 运行,如果遇到报错以下报错信息:

ModuleNotFoundError: No module named 'future'
1

解决办法:

conda install future
1

注意:是conda不是pip,即使pip安装成功,也是不可用的。

上次更新时间: 2020/3/13 下午12:44:00