使用 .goto() 覆盖 python 海龟中先前对象的位置
Using .goto() overwrites the location of previous objects in python turtle
import turtle
wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor("black")
wn.setup(height= 600, width= 800)
wn.tracer(0)
class Paddle:
paddle = turtle.Turtle()
def __init__(self):
self.paddle.speed(0)
self.paddle.color("white")
self.paddle.shape("square")
self.paddle.shapesize(stretch_wid = 5, stretch_len= 1)
self.paddle.penup()
def goto(self,location):
self.paddle.goto(location,0)
paddleA = Paddle()
paddleB = Paddle()
paddleA.goto(350)
paddleB.goto(-350)
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.color("white")
ball.shape("square")
ball.penup()
ball.goto(0,0)
# Movement Functions
# Game loop
while True:
wn.update()
我想让这个显示的是屏幕左右两侧的两个拨片。但是,创建的屏幕中只显示一个桨。这个球拍将是最后一个调用函数 .goto()
的球拍。为什么会这样?对此行为的纠正是什么?
您没有在 class 中创建 唯一可识别的 实例,它只是重复使用已经构建的同一个实例。
class Paddle:
paddle = turtle.Turtle()
def __init__(self):
print( id(self.paddle) )
self.paddle.speed(0)
self.paddle.color("white")
self.paddle.shape("square")
self.paddle.shapesize(stretch_wid = 5, stretch_len= 1)
self.paddle.penup()
def goto(self,location):
self.paddle.goto(location,0)
def color(self,color):
self.paddle.color(color)
paddleA = Paddle()
paddleB = Paddle()
paddleA.color('red')
paddleB.color('blue')
paddleA.goto(350)
paddleB.goto(-350)
# Ball
ball = turtle.Turtle()
print( id(ball) )
ball.speed(0)
ball.color("white")
ball.shape("square")
ball.penup()
ball.goto(0,0)
3066489872
3066489872
3066490064
将 paddle 构造放入 init 块中:
class Paddle:
def __init__(self):
self.paddle = turtle.Turtle()
print( id(self.paddle) )
self.paddle.speed(0)
self.paddle.color("white")
self.paddle.shape("square")
self.paddle.shapesize(stretch_wid = 5, stretch_len= 1)
self.paddle.penup()
def goto(self,location):
self.paddle.goto(location,0)
def color(self,color):
self.paddle.color(color)
3065846920
3065847064
3065847184
import turtle
wn = turtle.Screen()
wn.title("Pong")
wn.bgcolor("black")
wn.setup(height= 600, width= 800)
wn.tracer(0)
class Paddle:
paddle = turtle.Turtle()
def __init__(self):
self.paddle.speed(0)
self.paddle.color("white")
self.paddle.shape("square")
self.paddle.shapesize(stretch_wid = 5, stretch_len= 1)
self.paddle.penup()
def goto(self,location):
self.paddle.goto(location,0)
paddleA = Paddle()
paddleB = Paddle()
paddleA.goto(350)
paddleB.goto(-350)
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.color("white")
ball.shape("square")
ball.penup()
ball.goto(0,0)
# Movement Functions
# Game loop
while True:
wn.update()
我想让这个显示的是屏幕左右两侧的两个拨片。但是,创建的屏幕中只显示一个桨。这个球拍将是最后一个调用函数 .goto()
的球拍。为什么会这样?对此行为的纠正是什么?
您没有在 class 中创建 唯一可识别的 实例,它只是重复使用已经构建的同一个实例。
class Paddle:
paddle = turtle.Turtle()
def __init__(self):
print( id(self.paddle) )
self.paddle.speed(0)
self.paddle.color("white")
self.paddle.shape("square")
self.paddle.shapesize(stretch_wid = 5, stretch_len= 1)
self.paddle.penup()
def goto(self,location):
self.paddle.goto(location,0)
def color(self,color):
self.paddle.color(color)
paddleA = Paddle()
paddleB = Paddle()
paddleA.color('red')
paddleB.color('blue')
paddleA.goto(350)
paddleB.goto(-350)
# Ball
ball = turtle.Turtle()
print( id(ball) )
ball.speed(0)
ball.color("white")
ball.shape("square")
ball.penup()
ball.goto(0,0)
3066489872
3066489872
3066490064
将 paddle 构造放入 init 块中:
class Paddle:
def __init__(self):
self.paddle = turtle.Turtle()
print( id(self.paddle) )
self.paddle.speed(0)
self.paddle.color("white")
self.paddle.shape("square")
self.paddle.shapesize(stretch_wid = 5, stretch_len= 1)
self.paddle.penup()
def goto(self,location):
self.paddle.goto(location,0)
def color(self,color):
self.paddle.color(color)
3065846920
3065847064
3065847184