jeudelaviefail

This commit is contained in:
2022-04-23 20:44:31 +02:00
parent 0395920ee5
commit c9897fd0c3
Executable
+90
View File
@@ -0,0 +1,90 @@
#!/usr/bin/env python
import pygame
import copy
pygame.init()
clock = pygame.time.Clock()
rx, ry = 1000, 1000
gx, gy = 50, 50
surface = pygame.display.set_mode((1000, 1000))
grid = [[0 for i in range(gx)] for i in range(gy)]
color = "#ff0000"
s = 20
grid[34][24] = 1
grid[1][32] = 1
gameoflife = False
t = 60
cx, cy = 0, 0
n = 0
done = False
while not done:
print(done)
newgrid = copy.deepcopy(grid)
surface.fill("#000000")
n += 1
clock.tick(t)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEMOTION:
cx, cy = event.pos
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
gameoflife = not gameoflife
print("input_mode:", gameoflife)
elif event.key == pygame.K_1:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = cx // s, cy // s
newgrid[x][y] = 1 - grid[x][y]
for y, line in enumerate(grid):
for x, case in enumerate(line):
if gameoflife:
neighbours = sum(
grid[x + i][y + j]
for i in range(-1, 2)
for j in range(-1, 2)
if (i, j) != (0, 0)
if 0 <= x + i < gx
if 0 <= y + j < gy
)
# neighbours = 0
# for xi in (x - 1, x, x + 1):
# for yi in (y - 1, y, y + 1):
# if (xi, yi) != (x, y):
# if 0 <= xi < gx and 0 <= yi < gy:
# print(x, y)
# neighbours += grid[xi][yi]
# if neighbours > 2:
# print(x, y)
# print(neighbours)
if grid[x][y] and neighbours < 2:
newgrid[x][y] = 0
elif grid[x][y] and neighbours in (2, 3):
pass
elif grid[x][y] and neighbours > 3:
newgrid[x][y] = 0
elif not grid[x][y] and neighbours == 3:
newgrid[x][y] = 1
done = True
grid = newgrid
if grid[x][y]:
pygame.draw.rect(surface, color, pygame.Rect(x * s, y * s, s, s))
pygame.display.flip()
raise SystemExit