Python街机游戏arcade模块文本绘画示例集合

"""
 这个例子显示如何在屏幕上画文本和旋转文本,需要arcade模块支持。

 """
import arcade

# 常量定义

SCREEN_WIDTH = 500     # 屏幕宽度     
SCREEN_HEIGHT = 500    # 屏幕高度
SCREEN_TITLE = "Python街机游戏arcade模块文本绘画示例集合"

class MyGame(arcade.Window):
    """
    Main application class.
    """

    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.WHITE)
        self.text_angle = 0
        self.time_elapsed = 0.0

    def update(self, delta_time):
        self.text_angle += 1
        self.time_elapsed += delta_time

    def on_draw(self):
        """
        Render the screen.
        """
        # 此命令应该在所有绘画命令之前调用,它会清空屏幕重画所有对象。 
        arcade.start_render()

        # start_x 和 start_y 是文本的开始坐标. 我们画个点的目的就是为了更清晰地看到文本渲染的位置.
        start_x = 50
        start_y = 450
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 15) # 画个点做个标记
        arcade.draw_text("Simple line of text in 12 point", start_x, start_y, arcade.color.BLACK, 12,font_name='simhei')

        start_x = 50
        start_y = 150
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text("Garamond Text", start_x, start_y, arcade.color.BLACK, 15, font_name='GARA')

        start_x = 50
        start_y = 400
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text("Text anchored 'top' and 'left'.",
                         start_x, start_y, arcade.color.BLACK, 12, anchor_x="left", anchor_y="top")

        start_y = 350
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text("14 point multi\nline\ntext",
                         start_x, start_y, arcade.color.BLACK, 14, anchor_y="top")

        start_y = 450
        start_x = 300
        width = 200
        height = 20
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_lrtb_rectangle_outline(start_x, start_x + width,
                                           start_y + height, start_y,
                                           arcade.color.BLUE, 1)
        arcade.draw_text("Centered Text.",
                         start_x, start_y, arcade.color.BLACK, 14, width=200, align="center")

        start_y = 250
        start_x = 300
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text("Text centered on\na point",
                         start_x, start_y, arcade.color.BLACK, 14, width=200, align="center",
                         anchor_x="center", anchor_y="center")

        start_y = 150
        start_x = 300
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text("Text rotated on\na point", start_x, start_y,
                         arcade.color.BLACK, 14, width=200, align="center", anchor_x="center",
                         anchor_y="center", rotation=self.text_angle)     # self.text_angle每次更新update后它的值会增加1

        start_y = 150
        start_x = 20
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text("Sideways text", start_x, start_y,
                         arcade.color.BLACK, 14, width=200, align="center",
                         anchor_x="center", anchor_y="center", rotation=90.0)

        start_y = 20
        start_x = 50
        arcade.draw_point(start_x, start_y, arcade.color.BLUE, 5)
        arcade.draw_text(f"Time elapsed: {self.time_elapsed:7.1f}",
                         start_x, start_y, arcade.color.BLACK, 14)


def main():
    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.run()


if __name__ == "__main__":
    main()

 

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在arcade分类目录。将固定链接加入收藏夹。