1

I'm able to run the program and the screen pops up with the paddles and the ball, the paddles just wont go up and down, but i was able to get them to move previously before i started simplifying my code and i just cant seem to find where i made a mistake or if im missing something. Any help would be much appreciated. i attached my main.py file and my paddle.py file below. And thank you again for any help

main.py file

from turtle import Screen
from paddle import Paddle
from ball import Ball
import time

screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("It's Pong")
screen.tracer(0)

r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()

screen.listen()
screen.onkey(r_paddle.go_up(), "Up")
screen.onkey(r_paddle.go_down(), "Down")
screen.onkey(l_paddle.go_up(), "w")
screen.onkey(l_paddle.go_down(), "s")


game_is_on = True
while game_is_on:
    time.sleep(0.1)
    screen.update()
    ball.move()

screen.exitonclick()

paddle.py file

from turtle import Turtle


class Paddle(Turtle):

    def __init__(self, position):
        super().__init__()
        self.shape('square')
        self.color('white')
        self.shapesize(stretch_wid=5, stretch_len=1)
        self.penup()
        self.goto(position)

    def go_up(self):
        new_y = self.ycor() + 20
        self.goto(self.xcor(), new_y)

    def go_down(self):
        new_y = self.ycor() - 20
        self.goto(self.xcor(), new_y)
ggorlen
  • 44,755
  • 7
  • 76
  • 106
J Robb
  • 11
  • 1
  • 1
    `screen.onkey(r_paddle.go_up(), "Up")` is calling the method *once*, *right now*, and passing its return value (which is None) as the action to be performed when the key is pressed. You want to pass the method itself, to be called later - in other words, get rid of the `()`. – jasonharper Mar 29 '22 at 17:35
  • @jasonharper that worked! you're a genius, I cant believe I didn't even see that. Thank you so much for your help – J Robb Mar 29 '22 at 21:15
  • Does this answer your question? [onkeypress() function in turtle module problem](https://stackoverflow.com/questions/68983266/onkeypress-function-in-turtle-module-problem) – ggorlen Aug 24 '23 at 01:01

0 Answers0