I've been trying to make a python ping pong game using turtle but it does not work. Here is the code, pls help me with it.Whenever I run it, the ball does not go outside 0 and 0. I tried a lot of coordinates for y and x but it does not seem to work. So, if you figured out what the mistake is, please reply back.
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()
According to turtle's document. I didn't find an attribute named pendown
. Maybe you mean the pendown() function. The effect of penup()
and pendown()
is:
There are some logical mistakes in your code:
2.1 ball.sety(ball.xcor()+speedy)
-> ball.sety(ball.ycor()+speedy)
Original
ball.setx(ball.xcor()+speedx)
ball.sety(ball.xcor()+speedy)
2.2 elif ball.ycor()>-290:
-> elif ball.ycor()<-290:
Original
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)
or ball.setx(-390)
Original
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:
Original
if ball.xcor()<-340 and ball.ycor()<pa.ycor()+40 and ball.ycor()>pb.ycor()-40:
ball.setx(-340)
speedx=speedx*-1
The code after modification is
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.