Python colliderect与类一起工作吗?如果是,那么如何使用它?

Python colliderect与类一起工作吗?如果是,那么如何使用它?,python,pygame,Python,Pygame,我在游戏中添加“colliderect”时遇到问题。我有一个类用于设置对象的值,还有一个类用于将项目绘制到屏幕上 **编辑** 在整个编辑过程中,我更新了代码中存在的问题。我还没有做出任何决定 如何解决我的课堂问题。所有给出的答案都非常有用,但我仍然有同样的问题。Collite只是不想工作,当我使用pygame.sprite.sprite进行碰撞时,当它接触到另一个sprite.rect时,我会看到一个sprite begs闪烁,但rect的移动不会反转 如果能在这两个问题上提供帮助,我将不胜感

我在游戏中添加“colliderect”时遇到问题。我有一个类用于设置对象的值,还有一个类用于将项目绘制到屏幕上

**编辑** 在整个编辑过程中,我更新了代码中存在的问题。我还没有做出任何决定 如何解决我的课堂问题。所有给出的答案都非常有用,但我仍然有同样的问题。Collite只是不想工作,当我使用pygame.sprite.sprite进行碰撞时,当它接触到另一个sprite.rect时,我会看到一个sprite begs闪烁,但rect的移动不会反转

如果能在这两个问题上提供帮助,我将不胜感激,但在我解决问题之前,我将把问题留给大家。 我遇到的错误

如果选择1.Render().colliderect(球):

AttributeError:“非类型”对象没有属性“colliderect”

我现在遇到的错误

如果选择1.Render().colliderect(球):

AttributeError:“Object”对象没有属性“Collide Rect”

我不知道如何解决这个问题

这是我的球代码

                #Create a class named sprite to use for the paddles and the ball.
            class Object():
                def __init__(self,x,y,width,height,color):
            # Set X value
                     self.x = x
            # Set Y value
                     self.y = y
            # Set Width of object
                     self.width = width
            # Set Height of object
                     self.height = height
            # Set the (default) color of the object
                     self.color = (255,255,255)        
            #attirbute for drawing the sprite(s) to the screen
                def Render(self):
                    pygame.draw.rect(screen,self.color,(self.x,self.y,self.width,self.height))
                    return self
             #Create the sprites       
            paddle1 = Object(50,175,25,150,color[0])
            paddle2 = Object(650,175,25,150,color[1])
            ball = Object(300,250,25,25, color[2])
这是colliderect的代码

            #Commands for ball collision
                if paddle1.Render().colliderect(ball):
                    if True:
                        pass #do something
                if paddle2.Render().colliderect(ball):
                    if True:
                        pass #do something
            # --- Drawing code should go here
除了if语句之外,我还缺少什么使colliderect工作?

您编写的方法
render()
不返回任何值(好的,它返回
None

您是否打算在该方法结束时返回rect

def render(self):
     self.rect = pygame.draw.rect(screen,self.color,(self.x,self.y,self.width,self.height))
     return self.rect
然后,您可以按照自己的意愿链接方法调用:

paddle2.render().colliderect(ball):
…虽然您可能需要计算并设置球的直线并传球:

paddle2.render().colliderect(ball.rect):
我没有使用过
pygame
,所以我只是在盲目地帮助你,但我认为你对类有一些困惑。您的精灵应该来自
pygame.sprite.sprite
。然后你将你的精灵专门化为你想要的那种精灵,但是pygame的精灵实现了大部分的底层逻辑


下面是一个示例sprite实现:

类似的东西可能会起作用。要使用colliderect,Pygame需要
Rect(x,y,width,height)
Rect((x,y),(width,height))

所以基本上你想要:
myrectangle1.colliderect(myrectangle2)
与使用所需的collidepoint类似:
myrectangle1.collidepoint(mypoint1)

或者,如果不希望具有self.rect属性,则需要:

            def Render(self):
                pygame.draw.rect(screen,self.color,pygame.Rect(self.x,self.y,self.width,self.height))
                return self
            def Render(self):
                pygame.draw.rect(screen,self.color, self.rect) #draws the rect
                return self.rect #returns the rect
但是,此渲染方法只会绘制矩形。 如果希望它返回要测试碰撞的对象的矩形属性,则需要:

            def Render(self):
                pygame.draw.rect(screen,self.color,pygame.Rect(self.x,self.y,self.width,self.height))
                return self
            def Render(self):
                pygame.draw.rect(screen,self.color, self.rect) #draws the rect
                return self.rect #returns the rect
要检查碰撞,您需要:

if paddle1.rect.colliderect(ball.rect): #this is why I think having a rect attribute is cleaner than calling pygame.Rect(x,y,w,h) all the time.
或:


Python约定:类的前缀是大写字母,而对象是小写字母。简而言之,
paile1
paile2
Ball
应该是
paile1
paile2
Ball
。由于您违反了惯例,因此给人的印象是
render()
是一种静态方法。顺便说一句,使用and做得很好!令人耳目一新的非常感谢。我有几个帖子被删除了,所以我决定仔细阅读如何恰当地提问。第三次的魅力!我将继续并更改该语法以便于可读性回答否,它只是将“Sprite”注册为类名。如果我将名称更改为类似对象的名称,甚至只是一个,它仍然不起作用等待。。。你需要从pygame的Sprite中派生出来。是的,这就是我认为链接指向的,这就是为什么我说Sprite是为我的类设计的,即使我更改了类名,它也不起作用。你的类名并不重要。您从中派生类的是<代码>导入pygame.sprite;类MySprite(pygame.sprite.sprite):[…]sprite只是一个名称,我可以将其更改为任何名称。Sprite对我来说很有意义,所以我保留了它,我可能应该把它的名字改成square或object以避免混淆。这完全可以解释为什么它会工作!今天我会继续尝试一下。非常感谢你!没问题。只有一件事,如果您只是将“return self.rect”添加到渲染中,那么每次检查冲突时,您也将绘制矩形,因为您正在渲染中调用.draw函数。你可能想避免这种情况,并分开做。在刷新屏幕之前,仅使用.rect属性检查碰撞并在帧末尾绘制内容。是的,我昨晚尝试重新开始,并尝试使用pygame.sprite.sprite,但在绘制时遇到问题,然后再次更新主题绘制。当与精灵一起工作时,我的移动不会在击球时反转,球只会在桨上闪烁,在我移动它时继续直线移动。但现在这都不重要了,因为我很有信心这会对我起作用:)再次感谢SoradeI我尝试了添加上面显示的两种方式,它们都给了我一个错误,对象没有属性Rect(或Rect)。当我给对象一个self.rect=rect(xywh)的值时,我得到一个错误,表示没有定义名称rect。