Life

Tags: pgzero, game

https://simplegametutorials.github.io/pygamezero/

life.py
  1import pgzrun, pygame
  2import math
  3
  4GAME_X = 70
  5GAME_Y = 50
  6CELL_SIZE = 10
  7BORDER = 1
  8
  9HUD = 2 * CELL_SIZE
 10CELL_DRAW_SIZE = CELL_SIZE - BORDER
 11WIDTH = GAME_X * CELL_SIZE
 12HEIGHT = GAME_Y * CELL_SIZE + HUD
 13
 14
 15def reset() : # {{{
 16    global grid
 17
 18    grid = []
 19
 20    for y in range(GAME_Y):
 21        grid.append([False for x in range(GAME_X)])
 22    # }}}
 23def update(): # {{{
 24    global selected_x, selected_y
 25
 26    mouse_x, mouse_y = pygame.mouse.get_pos()
 27    selected_x = mouse_x // CELL_SIZE
 28    selected_y = (mouse_y - HUD) // CELL_SIZE
 29    selected_x = clamp(selected_x, 0, GAME_X-1)
 30    selected_y = clamp(selected_y, 0, GAME_Y-1)
 31
 32# }}}
 33def draw(): # {{{
 34    screen.fill((255, 255, 255))
 35    screen.draw.filled_rect(
 36        Rect(0, 0, WIDTH, HUD), color="black")
 37
 38    for y in range(GAME_Y):
 39        for x in range(GAME_X):
 40            # colour = (0, 255, 255) if x == selected_x and y == selected_y else (220, 220, 220)
 41            if x == selected_x and y == selected_y:
 42                colour = (0, 255, 255)
 43            elif grid[y][x]:
 44                colour = (255, 0, 255)
 45            else:
 46                colour = (220, 220, 220)
 47
 48            screen.draw.filled_rect(
 49                    Rect(
 50                        (x*CELL_SIZE, y*CELL_SIZE + HUD), 
 51                        (CELL_DRAW_SIZE, CELL_DRAW_SIZE)),
 52                    color=colour
 53                    )
 54    screen.draw.text(
 55        "X: %d, Y:%d" % (selected_x, selected_y),
 56        (3, 3),
 57        color="orange"
 58        )
 59# }}}
 60def on_mouse_down(pos, button): # {{{
 61    if button == mouse.LEFT:
 62        grid[selected_y][selected_x] = not grid[selected_y][selected_x] 
 63    if button == mouse.RIGHT:
 64        neighbour_count = 0
 65        print("Finding neighbours of grid %d:%d:" % (selected_y, selected_x))
 66
 67        for dy in range(-1, 2):
 68            for dx in range(-1, 2):
 69                if (
 70                        not (dy == 0 and dx ==0)
 71                        and 0 <= (selected_y + dy) < GAME_Y
 72                        and 0 <= (selected_x + dx) < GAME_X
 73                        and grid[selected_y + dy][selected_x + dx]
 74                        ):
 75                    neighbour_count +=1
 76
 77        print("Total neighbours: %d\n%s" % (neighbour_count, 30*"_"))
 78# }}}
 79def on_key_down(): # {{{
 80    global grid
 81    next_grid = []
 82
 83    for y in range(GAME_Y):
 84        next_grid.append([])
 85        for x in range(GAME_X):
 86            # next_grid.append([True for x in range(GAME_X)])
 87            
 88            neighbour_count = 0
 89            # print("Finding neighbours of grid %d:%d:" % (selected_y, selected_x))
 90
 91            for dy in range(-1, 2):
 92                for dx in range(-1, 2):
 93                    if (
 94                            not (dy == 0 and dx ==0)
 95                            and 0 <= (y + dy) < GAME_Y
 96                            and 0 <= (x + dx) < GAME_X
 97                            and grid[y + dy][x + dx]
 98                            ):
 99                        neighbour_count +=1
100                    
101            next_grid[y].append(
102                    neighbour_count == 3 or (grid[y][x] and neighbour_count == 2)
103                    )
104
105    grid = next_grid
106# }}}
107def clamp(val, minv, maxv): return minv if val < minv else maxv if val > maxv else val
108
109reset()
110pgzrun.go()