Python中的Shape类实现与实际绘制的方形?

时间:2017-11-04 13:31:22

标签: python class draw shape

我有以下代码创建一个形状类,我有两个问题,我会很感激答案: 1.运行以下代码时,输​​出为:

>>> 
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())

我可能会补充说,在SO上有类似的问题,但没有具体或有用的答案

Using class to draw shapes in turtle

更新:

我尝试过类似的东西,却无法让它发挥作用。我想我需要在构造函数中的某处初始化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())

1 个答案:

答案 0 :(得分:1)

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()
  1. print(square.print())此行在控制台上生成None,因为函数未返回任何内容。要删除它,只需删除外部打印()并保持剩余。即square.print()

  2. 是的,这是可能的,我对你的代码做了一些改动,现在它应该在屏幕上创建方块。