【发布时间】:2023-04-01 11:27:01
【问题描述】:
我有一个文本文档,其中只有 1 和 0 以 80 x 80 正方形的形式写入,即 80 行和 80 列仅写入 1 和 0。我需要创建一个代码来绘制一个 80 x 80 的正方形并将这些框填充为红色,而不是 1。
import pygame
import os
# create path to find my document
path = os.path.realpath('future.txt')
task = open(path, mode = 'r',encoding = 'utf-8')
#create screen
screen = pygame.display.set_mode((800,800))
white = [255, 255, 255]
red = [255, 0, 0]
x = 0
y = 0
#intepreter string as a list
for line in task:
line = line.replace('\n', '')
line = list(line)
# nested loop
for j in range(0,80):
for i in range(0,79):
pygame.draw.rect(screen, white, (x, y, 10, 10), 1)
x += 10
if line[i] == '1':
pygame.draw.rect(screen, red, (x, y, 9, 9))
if x == 800:
x = 0
y += 10
while pygame.event.wait().type != pygame.QUIT:
pygame.display.flip()
这是我的代码。 Python 版本 3。
【问题讨论】:
-
请说明您的问题。
-
我的代码中有一个包。它只为我的 pygame 广场画了第一条线。我认为它发生在我的嵌套循环中,但我不明白出了什么问题。我的问题是这个包在哪里,我应该如何更改我的程序?
标签: python arrays python-3.x pygame nested-loops