【发布时间】:2016-06-06 23:08:41
【问题描述】:
在我的游戏中添加“colliderect”时遇到问题。我有这个类用于设置对象的值,然后有一个用于将项目绘制到屏幕上。
**已编辑** 在整个编辑过程中,我更新了我的代码存在的问题。我还没有做任何的贝尔 ow 解决方案适用于我的班级。给出的所有答案都非常有帮助,但我仍然遇到同样的问题。 Collierect 只是不想工作,当我使用 pygame.sprite.Sprite 进行碰撞时,当它接触另一个 sprite.rect 时,我得到一个 sprite 请求闪烁,但 rect 的运动并没有逆转。
我们将非常感谢您对这些问题的任何一个提供帮助,但在我解决问题之前,我会将问题留待解决。 我遇到的错误
如果 paddle1.Render().colliderect(ball):
AttributeError: 'NoneType' 对象没有属性 'colliderect'
我现在遇到的错误
如果 paddle1.Render().colliderect(ball):
AttributeError: 'Object' 对象没有属性 'colliderect'
我不知道如何解决这个问题。
这是我的球代码
#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 工作?
【问题讨论】:
-
Python 约定:类以大写字母为前缀,但对象为小写。简而言之,
Paddle1、Paddle2和Ball应该是paddle1、paddle2和ball。由于您违反约定,因此给人的印象是render()是一个静态方法。 -
顺便说一句,stackoverflow.com/help/how-to-ask 和 stackoverflow.com/help/mcve 做得很好!清爽!
-
谢谢!我有几个被删除的帖子,所以我决定阅读如何正确提问。第三次魅力!
-
我将继续更改该语法以提高可读性