我想做一个扑鸟游戏,目前正在尝试把桨a变成一个类,但是它不会出现在它应该出现的时候。Im也没有收到任何错误消息,代码也可以运行,但是没有任何paddle (类中的对象)的迹象。这是代码:
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'“。第一次问问题。提前谢谢。
发布于 2022-09-05 22:06:53
在处理了一些多边形类的对象继承之后,我相信您需要在"Paddle“对象的类定义中引用"Turtle”对象,如下代码片段所示。
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值。
@Una:~/Python_Programs/Paddle$ python3 Paddle.py
square你可以试试。
补充说明。
参考您的评论,并更好地理解您试图通过一次性初始化属性来处理"Paddle“类,下面是示例代码的一个稍微修改的版本。
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根据你的规格生产正方形。

我把正方形变成蓝色,因为我的背景碰巧是白色的。
试试看。
https://stackoverflow.com/questions/73611450
复制相似问题