我一直在尝试使用乌龟制作python乒乓球游戏,但它不起作用。这是代码,请帮我解决。每当我运行它时,球都不会在0和0之外。 我为y和x尝试了很多坐标,但是似乎不起作用。因此,如果您发现了错误所在,请回复。
import turtle
canavs = turtle.Screen()
canavs.title('Aryesh\'s Ping Pong Game')
canavs.bgcolor('black')
canavs.setup(width=800,height=600)
pa=turtle.Turtle()
pa.penup()
pa.goto(-350,0)
pa.pendown
pa.color('white')
pb=turtle.Turtle()
pb.penup()
pb.goto(350,0)
pb.pendown
pb.color('white')
pb.shape('square')
pb.shapesize(stretch_wid=5,stretch_len=1)
ball=turtle.Turtle()
ball.penup()
ball.goto(0,0)
ball.pendown
ball.color('white')
ball.shape('circle')
speedx=2
speedy=2
def pa_mov_up():
y=pa.ycor()
y=y+20
pa.sety(y)
def pb_mov_up():
y=pb.ycor()
y=y+20
pb.sety(y)
def pa_mov_down():
y=pa.ycor()
y=y-20
pa.sety(y)
def pb_mov_down():
y=pb.ycor() #get the currenty cor
y=y-20 #sub -20
pb.sety(y) #set that ycor
canavs.listen()
canavs.onkeypress(pa_mov_up,('w'))
canavs.onkeypress(pa_mov_down,('s'))
canavs.onkeypress(pb_mov_up,('Up'))
canavs.onkeypress(pb_mov_down,('Down'))
while True:
canavs.update()
ball.setx(ball.xcor()+speedx)
ball.sety(ball.xcor()+speedy)
if ball.ycor()>290:
ball.sety(290)
speedy*=-1
elif ball.ycor()>-290:
ball.sety(-290)
speedy*=-1
if ball.xcor()>390:
ball.goto(0,0)
speedx=speedx*-1
elif ball.xcor()<-390:
ball.goto(0,0)
speedx=speedx*-1
if ball.xcor()>340 and ball.ycor()<pb.ycor()+40 and ball.ycor()>pb.ycor()-40:
ball.setx(340)
speedx=speedx*-1
if ball.xcor()<-340 and ball.ycor()<pa.ycor()+40 and ball.ycor()>pb.ycor()-40:
ball.setx(-340)
speedx=speedx*-1
turtle.done()
答案 0 :(得分:1)
根据乌龟的文档。我没有找到名为pendown
的属性。也许您是说pendown()函数。 penup()
和pendown()
的作用是:
您的代码中存在一些逻辑错误:
2.1 ball.sety(ball.xcor()+speedy)
-> ball.sety(ball.ycor()+speedy)
原始
ball.setx(ball.xcor()+speedx)
ball.sety(ball.xcor()+speedy)
2.2 elif ball.ycor()>-290:
-> elif ball.ycor()<-290:
原始
if ball.ycor()>290:
ball.sety(290)
speedy*=-1
elif ball.ycor()>-290:
ball.sety(-290)
speedy*=-1
2.3 ball.goto(0,0)
-> ball.setx(390)
或ball.setx(-390)
原始
if ball.xcor()>390:
ball.goto(0,0)
speedx=speedx*-1
elif ball.xcor()<-390:
ball.goto(0,0)
speedx=speedx*-1
2.4 ball.ycor()<pa.ycor()+40 and ball.ycor()>pb.ycor()-40:
至ball.ycor()<pa.ycor()+40 and ball.ycor()>pa.ycor()-40:
原始
if ball.xcor()<-340 and ball.ycor()<pa.ycor()+40 and ball.ycor()>pb.ycor()-40:
ball.setx(-340)
speedx=speedx*-1
修改后的代码是
import turtle
canavs = turtle.Screen()
canavs.title('Aryesh\'s Ping Pong Game')
canavs.bgcolor('black')
canavs.setup(width=800,height=600)
pa=turtle.Turtle()
pa.penup()
pa.goto(-350,0)
pa.color('white')
pa.shape('square')
pa.shapesize(stretch_wid=5,stretch_len=1)
pb=turtle.Turtle()
pb.penup()
pb.goto(350,0)
pb.color('white')
pb.shape('square')
pb.shapesize(stretch_wid=5,stretch_len=1)
ball=turtle.Turtle()
ball.penup()
ball.goto(0,0)
ball.pendown()
ball.color('white')
ball.shape('circle')
speedx=2
speedy=2
def pa_mov_up():
y=pa.ycor()
y=y+20
pa.sety(y)
def pb_mov_up():
y=pb.ycor()
y=y+20
pb.sety(y)
def pa_mov_down():
y=pa.ycor()
y=y-20
pa.sety(y)
def pb_mov_down():
y=pb.ycor() # get the currenty cor
y=y-20 # sub -20
pb.sety(y) # set that ycor
canavs.listen()
canavs.onkeypress(pa_mov_up,('w'))
canavs.onkeypress(pa_mov_down,('s'))
canavs.onkeypress(pb_mov_up,('Up'))
canavs.onkeypress(pb_mov_down,('Down'))
while True:
canavs.update()
ball.setx(ball.xcor()+speedx)
ball.sety(ball.ycor()+speedy)
if ball.ycor()>290:
ball.sety(290)
speedy*=-1
elif ball.ycor()<-290:
ball.sety(-290)
speedy*=-1
if ball.xcor()>390:
ball.setx(390)
speedx=speedx*-1
elif ball.xcor()<-390:
ball.setx(-390)
speedx=speedx*-1
if ball.xcor()>340 and ball.ycor()<pb.ycor()+40 and ball.ycor()>pb.ycor()-40:
ball.setx(340)
speedx=speedx*-1
if ball.xcor()<-340 and ball.ycor()<pa.ycor()+40 and ball.ycor()>pa.ycor()-40:
ball.setx(-340)
speedx=speedx*-1
turtle.done()