Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pong paddles wont move
#1
Exclamation 
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()
   
Reply
#2
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()
Reply
#3
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()
Reply
#4
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.
Reply
#5
okidoki, thanks alot, you took the time out of ur day to help me, i appretiate that thx
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] my pygame car wont move Erio85 1 1,095 Apr-24-2023, 04:52 PM
Last Post: Erio85
  Problem with my pong code Than999 3 3,477 Oct-22-2021, 08:50 AM
Last Post: Qanima
  Game “Pong” I have problems with the code BenBach18 2 3,520 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 2,824 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  Trying to make a simple pong game. kevindadmun 1 3,923 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  Smiley Pong Help Jasmineleroy 6 4,744 May-22-2019, 11:36 AM
Last Post: metulburr
  [PyGame] How do I add more balls to basic Pong game? JUtah 2 4,464 Apr-18-2019, 08:40 PM
Last Post: JUtah
  [PyGame] Pong game key.event problem erickDarko 2 4,205 Dec-12-2018, 03:17 PM
Last Post: erickDarko
  Text wont draw fierygaming 1 2,998 Jul-28-2018, 10:39 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020