在Python中使用实际绘制的正方形实现形状类?
我有以下创建shape类的代码,我有两个问题需要解答: 1.运行以下代码时,输出为:在Python中使用实际绘制的正方形实现形状类?,python,class,draw,shape,Python,Class,Draw,Shape,我有以下创建shape类的代码,我有两个问题需要解答: 1.运行以下代码时,输出为: >>> 100 100 None >>> 最后的“无”是什么,我怎样才能去掉这个输出? 2。理想情况下,我希望能够(在输出屏幕中)绘制一个正方形。我不希望使用pygame。我确实想知道是否有可能集成海龟来做这件事,但不知道如何开始?有没有关于使用海龟来实现这一点的方法的建议,或者任何其他天才的建议 from turtle import* class Shape:
>>>
100
100
None
>>>
最后的“无”是什么,我怎样才能去掉这个输出?
2。理想情况下,我希望能够(在输出屏幕中)绘制一个正方形。我不希望使用pygame。我确实想知道是否有可能集成海龟来做这件事,但不知道如何开始?有没有关于使用海龟来实现这一点的方法的建议,或者任何其他天才的建议
from turtle import*
class Shape:
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y):
self.x=x #the shape has the attribute x (width)
self.y=y #the shape has the attribute y (height)
description="The shape has not yet been brought into being"
author="No one has yet claimed authorship of this shape"
def area(self):
return self.x*self.y
def perimeter(self):
return 2*self.x+2*self.y
def describe(self,text):
self.description =text
def authorName(self,text):
self.author=text
def scaleSize(self,scale):
self.x=self.x*scale
self.y=self.y*scale
def print(self):
print(self.x)
print(self.y)
square=Shape(100,100)
print(square.print())
我可以补充一点,在这方面也有类似的问题,但没有具体或有用的答案
更新:
我试过这样的东西,但没能奏效。我想我需要在构造函数中的某个地方初始化turtle-但是在哪里以及如何初始化
from turtle import*
class Shape:
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y):
self.x=x #the shape has the attribute x (width)
self.y=y #the shape has the attribute y (height)
description="The shape has not yet been brought into being"
author="No one has yet claimed authorship of this shape"
def area(self):
return self.x*self.y
def perimeter(self):
return 2*self.x+2*self.y
def describe(self,text):
self.description =text
def authorName(self,text):
self.author=text
def scaleSize(self,scale):
self.x=self.x*scale
self.y=self.y*scale
def print(self,shapename):
print("This shape is a", shapename, "with dimensions:>",self.x,"by",self.y)
def draw(self):
turtle.forward(self.x)
turtle.left(90)
turtle.forward(se.f.x)
turtle.left(90)
turtle.forward(self.y)
turtle.left(90)
turtle.forward(self.y)
turtle.left(90)
square=Shape(100,100)
square.print("square")
print("The perimeter is:",square.perimeter())
print(square.draw())
print(square.print())
由于函数不返回任何内容,所以这一行在控制台上不生成任何内容。要删除它,只需删除外部打印()并保留剩余的打印。i、 e.square.print()
谢谢!对第二个问题有什么想法吗!我在上面:)我会随时更新谢谢!添加了一个更新。(draw的代码不正确)但这就是想法。我不知道从哪里可以让类识别海龟……警告,通过执行海龟导入中的和类形状:
您正在重新定义海龟自己的形状
类!您可以通过在类形状:
(重新)定义之前和之后打印id(形状)
来确认这一点。您应该限制您的导入,导入海龟
或从海龟导入海龟,屏幕
,或者将您的形状
类重命名为其他类。谢谢!海龟功能现在就在那里!!!!!!但是…它总是画一个正方形,并且不识别输入参数。我可以简单地将“正向”变量(例如50)更改为self.x和self.y吗etc@MissComputing我现在已经做了修改,所以现在它将使用用户输入,90是它改变方向的角度。我希望它对你有用:)太好了。非常感谢。作为最后一个请求,如果可能的话,请您对与添加turtle相关的代码进行注释。让用户了解它的实现。类的这一部分是怎样的…等等(例如canvas=Screen()canvas.setup(800800)turtle\u obj=turtle())
from turtle import*
class Shape:
canvas = Screen() # creating object of screen class
canvas.setup(800,800) # this will setup window on screen with dimension 800x800
turtle_obj = Turtle() # creating object of turtle class which will be used to plot square on window
#self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class
#to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name)
def __init__(self,x,y):
self.x=x #the shape has the attribute x (width)
self.y=y #the shape has the attribute y (height)
description="The shape has not yet been brought into being"
author="No one has yet claimed authorship of this shape"
def area(self):
return self.x*self.y
def perimeter(self):
return 2*self.x+2*self.y
def describe(self,text):
self.description =text
def authorName(self,text):
self.author=text
def scaleSize(self,scale):
self.x=self.x*scale
self.y=self.y*scale
def print(self):
print self.x
print self.y
# now we are referring to class object turtle using self.turtle_obj and
# calling forward() function which will move plotting cursor in forward direction
# upto x pixels and left() will rotate direction by 90 degree
self.turtle_obj.forward(self.x)
self.turtle_obj.left(90)
self.turtle_obj.forward(self.y)
self.turtle_obj.left(90)
self.turtle_obj.forward(self.x)
self.turtle_obj.left(90)
self.turtle_obj.forward(self.y)
self.turtle_obj.left(90)
square=Shape(100,100)
square.print()