Skip to content

Instantly share code, notes, and snippets.

@AntonC9018
Created April 30, 2025 04:30
Show Gist options
  • Save AntonC9018/c7254681c011fb4dd16d3588af2d1e60 to your computer and use it in GitHub Desktop.
Save AntonC9018/c7254681c011fb4dd16d3588af2d1e60 to your computer and use it in GitHub Desktop.
Example from video
import pygame
import random
def main():
pygame.init()
screen = pygame.display.set_mode(
size=(1280, 720),
flags=pygame.RESIZABLE)
clock = pygame.time.Clock()
cell_states = create_game_state()
game_loop(cell_states, screen, clock)
pygame.quit()
CELL_STATE_TRANSPARENT = 0
CELL_STATE_RED = CELL_STATE_TRANSPARENT + 1
CELL_STATE_GREEN = CELL_STATE_RED + 1
CELL_STATE_COUNT = CELL_STATE_GREEN + 1
CELL_INITIAL_STATE = CELL_STATE_TRANSPARENT
CELL_STATES_TYPE = list[list[int]]
def create_game_state() -> CELL_STATES_TYPE:
CELL_ROW_COUNT = 8
CELL_COL_COUNT = 8
ret = [] # cell_states[y][x]
for row_index in range(CELL_ROW_COUNT):
row = []
for col_index in range(CELL_COL_COUNT):
state = random.randrange(0, CELL_STATE_COUNT)
row.append(state)
ret.append(row)
return ret
class ScreenDimensions:
def __init__(
self,
width: int,
height: int):
self.width = width
self.height = height
def process_mouse_click(
cell_states: CELL_STATES_TYPE,
screen_dim: ScreenDimensions,
pos: tuple[int, int]) -> None:
row_count = len(cell_states)
cell_height = screen_dim.height / row_count
row_index = int(pos[1] / cell_height)
row = cell_states[row_index]
col_count = len(row)
cell_width = screen_dim.width / col_count
col_index = int(pos[0] / cell_width)
cell_state = row[col_index]
# new_cell_state = (cell_state + 1) % CELL_STATE_COUNT
new_cell_state = cell_state + 1
if new_cell_state == CELL_STATE_COUNT:
new_cell_state = CELL_INITIAL_STATE
row[col_index] = new_cell_state
def simulate_game_logic(cell_states: CELL_STATES_TYPE) -> None:
row_count = len(cell_states)
for row_index in range(row_count):
row = cell_states[row_index]
col_count = len(row)
for col_index in range(0, col_count - 2):
cell_state = row[col_index]
if cell_state == CELL_STATE_TRANSPARENT:
continue
if not is_enough_in_a_row(
row,
cell_state,
col_index + 1):
continue
for i in range(col_index, col_count):
prev_state = row[i]
if prev_state != cell_state:
break
row[i] = CELL_STATE_TRANSPARENT
def is_enough_in_a_row(
row: list[int],
required_value: int,
col_index_start: int) -> bool:
MAX_COUNT = 2
count = 0
col_count = len(row)
for col_index in range(col_index_start, col_count):
other_value = row[col_index]
if other_value == required_value:
count += 1
else:
break
if count == MAX_COUNT:
break
is_good = count == MAX_COUNT
return is_good
def draw_game_state(
cell_states: CELL_STATES_TYPE,
screen_dim: ScreenDimensions,
screen: pygame.Surface) -> None:
row_count = len(cell_states)
for row_index in range(row_count):
row = cell_states[row_index]
col_count = len(row)
for col_index in range(col_count):
cell_width = screen_dim.width / col_count
cell_height = screen_dim.height / row_count
rect = pygame.Rect(
cell_width * col_index,
cell_height * row_index,
cell_width,
cell_height)
cell_state = row[col_index]
if cell_state == CELL_STATE_TRANSPARENT:
continue
if cell_state == CELL_STATE_RED:
color = (255, 0, 0)
elif cell_state == CELL_STATE_GREEN:
color = (0, 255, 0)
else:
assert False, "Unhandled cell state"
pygame.draw.rect(
surface=screen,
color=color,
rect=rect)
def game_loop(
cell_states: CELL_STATES_TYPE,
screen: pygame.Surface,
clock: pygame.time.Clock) -> None:
running = True
while running:
screen_width, screen_height = screen.get_size()
screen_dim = ScreenDimensions(screen_width, screen_height)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
continue
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button != 1:
continue
pos = event.pos
process_mouse_click(cell_states, screen_dim, pos)
simulate_game_logic(cell_states)
screen.fill((0, 0, 0))
draw_game_state(cell_states, screen_dim, screen)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment