简体   繁体   English

Pygame矩形不绘制

[英]Pygame rectangles not drawing

So I'm not exactly new to python, but I am new to pygame.所以我对python并不完全陌生,但我是pygame的新手。 I've written a basic script that should build a grid of thin rectangles of random shades of grey.我已经编写了一个基本脚本,它应该构建一个由随机灰色阴影的细矩形组成的网格。 as far as I can tell it's building the objects properly and all the relevant variables seem to be working.据我所知,它正在正确构建对象,并且所有相关变量似乎都在工作。 but pygame will not draw the rectangles.但 pygame 不会绘制矩形。 I'm sure its something fairly simple, can someone point out my mistake?我确定它相当简单,有人可以指出我的错误吗?

just to clarify, the prints were for testing purposes只是为了澄清,印刷品用于测试目的

#simple pygame grid drawer
import pygame as pg
import random as r

pg.init()
X = 600
Y = 600
surface_screen = pg.display.set_mode((X , Y))
colors = [(i , i , i) for i in range(255)]

class line:
    def __init__(self , x , y , hight , width):
        self.x = x
        self.y = y
        self.color = r.choice(colors)
        self.hight = hight
        self.width = width
        
lines = []
lines.append([line(x*50 , Y , Y , 15) for x in range(X // 50)])
lines.append([line(X , y*50 , 15 , X) for y in range(Y // 50)])
print("yep1")    
for A in lines:
    for i in A:
        pg.draw.rect(surface_screen , i.color , pg.Rect(i.x , i.y , i.hight , i.width))
        print(f"yep2 {i.color} , X:{i.x} , Y:{i.y} , hight:{i.hight} , width:{i.width}")

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    
        pg.display.flip()
pg.quit()
            
       

The rectangles are out of bounds, because either the x or the y coordinate of the rectangle is 600.矩形超出边界,因为矩形的xy坐标是 600。
Change the following lines of code:更改以下代码行:

lines.append([line(x*50 , Y , Y , 15) for x in range(X // 50)])

lines.append([line(x*50, 0, 15, Y) for x in range(X // 50)]) 

lines.append([line(X , y*50 , 15 , X) for y in range(Y // 50)])

lines.append([line(0, y*50, Y, 15) for y in range(Y // 50)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM