簡體   English   中英

在 pygame 中以對角線而不是網格圖案繪制正方形

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM