Spaces:
Runtime error
Runtime error
Commit
·
678803d
1
Parent(s):
4b4be2a
add example output
Browse files
__pycache__/ai.cpython-311.pyc
DELETED
|
Binary file (2.31 kB)
|
|
|
__pycache__/cpu_ai.cpython-311.pyc
DELETED
|
Binary file (2.79 kB)
|
|
|
__pycache__/github_manager.cpython-311.pyc
DELETED
|
Binary file (3.17 kB)
|
|
|
examples/conway.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pygame
|
| 2 |
+
import asyncio
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# define some colors
|
| 6 |
+
BLACK = (0, 0, 0)
|
| 7 |
+
WHITE = (255, 255, 255)
|
| 8 |
+
GREEN = (0, 255, 0)
|
| 9 |
+
RED = (255, 0, 0)
|
| 10 |
+
BLUE = (0, 0, 255)
|
| 11 |
+
YELLOW = (255, 255, 0)
|
| 12 |
+
pygame.init()
|
| 13 |
+
screen = pygame.display.set_mode((500,500))
|
| 14 |
+
pygame.display.set_caption("Conway's Game of Life")
|
| 15 |
+
|
| 16 |
+
# create a grid of squares
|
| 17 |
+
grid = [[False for _ in range(500)] for _ in range(500)]
|
| 18 |
+
|
| 19 |
+
# create a font for displaying numbers
|
| 20 |
+
font = pygame.font.Font('freesansbold.ttf', 30)
|
| 21 |
+
|
| 22 |
+
# create a function to display the grid
|
| 23 |
+
def draw_grid():
|
| 24 |
+
for row in range(500):
|
| 25 |
+
for col in range(500):
|
| 26 |
+
if grid[row][col]:
|
| 27 |
+
pygame.draw.rect(screen, GREEN, (col*50, row*50, 50, 50))
|
| 28 |
+
else:
|
| 29 |
+
pygame.draw.rect(screen, BLACK, (col*50, row*50, 50, 50))
|
| 30 |
+
pygame.display.flip()
|
| 31 |
+
|
| 32 |
+
# create a function to update the grid
|
| 33 |
+
def update_grid(neighbors):
|
| 34 |
+
for row in range(500):
|
| 35 |
+
for col in range(500):
|
| 36 |
+
# get the number of neighbors
|
| 37 |
+
num_neighbors = sum([grid[r][c] for r, c in neighbors if grid[r][c]])
|
| 38 |
+
# update the grid
|
| 39 |
+
grid[row][col] = num_neighbors == 3 or (num_neighbors == 2 and grid[row][col])
|
| 40 |
+
|
| 41 |
+
# create a function to display the neighbors
|
| 42 |
+
def draw_neighbors(row, col):
|
| 43 |
+
neighbors = [(r, c) for r, c in [(row-1, col), (row-1, col-1), (row, col-1), (row+1, col-1), (row+1, col), (row+1, col+1), (row, col+1), (row-1, col+1)]]
|
| 44 |
+
for neighbor in neighbors:
|
| 45 |
+
if neighbor in grid:
|
| 46 |
+
update_grid(neighbors)
|
| 47 |
+
else:
|
| 48 |
+
grid.append(neighbor)
|
| 49 |
+
update_grid(neighbors)
|
| 50 |
+
draw_grid()
|
| 51 |
+
|
| 52 |
+
# create a function to handle the events
|
| 53 |
+
def event_handler(event):
|
| 54 |
+
if event.type == pygame.QUIT:
|
| 55 |
+
pygame.quit()
|
| 56 |
+
quit()
|
| 57 |
+
elif event.type == pygame.KEYDOWN:
|
| 58 |
+
if event.key == pygame.K_ESCAPE:
|
| 59 |
+
pygame.quit()
|
| 60 |
+
quit()
|
| 61 |
+
elif event.key == pygame.K_r:
|
| 62 |
+
draw_grid()
|
| 63 |
+
elif event.key == pygame.K_SPACE:
|
| 64 |
+
draw_neighbors(50, 50)
|
| 65 |
+
|
| 66 |
+
# create the main function
|
| 67 |
+
async def main():
|
| 68 |
+
while True:
|
| 69 |
+
# handle events
|
| 70 |
+
for event in pygame.event.get():
|
| 71 |
+
event_handler(event)
|
| 72 |
+
# update the grid
|
| 73 |
+
await asyncio.sleep(1)
|
| 74 |
+
|
| 75 |
+
# run the game
|
| 76 |
+
asyncio.run(main())
|