Posts: 3
Threads: 1
Joined: Dec 2022
Dec-29-2022, 07:42 PM
help pls! i am having trouble with the pong paddles and there are no visible errors in my code, pLeASe hELPA mE :((((
Here's my code:
import turtle
wn = turtle.Screen()
wn.title("pong py project")
wn.bgcolor("yellow")
wn.setup(width=800, height=600)
#wn.tracer(0)
# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(3)
paddle_a.shape("square")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.color('white')
paddle_a.penup()
paddle_a.goto(-350, 0)
# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(3)
paddle_b.shape("square")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.color('white')
paddle_b.penup()
paddle_b.goto(+350, 0)
# Ball
ball = turtle.Turtle()
ball.speed(3)
ball.shape("square")
ball.color('white')
ball.penup()
ball.goto(0, 0)
# Function
def paddle_a_up():
y = paddle_a.ycor(y)
y += 20
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor(y)
y -= 20
paddle_a.sety(y)
# Keyboard binding
wn.onkeypress(paddle_a_up, 'w')
wn.onkeypress(paddle_a_down, 's')
#main game loop
while True:
wn.update()
Posts: 6,798
Threads: 20
Joined: Feb 2020
Dec-29-2022, 11:20 PM
(This post was last modified: Dec-29-2022, 11:20 PM by deanhystad.)
You need to listen() for key press events. You also had an indenting problem on wn.onkeypress()
You should consider making some classes to organize your code. This is your program using classes to make two movable paddles and a ball with 14 fewer lines of code.
import turtle
class Paddle(turtle.Turtle):
def __init__(self, x, keys=None, color='black'):
super().__init__()
self.speed(3)
self.shape('square')
self.shapesize(stretch_wid=5, stretch_len=1)
self.color(color)
self.penup()
self.goto(x, 0)
if keys:
# Have key press events move the paddles
wn.onkeypress(lambda: self.move_y(20), keys[0])
wn.onkeypress(lambda: self.move_y(-20), keys[1])
def move_y(self, step):
self.sety(self.ycor()+step)
class Ball(turtle.Turtle):
def __init__(self, color='black', keys=None):
super().__init__()
self.speed(3)
self.shape('circle')
self.color(color)
self.penup()
self.goto(0, 0)
wn = turtle.Screen()
wn.setup(width=800, height=600)
paddles = (Paddle(-350, "ad"), Paddle(350, "jl"))
ball = Ball()
wn.listen() # Need to call this to have turtle listen for key press events.
while True:
wn.update()
Posts: 3
Threads: 1
Joined: Dec 2022
omgomgomg thank you so much! the paddles are moving now, but now idk how to get the ball moving... ik this code looks alot like the code you showed me, sry for that, i'm still learning the basics of python/turtle
import turtle
class Paddle (turtle.Turtle):
def __init__(self, x, keys=None, color='white'):
super().__init__()
self.speed(3)
self.shape('square')
self.shapesize(stretch_wid=5, stretch_len=1)
self.color(color)
self.penup()
self.goto(x, 0)
if keys:
wn.onkeypress(lambda: self.move_y(20), keys[0])
wn.onkeypress(lambda: self.move_y(-20), keys[1])
def move_y(self, step):
self.sety(self.ycor()+step)
class Ball (turtle.Turtle):
def __init__(self, color='white', keys=None):
super().__init__()
self.speed(3)
self.shape('circle')
self.color(color)
self.penup()
self.goto(0, 0)
wn = turtle.Screen()
wn.setup(width=800, height=600)
paddles = (Paddle(-350, "ad"), Paddle(350, "jl"))
ball = Ball()
wn.bgcolor('yellow')
wn.title('pong')
wn.listen()
while True:
wn.update()
Posts: 6,798
Threads: 20
Joined: Feb 2020
Code to move the ball will go in the while loop. Move ball, check for collision with walls or paddle, add to score if ball gets past the paddle, etc... You have a lot of coding ahead.
Posts: 3
Threads: 1
Joined: Dec 2022
okidoki, thanks alot, you took the time out of ur day to help me, i appretiate that thx
Posts: 16
Threads: 3
Joined: Jan 2023
It depends on what you are trying to do. If you are trying to create a web application, then you will need to use a web framework such as Django or Flask. If you are trying to create a desktop application, then you will need to use a GUI library such as Tkinter or PyQt. If you are trying to create a command line application, then you will need to use a library such as argparse or click.
|