[英]Drawing squares in diagonal line instead of in grid pattern in pygame
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
至
x_pos = x * cell_size
y_pos = y * cell_size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.