import random
import pygame
# Initializing the main varibales:
width = 600
height = 600
cell_size = 10
cols = int(width / cell_size)
rows = int(height / cell_size)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Conway's Game of Life - Clavio Steltman")
def dead_state(w, h):
# returns 2d array
def random_state(w, h):
# returns a 2d array with values between 0 and 1
def draw_cell(x, y, state):
x_pos = x * cell_size
y_pos = x * cell_size
if state[x][y] == 1:
color = BLACK
elif state[x][y] == 0:
color = WHITE
rect = (x_pos, y_pos, cell_size, cell_size)
pygame.draw.rect(screen, color, rect, 1)
pygame.display.update()
def main():
initial_board = random_state(cols, rows)
print(initial_board)
screen.fill((255, 255, 255))
while True:
for x in range(cols):
for y in range(rows):
draw_cell(x, y, initial_board)
pygame.display.update()
main()
上面的代码在从左上角到右下角的对角线上打印正方形,而不是网格图案。有人可以告诉我我做错了吗?它与我的前循环有关吗?我现在很迷失。
这是一个错字(实际上有很多问题)
只需更改
x_pos = x * cell_size
y_pos = x * cell_size
to
x_pos = x * cell_size
y_pos = y * cell_size