演示使用缓冲在屏幕上画格子例子2

"""
演示使用缓冲在屏幕上画格子例子2
This demo shows using buffered rectangles to draw a grid of squares on the
screen.

For me this starts at 0.500 seconds and goes down to 0.220 seconds after the
graphics card figures out some optimizations.
它比前一个例子更快是由于没有重复把坐标和颜色传到显卡中。不是很快是由于还是给每个格子发送了单个的绘画命令到显卡。
It is faster than demo 1 because we aren't loading the vertices and color
to the card again and again. It isn't very fast because we are still sending
individual draw commands to the graphics card for each square.

If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.shape_list_demo_2
"""

import arcade
import timeit

SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
SCREEN_TITLE = "演示使用缓冲在屏幕上画格子例子2"

SQUARE_WIDTH = 5
SQUARE_HEIGHT = 5
SQUARE_SPACING = 10


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

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

        arcade.set_background_color(arcade.color.DARK_SLATE_GRAY)

        self.draw_time = 0
        self.shape_list = None


    def setup(self):
        # --- 创建顶点缓冲对象
        self.shape_list = arcade.ShapeElementList()
        for x in range(0, SCREEN_WIDTH, SQUARE_SPACING):
            for y in range(0, SCREEN_HEIGHT, SQUARE_SPACING):
                shape = arcade.create_rectangle_filled(x, y, SQUARE_WIDTH, SQUARE_HEIGHT, arcade.color.DARK_BLUE)
                self.shape_list.append(shape)


    def on_draw(self):
        """
        Render the screen.
        """

        # 开始渲染
        arcade.start_render()

        # 记录起始时间
        draw_start_time = timeit.default_timer()

        # --- 重画所有格子
        self.shape_list.draw()

        output = f"Drawing time: {self.draw_time:.3f} seconds per frame."
        arcade.draw_text(output, 20, SCREEN_HEIGHT - 40, arcade.color.WHITE, 18)

        self.draw_time = timeit.default_timer() - draw_start_time



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


if __name__ == "__main__":
    main()

 

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 演示使用缓冲在屏幕上画格子例子2
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页