我试图在模拟中添加一个空单元,但某些单元没有被检查。
我尝试添加一个空单元,但底部和右侧的邻居没有被删除。 这是代码:
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