[英]Ping Pong game with python turtle not working
我一直在嘗試使用海龜制作 python 乒乓球游戲,但它不起作用。 這是代碼,請幫助我。每當我運行它時,球在 0 和 0 之外都不會 go。我嘗試了很多 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()
根據海龜的文件。 我沒有找到名為pendown
的屬性。 也許你的意思是 pendown () function。 penup()
和 pendown( 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:
to 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.