首页 > 编程问答 >我的沙子模拟遇到问题

我的沙子模拟遇到问题

时间:2024-07-21 04:56:01浏览次数:7  
标签:python pygame cellular-automata

我试图在模拟中添加一个空单元,但某些单元没有被检查。

我尝试添加一个空单元,但底部和右侧的邻居没有被删除。 这是代码:

import pygame
pygame.init()
CELL_SIZE = 5
WIDTH = 1600
HEIGHT = 900
FPS = 60
BLACK = (0, 0, 0)
SAND_COLOR = (255, 255, 0)
MUD_COLOR = (100, 50, 0)
WOOD_COLOR = (200,100,0)
GENERATOR_COLOR = (100,100,100)
VOID_COLOR = (50,50,50)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
cols = WIDTH // CELL_SIZE
rows = HEIGHT // CELL_SIZE
grid = [[0 for _ in range(rows)] for _ in range(cols)]
hue_value = 1
def draw_grid(hue_value):
    color_sand_color = pygame.Color(0)
    for i in range(cols):
        for j in range(rows):
            if grid[i][j] == 1:
                pygame.draw.rect(screen, SAND_COLOR, (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE))
            elif grid[i][j] == 2:
                pygame.draw.rect(screen, MUD_COLOR, (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE))
            elif grid[i][j] == 3:
                pygame.draw.rect(screen, WOOD_COLOR, (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE))
            elif grid[i][j] == 4:
                color_sand_color = pygame.Color(0)
                color_sand_color.hsva = (hue_value,100,100)
                pygame.draw.rect(screen, color_sand_color, (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE))
            elif grid[i][j] == 5:
                pygame.draw.rect(screen, GENERATOR_COLOR, (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE))
            elif grid[i][j] == 6:
                pygame.draw.rect(screen, VOID_COLOR, (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE))
def update_grid(grid):
    new_grid = [[0 for _ in range(rows)] for _ in range(cols)]
    def move_cell(i, j, cell_type):
        if j < rows - 1 and grid[i][j + 1] == 0:
            new_grid[i][j + 1] = cell_type
        elif j == rows - 1:
            new_grid[i][j] = cell_type
        elif i < cols - 1 and grid[i + 1][j + 1] == 0:
            new_grid[i + 1][j + 1] = cell_type
        elif i > 0 and grid[i - 1][j + 1] == 0:
            new_grid[i - 1][j + 1] = cell_type
        else:
            new_grid[i][j] = cell_type
    def simple_move(i, j, cell_type):
        if j < rows - 1 and grid[i][j + 1] == 0:
            new_grid[i][j + 1] = cell_type
        else:
            new_grid[i][j] = cell_type
    def create_static_body(i,j,cell_type):
        new_grid[i][j] = cell_type
    def create_generator_cell(i,j,cell_type):
        new_grid[i][j] = cell_type
        if j < rows - 1 and grid[i][j + 1] == 0:
            new_grid[i][j + 1] = 1
    def create_new_void_cell(i, j, cell_type):
        new_grid[i][j] = cell_type
        neighbors = [
            (i, j + 1),
            (i, j - 1),
            (i + 1, j),
            (i - 1, j)
        ]
        for ni, nj in neighbors:
            if 0 <= ni < cols and 0 <= nj < rows and grid[ni][nj] != 0 and grid[ni][nj] != 6:
                new_grid[ni][nj] = 0
    for i in range(cols):
        for j in range(rows):
            state = grid[i][j]
            if state == 1:
                move_cell(i, j, 1)
            elif state == 2:
                simple_move(i,j,2)
            elif state == 3:
                create_static_body(i,j,3)
            elif state == 4:
                move_cell(i, j, 4)
            elif state == 5:
                create_generator_cell(i,j,5)
            elif state == 6:
                create_new_void_cell(i,j,6)
    return new_grid
current_state = 1
running = True
paused = False
counter = 0
while running:
    screen.fill(BLACK)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            counter += 1
    keys = pygame.key.get_pressed()
    if keys[pygame.K_m]:
        current_state = 2
    if keys[pygame.K_s]:
        current_state = 1
    if keys[pygame.K_w]:
        current_state = 3
    if keys[pygame.K_c]:
        current_state = 4
    if keys[pygame.K_g]:
        current_state = 5
    if keys[pygame.K_v]:
        current_state = 6
    if counter % 2 == 0:
        paused = False
    if counter % 2 == 1:
        paused = True
    mouse_buttons = pygame.mouse.get_pressed()
    if mouse_buttons[0]:
        pos = pygame.mouse.get_pos()
        col, row = pos[0] // CELL_SIZE, pos[1] // CELL_SIZE
        for i in range(2):
            for j in range(2):
                if 0 <= col + i < cols and 0 <= row + j < rows:
                    if grid[col + i][row + j] == 0:
                        grid[col + i][row + j] = current_state
    if mouse_buttons[2]:
        pos = pygame.mouse.get_pos()
        col, row = pos[0] // CELL_SIZE, pos[1] // CELL_SIZE
        for i in range(2):
            for j in range(2):
                if 0 <= col + i < cols and 0 <= row + j < rows:
                    grid[col + i][row + j] = 0
    if not paused:
        grid = update_grid(grid)
        hue_value += 0.5
        if hue_value > 360:
            hue_value = 1
    draw_grid(hue_value)
    pygame.display.flip()
    clock.tick(FPS)
pygame.quit()

任何人都可以帮忙吗?我已经尝试了一切,但没有成功。 如您所见,底部和右侧的单元格没有被删除。


你的沙盒游戏看起来很棒!你遇到的问题是空隙单元格的邻居删除逻辑中有一个小错误。

create_new_void_cell 函数中,你正在检查一个邻居是否在网格范围内以及它是否不是空隙单元格 ( grid[ni][nj] != 0 and grid[ni][nj] != 6 )。问题是,此条件还允许空隙单元格删除其他空隙单元格,从而产生了你不希望看到的视觉伪影。

要解决此问题,你需要修改条件以防止空隙单元格删除其他空隙单元格。你可以通过添加一个检查来确保邻居不是空隙单元格来做到这一点:

def create_new_void_cell(i, j, cell_type):
    new_grid[i][j] = cell_type
    neighbors = [
        (i, j + 1),
        (i, j - 1),
        (i + 1, j),
        (i - 1, j)
    ]
    for ni, nj in neighbors:
        if 0 <= ni < cols and 0 <= nj < rows and grid[ni][nj] != 0 and grid[ni][nj] != 6:
            new_grid[ni][nj] = 0 

通过进行此更改,你的空隙单元格现在将只删除非空隙邻居,从而产生你想要的清除效果。

玩得开心!

标签:python,pygame,cellular-automata
From: 78773904

相关文章

  • 除了curses之外,是否有一个python包可以轻松控制终端的输出?
    我现在正在处理一些小项目,我对GUI的偏好是终端中漂亮的文本界面。我宁愿不强迫用户处理Windowscurses二进制文件,所以我正在寻找不同的选项。我已经发现了asciimatics,但我想考虑所有可能的选择。如果有人有任何经验或知道解决此用例的包,我将不胜感激。谢谢你说的没错......
  • 当值来自函数 python unittest 时,如何模拟全局变量
    我必须在python中模拟全局变量,但变量值来自另一个函数。当我导入文件时,这个函数正在运行,但我想要那里的模拟值。secrets.pyimporttracebackimportloggingimportboto3importosimportjsonlogger=logging.getLogger()logger.setLevel(logging.INFO)secret_......
  • 使用 python print 和 gdb 时出现 BrokenPipeError
    我正在尝试在Linux中运行应用程序并使用Python生成输入:python3-c'print(".....")'|./someapp但出现下一个错误:Exceptionignoredin:<_io.TextIOWrappername='<stdout>'mode='w'encoding='utf-8'>BrokenPipeError:......
  • python 舰队容器
    我正在尝试使用容器在flet中制作一个菜单,它应该是半透明的,但其中的项目不是。我尝试将opacity=1分配给元素,但没有成功-它们与容器一样透明感谢任何帮助我的代码:nickname=ft.TextField(label="xxx",hint_text="xxx")column=ft.Column(controls=[nickname......
  • Python应用程序跨子包共享的配置文件
    我正在构建一个应用程序来控制一些硬件。我在包中实现了不同类型的硬件:电机和测量设备。我的文件结构如下:name_of_my_app/__init__.pymain.pyconfig.iniCONFIG.pymotors/__init__.pyone_kind_of_motor.pymeasurement_devices/......
  • python中时间序列数据的梯度计算
    我正在尝试编写一个函数,它可以从最适合下面的线返回梯度dataframe在浏览了谷歌的几个资源之后,我仍然不确定这是如何完成的。我明白最佳拟合线的计算公式为:y=mx+b将因变量(y)设置为foos,将自变量(x)设置为DateTimeDatafram......
  • 调试用 C 编写的 Python 扩展
    我非常熟悉编写C代码,并且很擅长编写Python代码。我正在尝试学习如何用C编写可以从OSX10.15.7上的Python-3.9.X调用的模块。我已经得到了几个“helloworld”类型的示例,但是对于复杂的示例,我正在努力弄清楚如何调试我编写的C扩展。MWE:src/add.c//......
  • 具有块大小选项的 Python pandas read_sas 因索引不匹配而失败并出现值错误
    我有一个非常大的SAS文件,无法容纳在我的服务器内存中。我只需要转换为镶木地板格式的文件。为此,我使用pandas中chunksize方法的read_sas选项分块读取它。它主要是在工作/做它的工作。除此之外,一段时间后它会失败并出现以下错误。此特定SAS文件有794......
  • 使用 requests 包 python 时打开文件太多
    我正在使用Pythonrequests包向API发出大量请求。然而,在某些时候,我的程序由于“打开的文件太多”而崩溃。当我明确关闭我的会话时,我真的不知道这是怎么回事。我使用以下代码:importrequestsimportmultiprocessingimportnumpyasnps=requests.session()s.keep......
  • Python 是一种选择性解释语言吗?为什么下面的代码不起作用?
    由于程序是从上到下运行的,为什么下面的代码不执行块中的第一行就直接抛出错误?if5>2:print("TwoislessthanFive!")print("Fiveisgreaterthantwo!")错误:文件“/Users/____/Desktop/Pythonpractise/practise.py”,第3行print("五比二大!")Indentati......