![](/img/trans.png)
[英]How do I change the Pygame coordinate system so that the center of the window is (0,0)?
[英]How do I set the default value Point(0,0) for center and radius = 1?
請幫我修改Circle類,(1)設置一個默認實例。
圓類使用Point對象為中心。 使用center和radius關鍵字,將center作為第一個參數。 使用repr和str 。 將點對象和半徑值分配給圓時, 分配值的輸出應如下所示
沒有輸入的圓的默認值為:以Point對象的默認實例為中心,半徑為1。
如果我要更改以下內容
def __repr__(self):
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center.x,self.center.y,self.radius)
對此:
def __repr__(self):
return "Circle(center=Point({0}, {1}), radius {2})"
.format(self.center[0],self.center[1],self.radius)
這將產生一個錯誤,因為分配給中心Point(2,3)的點對象不支持索引。
import math
class Point:
def __init__(self,x=0,y=0):
self.x = x
self.y = y
def __iter__(self):
yield self.x
yield self.y
def __add__(self,other):
return Point(self.x+other.x,self.y+other.y)
def __mul__(self,n):
return Point(self.x*n,self.y*n)
def __rmul__(self,n):
return Point(n*self.x,n*self.y)
@classmethod
def from_tuple (cls, self=(0,0)):
return cls(*self)
def loc_from_tuple(self,t=(0,0)):
self.x=t[0]
self.y=t[1]
def __str__(self):
return "Point at ({0}, {1})".format(self.x,self.y)
def __repr__(self):
return"Point(x={0}, y={1})".format(self.x,self.y)
@property
def magnitude(self):
return math.sqrt(self.x**2+self.y**2)
def distance (self, other):
return math.sqrt((self.x-other.x)**2+(self.y-other.y)**2)
def shift(p1, p2):
mx = p1.x+p2.x
my = p1.y+p2.y
return Point(mx,my)
class Circle(Point):
def __init__(self, center=(0,0), radius=1):
Point.__init__(self,center)
self.center=center
self.radius=radius
def __str__(self):
return "Circle with center at ({0}, {1}) and radius {2}"
.format(self.center.x, self.center.y, self.radius)
def __repr__(self):
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center.x,self.center.y,self.radius)
def __add__(self,other):
return Circle(
Point(self.center.x+other.center.x,
self.center.y+other.center.y),
self.radius+other.radius)
@classmethod
def from_tuple(cls, center,radius):
return cls(center, radius)
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, radius):
if radius<0:
raise ValueError('The radius cannot be negative')
self._radius=radius
@property
def diameter(self):
return self._radius*2
@diameter.setter
def diameter(self, diameter):
if diameter<0:
raise ValueError('The radius cannot be negative')
self._radius = diameter/2
@property
def area(self):
return math.pi*self.radius**2
Center是一個元組,因此它沒有屬性x
。 在這里定義中心:
class Circle(Point):
def __init__(self, center=(0,0), radius=1):
Point.__init__(self,center)
self.center=center
在這里,您致電center.x
def __repr__(self):
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center.x,self.center.y,self.radius)
一個快速的解決方法是將__repr__
更改為以下內容:
def __repr__(self):
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center[0],self.center[1],self.radius)
這樣,您將為元組建立索引。 另一種方法是使center成為字典,例如center = {'x':0, 'y'0}
。 然后__repr__
將__repr__
工作,盡管您必須遍歷程序的其余部分以確保一致性。
錯誤消息告訴您您需要了解的所有信息:AttributeError:'tuple'對象沒有屬性'x'
令人討厭的一句話是:
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center.x,self.center.y,self.radius)
對象center
是一個元組,這意味着其變量是通過索引而不是屬性名稱訪問的。 請嘗試:
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.center[0],self.center[1],self.radius)
更好的方法是實際利用Circle
從Point
繼承的繼承關系並直接訪問x
和y
如果已經有了center
,則沒有理由將center
保留為實例變量:
return "Circle(center=Point({0}, {1}), radius={2})"
.format(self.x,self.y,self.radius)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.