Arcade 快速的绘制格子普通

python 复制代码
"""
Array Backed Grid

Show how to use a two-dimensional list/array to back the display of a
grid on-screen.

Note: Regular drawing commands are slow. Particularly when drawing a lot of
items, like the rectangles in this example.

For faster drawing, create the shapes and then draw them as a batch.
See array_backed_grid_buffered.py

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

# Set how many rows and columns we will have
ROW_COUNT = 15
COLUMN_COUNT = 15

# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 30
HEIGHT = 30

# This sets the margin between each cell
# and on the edges of the screen.
MARGIN = 5

# Do the math to figure out our screen dimensions
SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN
SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN
SCREEN_TITLE = "Array Backed Grid Example"


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

    def __init__(self, width, height, title):
        """
        Set up the application.
        """

        super().__init__(width, height, title)

        # Create a 2 dimensional array. A two-dimensional
        # array is simply a list of lists.
        self.grid = []
        for row in range(ROW_COUNT):
            # Add an empty array that will hold each cell
            # in this row
            self.grid.append([])
            for column in range(COLUMN_COUNT):
                self.grid[row].append(0)  # Append a cell

        arcade.set_background_color(arcade.color.BLACK)

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

        # This command has to happen before we start drawing
        self.clear()

        # Draw the grid
        for row in range(ROW_COUNT):
            for column in range(COLUMN_COUNT):
                # Figure out what color to draw the box
                if self.grid[row][column] == 1:
                    color = arcade.color.GREEN
                else:
                    color = arcade.color.WHITE

                # Do the math to figure out where the box is
                x = (MARGIN + WIDTH) * column + MARGIN + WIDTH // 2
                y = (MARGIN + HEIGHT) * row + MARGIN + HEIGHT // 2

                # Draw the box
                arcade.draw_rectangle_filled(x, y, WIDTH, HEIGHT, color)

    def on_mouse_press(self, x, y, button, modifiers):
        """
        Called when the user presses a mouse button.
        """

        # Change the x/y screen coordinates to grid coordinates
        column = int(x // (WIDTH + MARGIN))
        row = int(y // (HEIGHT + MARGIN))

        print(f"Click coordinates: ({x}, {y}). Grid coordinates: ({row}, {column})")

        # Make sure we are on-grid. It is possible to click in the upper right
        # corner in the margin and go to a grid location that doesn't exist
        if row < ROW_COUNT and column < COLUMN_COUNT:

            # Flip the location between 1 and 0.
            if self.grid[row][column] == 0:
                self.grid[row][column] = 1
            else:
                self.grid[row][column] = 0


def main():

    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.run()


if __name__ == "__main__":
    main()

这个代码实例展示了如何使用二维数组来在屏幕上显示一个网格。

首先,我们定义了网格的行数和列数:

ROW_COUNT = 15

COLUMN_COUNT = 15

然后,我们设置了每个网格单元格的宽度和高度:

WIDTH = 30

HEIGHT = 30

接下来,我们定义了网格单元格之间的边距:

MARGIN = 5

通过计算,我们得到了屏幕的宽度和高度:

SCREEN_WIDTH = (WIDTH + MARGIN) * COLUMN_COUNT + MARGIN

SCREEN_HEIGHT = (HEIGHT + MARGIN) * ROW_COUNT + MARGIN

SCREEN_TITLE = "Array Backed Grid Example"

然后,我们创建了一个名为MyGame的类,继承自arcade.Window类。这个类是我们的主应用程序类。

在MyGame类的初始化方法中,我们创建了一个二维数组来表示网格。数组的每个元素表示一个网格单元格,初始值为0表示单元格为空。

在on_draw方法中,我们使用循环遍历整个二维数组,根据单元格的值来决定绘制的颜色,然后计算每个单元格的位置,绘制矩形。

在on_mouse_press方法中,我们根据鼠标点击的位置计算出网格单元格的坐标,然后根据坐标来改变对应单元格的值。

最后,在main函数中,我们创建了MyGame对象,并调用arcade.run()方法来启动游戏循环。

相关推荐
plmm烟酒僧20 分钟前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
Jtti3 小时前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
小奥超人3 小时前
PPT文件设置了修改权限,如何取消权?
windows·经验分享·microsoft·ppt·办公技巧
hairenjing112313 小时前
使用 Mac 数据恢复从 iPhoto 图库中恢复照片
windows·stm32·嵌入式硬件·macos·word
九鼎科技-Leo14 小时前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo17 小时前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
黎明晓月21 小时前
Java之字符串分割转换List
java·windows·list
九鼎科技-Leo21 小时前
在 C# 中,ICollection 和 IList 接口有什么区别?
windows·c#·.net
顾辰呀1 天前
实现uniapp-微信小程序 搜索框+上拉加载+下拉刷新
前端·windows
Bunny Chen1 天前
如何缩小PPT演示文稿的大小?
windows·microsoft·powerpoint