Untitled
unknown
plain_text
2 years ago
1.9 kB
5
Indexable
import pygame
import numpy as np
# Definicja klas
class Cell:
def __init__(self, x, y, size):
self.x = x
self.y = y
self.size = size
self.is_alive = False
def draw(self, screen):
if self.is_alive:
pygame.draw.rect(screen, (255, 255, 255), (self.x * self.size, self.y * self.size, self.size, self.size))
else:
pygame.draw.rect(screen, (0, 0, 0), (self.x * self.size, self.y * self.size, self.size, self.size))
class Board:
def __init__(self, width, height, cell_size):
self.width = width
self.height = height
self.cell_size = cell_size
self.grid = np.zeros((width, height), dtype=Cell)
for x in range(width):
for y in range(height):
self.grid[x, y] = Cell(x, y, cell_size)
def draw(self, screen):
screen.fill((0, 0, 0))
for x in range(self.width):
for y in range(self.height):
self.grid[x, y].draw(screen)
for x in range(0, self.width * self.cell_size, self.cell_size):
pygame.draw.line(screen, (255, 255, 255), (x, 0), (x, self.height * self.cell_size))
for y in range(0, self.height * self.cell_size, self.cell_size):
pygame.draw.line(screen, (255, 255, 255), (0, y), (self.width * self.cell_size, y))
# Inicjalizacja Pygame
pygame.init()
# Ustawienia planszy
WIDTH, HEIGHT = 800, 600
CELL_SIZE = 10
# Utworzenie planszy i ekranu
board = Board(WIDTH // CELL_SIZE, HEIGHT // CELL_SIZE, CELL_SIZE)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game of Life")
# Pętla główna
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Wyświetlanie planszy
board.draw(screen)
pygame.display.flip()
# Zamknięcie Pygame
pygame.quit()
Editor is loading...
Leave a Comment