简体   繁体   中英

Pygame rectangles not drawing

So I'm not exactly new to python, but I am new to 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. 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.
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)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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