make the generator use the tilemap instead of random lines, got walls and floor tiles:…
- User
make the generator use the tilemap instead of random lines, got walls and floor tiles:
extends Node2D
var grid_size = Vector2(50, 50) # Change to desired grid size var cell_size = 10 # Change to the size of each cell in pixels var grid = [] var update_rate = 0.5 # Seconds
func _ready(): initialize_grid() update_grid() var timer = Timer.new() timer.set_wait_time(update_rate) timer.set_one_shot(false)
# Create a Callable by referencing the method directly var callable = Callable(self, "update_grid") # Use the Callable to connect the signal timer.connect("timeout", callable) add_child(timer) timer.start()func initialize_grid(): grid = [] for y in range(grid_size.y): var row = [] for x in range(grid_size.x): row.append(randi() % 2) # Randomly set initial state to 0 or 1 grid.append(row)
func draw_grid(): _draw()
func _draw(): for y in range(grid_size.y): for x in range(grid_size.x): var color = Color(1, 1, 1) if grid[y][x] == 1 else Color(0, 0, 0) draw_rect(Rect2(Vector2(x, y) * cell_size, Vector2(cell_size, cell_size)), color, false)
func get_cell(x, y): if x < 0 or x >= grid_size.x or y < 0 or y >= grid_size.y: return 0 return grid[y][x]
func count_neighbors(x, y): var count = 0 for i in range(-1, 2): for j in range(-1, 2): if i == 0 and j == 0: continue count += get_cell(x + i, y + j) return count
func update_grid(): var new_grid = [] for y in range(grid_size.y): new_grid.append(grid[y].duplicate())
for y in range(grid_size.y): for x in range(grid_size.x): var neighbors = count_neighbors(x, y) if grid[y][x] == 1 and (neighbors < 2 or neighbors > 3): new_grid[y][x] = 0 # Cell dies elif grid[y][x] == 0 and neighbors == 3: new_grid[y][x] = 1 # Cell becomes alive grid = new_grid draw_grid() - Assistant