Question:
Can yu help me let the paddles move?
Current behavior
Only the ball moves when I run it.
Desired behavior
Need both the ball and the paddles to move.
Repl link:
https://replit.com/@creaternet101/pong#main.py
def paddleaup():
y = left_pad.ycor()
y += 20
left_pad.sety(y)
def paddleadown():
y = left_pad.ycor()
y -= 20
left_pad.sety(y)
def paddlebup():
y = right_pad.ycor()
y += 20
right_pad.sety(y)
def paddlebdown():
y = right_pad.ycor()
y -= 20
right_pad.sety(y)
sc.listen()
sc.onkeypress(paddleaup, "e")
sc.onkeypress(paddleadown, "x")
sc.onkeypress(paddlebup, "Up")
sc.onkeypress(paddlebdown, "Down")
1 Like
Welcome to the forums, @creaternet101 !
I’d be happy to help. For one, could you invite me to your repl so I can help as best as I can?
1 Like
Sky
October 21, 2023, 2:13pm
3
I checked his code and noticed there was a syntax error in certain parts. This could easily be triggering his error.
I noticed the same thing, but I want the ability to debug. That’s why I asked him to invite me.
Sky
October 21, 2023, 2:31pm
5
Here, I fixed your syntax error, which allowed the code to run properly and enabled you to move the paddles, etc. I also improved the collision detection just a bit. You can make further improvements as you see fit. I tested it a bit, and it should work as you intended:
import turtle
sc = turtle.Screen()
sc.title("Pong game")
sc.bgcolor("white")
sc.setup(width=1000, height=600)
left_pad = turtle.Turtle()
left_pad.speed(0)
left_pad.shape("square")
left_pad.color("black")
left_pad.shapesize(stretch_wid=6, stretch_len=2)
left_pad.penup()
left_pad.goto(-400, 0)
right_pad = turtle.Turtle()
right_pad.speed(0)
right_pad.shape("square")
right_pad.color("black")
right_pad.shapesize(stretch_wid=6, stretch_len=2)
right_pad.penup()
right_pad.goto(400, 0)
hit_ball = turtle.Turtle()
hit_ball.speed(40)
hit_ball.shape("circle")
hit_ball.color("blue")
hit_ball.penup()
hit_ball.goto(0, 0)
hit_ball.dx = 5
hit_ball.dy = -5
left_player = 0
right_player = 0
sketch = turtle.Turtle()
sketch.speed(0)
sketch.color("blue")
sketch.penup()
sketch.hideturtle()
sketch.goto(0, 260)
sketch.write("Left player: 0 Right player: 0",
align="center", font=("Courier", 24, "normal"))
def paddleaup():
y = left_pad.ycor()
y += 20
left_pad.sety(y)
def paddleadown():
y = left_pad.ycor()
y -= 20
left_pad.sety(y)
def paddlebup():
y = right_pad.ycor()
y += 20
right_pad.sety(y)
def paddlebdown():
y = right_pad.ycor()
y -= 20
right_pad.sety(y)
sc.listen()
sc.onkeypress(paddleaup, "e")
sc.onkeypress(paddleadown, "x")
sc.onkeypress(paddlebup, "Up")
sc.onkeypress(paddlebdown, "Down")
while True:
sc.update()
hit_ball.setx(hit_ball.xcor() + hit_ball.dx)
hit_ball.sety(hit_ball.ycor() + hit_ball.dy)
if hit_ball.ycor() > 280:
hit_ball.sety(280)
hit_ball.dy *= -1
if hit_ball.ycor() < -280:
hit_ball.sety(-280)
hit_ball.dy *= -1
if hit_ball.xcor() > 500:
hit_ball.goto(0, 0)
hit_ball.dx *= -1
left_player += 1
sketch.clear()
sketch.write("Left player: {} Right player: {}".format(
left_player, right_player), align="center",
font=("Courier", 24, "normal"))
if hit_ball.xcor() < -500:
hit_ball.goto(0, 0)
hit_ball.dx *= -1
right_player += 1
sketch.clear()
sketch.write("Left player: {} Right player: {}".format(
left_player, right_player), align="center",
font=("Courier", 24, "normal"))
if (hit_ball.xcor() > 360 and hit_ball.xcor() < 370) and (right_pad.ycor() + 50 > hit_ball.ycor() > right_pad.ycor() - 50):
hit_ball.setx(360)
hit_ball.dx *= -1
if (hit_ball.xcor() < -360 and hit_ball.xcor() > -370) and (left_pad.ycor() + 50 > hit_ball.ycor() > left_pad.ycor() - 50):
hit_ball.setx(-360)
hit_ball.dx *= -1
if hit_ball.ycor() > 290 or hit_ball.ycor() < -290:
hit_ball.dy *= -1
1 Like
@RedCoder I’ve invited you.
Please mark the changes you make so i can learn from this.
Thank You
@Sky
The code seems to work the same exept the ball goes to the opposite direction when scoring.
The paddles still won’t move. What did you try this on? I currently have nothing installed.
1 Like
In case that you need to edit i have invited you @Sky .
Sky
October 22, 2023, 3:21am
9
I tried it on Replit, and it worked fine.
I’ve been out and about, sorry. I’ll look into it tomorrow.
Sky
October 22, 2023, 1:32pm
11
Are you sure you’re using the correct keybinds, you might be using the wrong keybinds for moving the paddles.
The right and left paddle seems to move, it’s just that the screen width and height are too large to see without a bigger screen.
Yes this has worked, my apoligies. it seems that since last time my computer went though an update and thats why it didnt work.
Thank you for the help.
1 Like
system
Closed
October 30, 2023, 5:47am
15
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.