【问题标题】:Shape Class implementation in Python with actual drawn square?Python中的Shape Class实现与实际绘制的正方形?
【发布时间】:2017-11-04 13:31:21
【问题描述】:

我有以下创建形状类的代码,我有两个问题希望得到解答: 1.运行以下代码时,输​​出为:

>>> 
100
100
None
>>> 

最后的“无”是什么,我怎样才能摆脱这个输出?

2。理想情况下,我希望能够(在输出屏幕中)绘制一个正方形。我不想使用 pygame。我确实想知道是否可以集成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):
        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())

【问题讨论】:

  • print(square.print()) 此行在控制台上生成 None ,因为函数没有返回任何内容。要删除它,只需删除外部 print() 并保持剩余。即square.print()
  • 谢谢!关于第二个问题的任何想法!
  • 我在上面:) 我会在某个时候更新
  • 谢谢!添加了更新。 (绘制的代码不正确)但这就是想法。我不知道从哪里让类认龟......
  • 警告,通过 from turtle import *class Shape: 你正在重新定义海龟自己的 Shape 类!您可以通过在您的class Shape:(重新)定义之前和之后打印id(Shape) 来确认这一点。您应该限制您的导入,import turtlefrom turtle import Turtle, Screen,或者将您的 Shape 类重命名为其他名称。

标签: python class draw shape


【解决方案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 ,因为函数没有返回任何内容。要删除它,只需删除外部 print() 并保持剩余。即 square.print()

  2. 是的,这是可能的,我对您的代码进行了少许更改,现在它应该在屏幕上创建正方形。

【讨论】:

  • 谢谢!乌龟功能现在在那里!!!!!!但是......它总是画一个正方形,并且不识别输入参数。
  • 我可以简单地将'forward'变量(例如50)更改为self.x和self.y等
  • @MissComputing 我现在已经进行了更改,所以现在它将利用用户输入,并且 90 是它改变方向的角度。我希望它对你有用:)
  • 太棒了。谢谢!
  • 作为最后一个请求,如果可能的话,您能否评论一下与添加海龟有关的代码。让用户了解它的实现。类的那部分如何......等等(例如canvas = Screen()canvas.setup(800,800)turtle_obj = Turtle()“)
猜你喜欢
  • 2016-06-26
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多