【问题标题】:Drawing squares in diagonal line instead of in grid pattern in pygame在pygame中以对角线而不是网格模式绘制正方形
【发布时间】:2020-06-03 15:30:16
【问题描述】:
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()

上面的代码在从左上角到右下角的对角线上打印正方形,而不是网格模式。有人可以告诉我我做错了什么吗?它与我的前循环或类似的东西有关吗?在这一点上我很迷茫。

【问题讨论】:

    标签: python pygame conways-game-of-life


    【解决方案1】:

    这是一个错字(其实很多问题都是)

    只是改变

        x_pos = x * cell_size
        y_pos = x * cell_size
    

        x_pos = x * cell_size
        y_pos = y * cell_size
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多