Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame.draw.rect类型错误:rect参数无效_Python_Pygame_Typeerror - Fatal编程技术网

Python pygame.draw.rect类型错误:rect参数无效

Python pygame.draw.rect类型错误:rect参数无效,python,pygame,typeerror,Python,Pygame,Typeerror,我现在正在写一篇关于Python3和Pygame的跳跃跑。 这次我试图用我的旧函数创建一个“button”类,使游戏可读性更好,性能更好。 这是我的旧代码: def button(msg,x,y,w,h,ic,ac,action=None): time.sleep(0) mouse=pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if x+w > mo

我现在正在写一篇关于Python3和Pygame的跳跃跑。 这次我试图用我的旧函数创建一个“button”类,使游戏可读性更好,性能更好。 这是我的旧代码:

    def button(msg,x,y,w,h,ic,ac,action=None):
        time.sleep(0)
        mouse=pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if x+w > mouse[0] > x and y+h > mouse[1] > y:
            pygame.draw.rect(screen,black,(x,y,w,h),2)
            pygame.draw.rect(screen, ac,(x,y,w,h))
            noevent = False
            if click[0] == 1 and action != None:
                    action()
        else:
            pygame.draw.rect(screen,black,(x,y,w,h),4)
            pygame.draw.rect(screen, ic,(x,y,w,h))
        textSurf, textRect = text_objects(msg, smallText)
        textRect.center = ( (x+(w/2)), (y+(h/2)) )
        screen.blit(textSurf, textRect)
现在我开始用它写一个类:

    class buttons():
    def __init__(self,sort,msg,x,y,w,h,ic,ac,action=None):
            self.type=sort
            self.msg=msg
            self.x=x
            self.y=y
            self.w=w
            self.h=h
            self.ic=ic
            self.ac=ac
            self.action=action
            self.mouse=pygame.mouse.get_pos()
            self.click = pygame.mouse.get_pressed()
            self.noevent = False
    def render(self):
            if self.x+self.w > self.mouse[0] > self.x and self.y+self.h > self.mouse[1] > self.y:
                    pygame.draw.rect(screen,black,(self.x,self.y,self.w,self.h),2)
                    pygame.draw.rect(screen, self.ac,(self.x,self.y,self.w,self.h))
                    if click[0] == 1 and action != None:
                            self.action()
            else:
                    pygame.draw.rect(screen,black,(self.x,self.y,self.w,self.h),4)
                    pygame.draw.rect(screen, self.ic,(self.x,self.y,self.w,self.h))
            self.textSurf, self.textRect = text_objects(self.msg, smallText)
            self.textRect.center = ( (self.x+int(self.w/2)), (self.y+int(self.h/2)))
            screen.blit(self.textSurf, self.textRect)
但它给了我同样的问题:

        pygame.draw.rect(screen,black,(self.x,self.y,self.w,self.h),4)
        TypeError: Rect argument is invalid
我现在不知道该怎么办。我还阅读了有关Stackoverflow主题的其他问题。我找不到问题。

pygame.draw.rect()需要
pygame.rect()
对象不是元组
(x,y,w,h)

因此,保持大小和位置为
pygame.Rect()

或更短

self.rect = pygame.Rect(x, y, w, h)
您将不需要所有这些
self.x
self.y
,等等

pygame.draw.rect(screen, black, self.rect, 4)

screen.blit(self.image, self.rect)

你们可以用鼠标点击按钮

if self.rect.collidepoint(self.mouse):
而不是

if self.x+self.w > self.mouse[0] > self.x and self.y+self.h > self.mouse[1] > self.y:

你也可以使用

  • self.rect.right
    而不是
    self.x+self.w
  • self.rect.bottom
    而不是
    self.y+self.h
  • self.rect.centerx
    而不是
    self.x+(self.w/2)
  • self.rect.centery
    而不是
    self.y+(selfh./2)
  • 等等

您可以在按钮上居中显示文本

self.textRect.center = self.rect.center
而不是

self.textRect.center = ( (self.x+int(self.w/2)), (self.y+int(self.h/2)) )
参见PyGame文档:


顺便说一句:


顺便说一句:为了使代码更具可读性,请使用
CamelCase
类的名称
class按钮
小写的函数/方法和变量的名称
self.text\u image
self.text\u rect


请参阅:

pygame.draw.rect
接受
rect
对象,而不是元组。请尝试
pygame.draw.rect(屏幕,黑色,rect(self.x,self.y,self.w,self.h),4)
谢谢你花时间回答我的问题,但是'self.rect=pygame.rect()'会导致另一个错误:'self.rect=pygame.rect()类型错误:参数必须是rect样式的对象'可能需要参数-直接尝试
pygame.rect(x,y,w,h)
-您有指向文档的链接,因此可以查看文档。
self.textRect.center = ( (self.x+int(self.w/2)), (self.y+int(self.h/2)) )