Python Forum
Problem with my pong game code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with my pong game code (/thread-34753.html)



Problem with my pong game code - Than999 - Aug-28-2021

Hi, I am following a tutorial online: freecodecamp.org on Youtube. I am learning how to program a simple pong game in Python. However, when I go and execute my code the "left paddle" associated with the "w" key on my keyboard doesn't respond. My code looks the same as the guy in the tutorial video, and I don't know why my code doesn't work. I believe it's my wn.listen() method, but I'm not sure. Any help or suggestions would be greatly appreciated.

import turtle

wn = turtle.Screen()
wn.title("Pong by @TokyoEdTech")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

# Paddle A

paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)

# Paddle B
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(350, 0)

# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)


# Function
def paddle_a_up():
    y = paddle_a.ycor()
    y += 20
    paddle_a.sety(y)


# Keyboard binding
wn.listen()
wn.onkeypress(paddle_a_up(), "w")  # this calls the function paddle_a_up()

# Main game loop
while True:
    wn.update()



RE: Problem with my pong game code - ndc85430 - Aug-28-2021

Line 46: drop the parens on paddle_a_up. Why? Because having the parens means calling the function and then the return value is passed to onkeypress, which isn't what you want to do. Instead, you want to pass the function itself to onkeypress, so that it can be called when the relevant key is pressed.


RE: Problem with my pong game code - Than999 - Sep-09-2021

(Aug-28-2021, 04:41 PM)ndc85430 Wrote: Line 46: drop the parens on paddle_a_up. Why? Because having the parens means calling the function and then the return value is passed to onkeypress, which isn't what you want to do. Instead, you want to pass the function itself to onkeypress, so that it can be called when the relevant key is pressed.

Okay, thanks for your suggestion!


RE: Problem with my pong game code - kinggemini696 - May-14-2022

(Sep-09-2021, 11:34 AM)Than999 Wrote:
(Aug-28-2021, 04:41 PM)ndc85430 Wrote: Line 46: drop the parens on paddle_a_up. Why? Because having the parens means calling the function and then the return value is passed to onkeypress, which isn't what you want to do. Instead, you want to pass the function itself to onkeypress, so that it can be called when the relevant key is pressed.

Okay, thanks for your suggestion!

I am having a similar issue and since i am a bit new to coding i dont properly understand what you are saying


RE: Problem with my pong game code - ndc85430 - May-14-2022

It might help if you posted the code and the errors, probably in a separate thread since it's a separate piece of code.


RE: Problem with my pong game code - kinggemini696 - May-14-2022

(May-14-2022, 03:08 PM)ndc85430 Wrote: It might help if you posted the code and the errors, probably in a separate thread since it's a separate piece of code.


the problem is I am not getting any errors just like this guy here, I code it exactly i even removed (win. tracer)
I entered (turtle. done)


it just becomes unresponsive after a certain point in time.


RE: Problem with my pong game code - ndc85430 - May-15-2022

You need to show the code. We can't see your screen.


RE: Problem with my pong game code - kinggemini696 - May-15-2022

(May-15-2022, 04:43 AM)ndc85430 Wrote: You need to show the code. We can't see your screen.

import turtle

wn = turtle.Screen()
wn.title ("pong by king gemini")
wn.bgcolor("black")
wn.setup(width=800, height=600)



#Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(-350, 0)

#Paddle B
paddle_a = turtle.Turtle()
paddle_a.speed(0)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.goto(+350, 0)

#Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0,0)


#function
def paddle_a_up():
y = paddle_a.ycor()
y += 20
paddle_a.sety(y)

#keyboard binding

wn.listen()
wn.onkeypress(paddle_a_up,"q")




#main game loop
while True:
wn.update()
turtle.done


RE: Problem with my pong game code - deanhystad - May-15-2022

Works fine for me. You will want to write a paddle_a_down() function and bind it to a different key. You might also want to modify the up and down functions to prevent the paddle from moving outside the window.

What do you think is wrong?