首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类中的Paddle对象不会出现

类中的Paddle对象不会出现
EN

Stack Overflow用户
提问于 2022-09-05 15:10:03
回答 1查看 37关注 0票数 0

我想做一个扑鸟游戏,目前正在尝试把桨a变成一个类,但是它不会出现在它应该出现的时候。Im也没有收到任何错误消息,代码也可以运行,但是没有任何paddle (类中的对象)的迹象。这是代码:

代码语言:javascript
复制
class Paddle:
Paddle = turtle.Turtle()
Paddle.penup()
Paddle.goto(0,0)
def __init__(self,shape,color,stretch_wid,stretch_len):
    self.shape = shape
    self.color = color
    self.stretch_wid = stretch_wid
    self.stretch_len = stretch_len
paddle_x = Paddle("square","white",5,5)
paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=20,stretch_len=5)
paddle_a.penup()
paddle_a.goto(150,-250)

我已经试着在类下的"class Paddle:“之后放置3行代码,然后将"Paddle”转换为paddle_x,但是它说"'Paddle‘对象没有属性'penup'“。第一次问问题。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-05 22:06:53

在处理了一些多边形类的对象继承之后,我相信您需要在"Paddle“对象的类定义中引用"Turtle”对象,如下代码片段所示。

代码语言:javascript
复制
import turtle

class Paddle(turtle.Turtle):            # Define as a child of the Turtle object

    def __init__(self,shape,color,stretch_wid,stretch_len):     
        turtle.Turtle.__init__(self)    # Use the Turtle object's initialization
        self.shape = shape
        self.color = color
        self.stretch_wid = stretch_wid
        self.stretch_len = stretch_len
        
paddle_x = Paddle("square","white",5,5)
paddle_x.penup()
paddle_x.goto(0,0)
print(paddle_x.shape)

paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=20,stretch_len=5)
paddle_a.penup()
paddle_a.goto(150,-250)

它某种程度上利用了基于父类(或"super")类初始化子对象的C++方法。当尝试删除该代码片段时,屏幕上会简短地显示一个海龟指针,并打印出测试中请求的paddle值。

代码语言:javascript
复制
@Una:~/Python_Programs/Paddle$ python3 Paddle.py 
square

你可以试试。

补充说明。

参考您的评论,并更好地理解您试图通过一次性初始化属性来处理"Paddle“类,下面是示例代码的一个稍微修改的版本。

代码语言:javascript
复制
import turtle

class Paddle(turtle.Turtle):        # Define as a child of the Turtle object

    def __init__(self,shape,color,stretch_wid,stretch_len):     
        turtle.Turtle.__init__(self)    # Use the Turtle objects initialization
        self.shape(shape)
        self.color(color)
        self.shapesize(stretch_wid = stretch_wid, stretch_len=stretch_len)
        
paddle_x = Paddle("square","blue",5,5)
paddle_x.penup()
paddle_x.goto(0,0)

paddle_a = turtle.Turtle()
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=20,stretch_len=5)
paddle_a.penup()
paddle_a.goto(150,-250)

while True:
    pass

根据你的规格生产正方形。

我把正方形变成蓝色,因为我的背景碰巧是白色的。

试试看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73611450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档