我如何得到这个值?(Python)
我有我如何得到这个值?(Python),python,class,collision,detect,Python,Class,Collision,Detect,我有 类点: 定义初始化(self、initX、initY): self.x=initX self.y=initY def getX(自我): 返回self.x def getY(自我): 回归自我 定义(自我): 返回“x=“+str(self.x)+”,y=“+str(self.y)” 类矩形: 定义初始化(self,initP,initW,initH): self.\uuu location=initP 自身宽度=初始宽度 自身高度=初始高度 def getWidth(自): 返回自身宽度
类点:
定义初始化(self、initX、initY):
self.x=initX
self.y=initY
def getX(自我):
返回self.x
def getY(自我):
回归自我
定义(自我):
返回“x=“+str(self.x)+”,y=“+str(self.y)”
类矩形:
定义初始化(self,initP,initW,initH):
self.\uuu location=initP
自身宽度=初始宽度
自身高度=初始高度
def getWidth(自):
返回自身宽度
def getHeight(自):
返回自我高度
def getLocation(自):
返回自我位置
#---------------------------------------------------------------
#串
定义(自我):
返回“x=“+str(self.\uu location.x)+”,y=“+str(self.\uu location.y)+”,Width=“+str(self.getWidth())+”,Height=“+str(self.getHeight())
def区域(自身):
返回self.getWidth()*self.getHeight()
def计算器参数(自身):
返回self.getWidth()*2+self.getHeight()*2
def转置(自):
温度=自身宽度
自。\宽度=自。\高度
自身高度=温度
def附件(自身、其他P):
return((self.getWidth()+self.getLocation().getX())>otherP.getX()\
和(self.getLocation().getX())otherP.getY())\
和self.getLocation().getY()
我正在试图检测碰撞。我有点卡住了。(检测碰撞)
我在获取从point类到rectangle类的值时遇到问题。
有人知道吗
检测碰撞功能错误。我正在测试,我可以用getHeight()获得宽度和高度,但无法获得点内的值
我在获取从point类到rectangle类的值时遇到问题
我认为你需要通读一本很好的课堂教程。也许,也许是第三方教程。StackOverflow不是学习基本概念的好地方
实际上,您不希望从point类获取值,而是希望从特定的point实例获取值。毕竟,世界上有很多点,每个点都有不同的x
和y
值,并且您试图检查某个特定点是否与矩形碰撞
你怎么知道是哪种情况?将一个作为参数。然后您可以访问该对象的成员、方法等,就像您对字符串或任何其他对象所做的一样
class Point:
def __init__(self, initX, initY):
self.x = initX
self.y = initY
def getX(self):
return self.x
def getY(self):
return self.y
def __str__(self):
return "x=" + str(self.x) + ", y=" + str(self.y)
class Rectangle:
def __init__(self, initP, initW, initH):
self.__location = initP
self.__width = initW
self.__height = initH
def getWidth(self):
return self.__width
def getHeight(self):
return self.__height
def getLocation(self):
return self.__location
#---------------------------------------------------------------
#string
def __str__(self):
return "x=" + str(self.__location.x) + ", y=" + str(self.__location.y) +", Width=" + str(self.getWidth()) + ", Height=" +str(self.getHeight())
def area(self):
return self.getWidth() * self.getHeight()
def calculatePerimeter(self):
return self.getWidth()*2 +self.getHeight()*2
def transpose(self):
temp = self.__width
self.__width = self.__height
self.__height = temp
def encloses(self, otherP):
return ((self.getWidth() + self.getLocation().getX()) > otherP.getX()\
and (self.getLocation().getX()) <otherP.getX() \
and (self.getHeight() + self.getLocation().getY()) >otherP.getY()\
and self.getLocation().getY() < otherP.getY())
def computeDiagonal(self):
d = (self.getWidth()**2 + self.getHeight()**2) ** 0.5
return d
def detectCollision(firstRectangle, secondRectangle):
print(firstRectangle.getWidth())
print(secondRectangle)
first = Rectangle(Point(1,0), 4, 3)
second = Rectangle(Point(4,0), 4, 3)
Rectangle.detectCollision(first, second)
现在,您已经向我们展示了更多的代码,看起来您正在尝试碰撞检查两个矩形,更重要的是,您的问题是,您的矩形使用一个
点作为其左上角原点,您不知道如何访问该点的坐标
根据您的描述,“我正在测试,我可以通过getHeight()获得宽度和高度,但我无法获得点
内的值”,您似乎仍然缺少这里的关键问题。您不希望获取点
内的值<代码>点
是用于创建实际点对象的a级工厂。您希望获取其中一个实际点对象内的值,该点对象已存储在矩形对象的\u位置
中,并通过getLocation
方法提供。(正如我已经解释过的,您应该去掉那些getter方法,只需要有一个location
属性,但是现在让我们先忘掉它。)
因此,获取感兴趣的特定点对象的方法是在矩形上调用getLocation()
,然后获取该特定点对象的x和y值的方法是调用其getX
和getY
方法。下面是一个使用所有这些方法的示例:
>>> rect = Rectangle(10, 10, 20, 20)
>>> point1 = Point(5, 5)
>>> rect.collision_check(point1)
False
>>> point2 = Point(15, 15)
>>> rect.collision_check(point2)
True
也可以将这些调用组合到一个表达式中:
firstLocation = firstRectangle.getLocation()
firstLeft = firstLocation.getX()
所以,你可以这样做:
firstLeft = firstRectangle.getLocation().getX()
def检测碰撞(第一个矩形,第二个矩形):
firstLeft=firstRectangle.getLocation().getX()
firstRight=firstLeft+firstRectangle.getWidth()
#顶部和底部以及第二个矩形的类似代码
return((左一)作为旁注,在Python中几乎不需要这样的getter/setter函数。只需访问mypoint.x
,不需要mypoint.getX()
。此外,如果这是Python 2.x,则需要类点(对象):
,而不是类点:
。请添加更多关于您的矩形类的信息,以及您试图在其中获取什么以及如何获取?它在那里。您可以看到它如何打印(firstRectangle.getWidth())和打印(secondRectangle)嘿,所以我这样做是为了看看我是否理解您。def detectCollision(self,Point):return Point.getX()我得到的矩形对象没有属性x?@user3159537:首先,你从哪里得到返回点.getX()
的?你想调用getX()
的Point
,你传入的参数,而不是Point
,类。第二,我不知道你是如何得到“矩形对象没有属性x的”当我没有在任何地方编写过self.x
时;很明显,您正在修改我的代码以适应您对Rectangle
的定义,但如果您这样做,您必须实际适应您的定义。
firstLeft = firstRectangle.getLocation().getX()
def detectCollision(firstRectangle, secondRectangle):
firstLeft = firstRectangle.getLocation().getX()
firstRight = firstLeft + firstRectangle.getWidth()
# similar code for top and bottom, and for second rectangle
return ((firstLeft <= secondLeft <= firstRight or
firstLeft <= secondRight <= firstRight) and
(firstTop <= secondTop <= firstBottom or
firstTop <= secondBottom <= firstBottom))