简体   繁体   中英

How to draw rectangles from a list in pygame

It says rect argument is invalid but when I give the same rect without a variable it works, here is the part of a code responsible for it

class Background:
    def __init__(self, background):
        self.background = background
        self.endpoint = 5590
        self.bx = 0
        self.scrolling_right = False
        self.scrolling_right = True
        self.obstacleses = []
    
    def obstacles(self):
        self.obstacleses = pygame.Rect(300, 300, 20, 60)
        print(self.obstacleses)

    def surface(self):
        screen.blit(self.background, (self.bx, 0))
        screen.blit(templar1.image, templar1.pos)
        screen.blit(Rogue.image, Rogue.pos)  
        pygame.draw.rect(screen, (255, 0, 0), self.obstacleses)
        pygame.display.update()

I also tried self.obstacleses(0) in pygame.draw.rect but then i get list object is not callable .

You have to draw the rectangles in a loop:

for obstacle in self.obstacleses:
    pygame.draw.rect(screen, (255, 0, 0), obstacle)
import pygame ... screen = pygame.display.set_mode((WIDTH, HEIGHT)) RECTS = [] RECTS.append( pygame.Rect(10, 10, 60, 60) ) # Use this in mainloop for rect in RECTS: pygame.draw.rect(screen, GREEN, rect)

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