# ---------------------------------------------------------
# MAKECODE ARCADE - HÜCRESEL EVRİM SIMÜLASYONU
# ---------------------------------------------------------
# Izgara Yapılandırması (Kasmaması için 4x4 ölçekleme)
SCALE = 4
GRID_W = scene.screen_width() // SCALE # 40 Hücre
GRID_H = scene.screen_height() // SCALE # 30 Hücre
world = image.create(scene.screen_width(), scene.screen_height())
scene.set_background_image(world)
is_running = False
cursor_x = GRID_W // 2
cursor_y = GRID_H // 2
# Canlı hücre renkleri (MakeCode Palet Numaraları)
COLOR_BG = 15 # Siyah
COLOR_ALIVE = 7 # Yeşil
COLOR_CURSOR = 2 # Kırmızı (İmleç)
# Hücre Durum Matrisi
cells = []
for x in range(GRID_W):
col = []
for y in range(GRID_H):
col.append(0)
cells.append(col)
def count_neighbors(cx: int, cy: int) -> int:
count = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
# Toroidal grid (Ekranın sağından çıkan soldan girer)
nx = (cx + dx) % GRID_W
ny = (cy + dy) % GRID_H
if cells[nx][ny] == 1:
count += 1
return count
def step_simulation():
global cells
next_cells = []
for x in range(GRID_W):
col = []
for y in range(GRID_H):
col.append(0)
next_cells.append(col)
for x in range(GRID_W):
for y in range(GRID_H):
alive = cells[x][y] == 1
neighbors = count_neighbors(x, y)
if alive and (neighbors == 2 or neighbors == 3):
next_cells[x][y] = 1
elif not alive and neighbors == 3:
next_cells[x][y] = 1
else:
next_cells[x][y] = 0
cells = next_cells
def render():
world.fill(COLOR_BG)
alive_count = 0
# 1. Hücreleri Çiz
for x in range(GRID_W):
for y in range(GRID_H):
if cells[x][y] == 1:
alive_count += 1
world.fill_rect(x * SCALE, y * SCALE, SCALE, SCALE, COLOR_ALIVE)
# 2. İmleci Çiz
world.draw_rect(cursor_x * SCALE, cursor_y * SCALE, SCALE, SCALE, COLOR_CURSOR)
# 3. Durum Bilgisini Göster
status_text = "CALISIYOR" if is_running else "DURDURULDU"
world.print_string(status_text + " | CANLI: " + str(alive_count), 2, 2, 1)
# --- Kontroller ---
def on_a_pressed():
global is_running
is_running = not is_running
controller.A.on_event(ControllerButtonEvent.PRESSED, on_a_pressed)
def on_b_pressed():
# B'ye basıldığında imlecin etrafına rastgele yaşam saçar
for dx in range(-1, 2):
for dy in range(-1, 2):
nx = (cursor_x + dx) % GRID_W
ny = (cursor_y + dy) % GRID_H
cells[nx][ny] = randint(0, 1)
controller.B.on_event(ControllerButtonEvent.PRESSED, on_b_pressed)
# Yön Tuşları
def on_left():
global cursor_x
cursor_x = (cursor_x - 1) % GRID_W
controller.left.on_event(ControllerButtonEvent.PRESSED, on_left)
def on_right():
global cursor_x
cursor_x = (cursor_x + 1) % GRID_W
controller.right.on_event(ControllerButtonEvent.PRESSED, on_right)
def on_up():
global cursor_y
cursor_y = (cursor_y - 1) % GRID_H
controller.up.on_event(ControllerButtonEvent.PRESSED, on_up)
def on_down():
global cursor_y
cursor_y = (cursor_y + 1) % GRID_H
controller.down.on_event(ControllerButtonEvent.PRESSED, on_down)
# Oyun Döngüsü
def game_loop():
if is_running:
step_simulation()
render()
game.on_update_interval(150, game_loop)