如何从Pygame列表中绘制矩形

发布于 2025-01-23 21:25:58 字数 839 浏览 0 评论 0原文

它说rect参数是无效的,但是当我给出相同的rect而无需变量时,这是负责它的代码的一部分,

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()

我也尝试了self.obstacleses(0)在pygame.draw.Rect中,但随后我获得列表对象不是可呼叫

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2025-01-30 21:25:59

您必须在循环中绘制矩形:

for obstacle in self.obstacleses:
    pygame.draw.rect(screen, (255, 0, 0), obstacle)

You have to draw the rectangles in a loop:

for obstacle in self.obstacleses:
    pygame.draw.rect(screen, (255, 0, 0), obstacle)
我还不会笑 2025-01-30 21:25:59
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)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文