I'm trying to do a Pong game using turtle to practice a little OOP. My objective here is to make the paddles and set their places.
There's two different files:
the main>
from turtle import Screen
from paddle import Paddle
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("blue")
screen.title("Pong Game")
screen.tracer(0)
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
screen.update()
screen.listen()
screen.onkeypress(r_paddle.go_up, "Up")
screen.onkeypress(r_paddle.go_down, "Down")
screen.exitonclick()
and the Paddle file>
from turtle import Turtle
class Paddle(Turtle):
def __int__(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)
When I run it, an error appears: Key error.
I've tried to modify the code, put all in the same file, google somethings... Till now I really don't understand why it doesn't work.
Errors are good! More than just a
KeyError, this error gives the exact line the exception occurred on and a stack trace of all function calls leading to the error:Don't ignore this error. Always share it when you have a question about your code. What can be learned from this error?
(350, 0)is being passed into theRawTurtleinternal initializer and some code that has to do with shapes in the turtle library is raising. A possible next step is to minimize the problem. It can be reproduced with two lines of code that doesn't involve thePaddleclass at all:The library is not liking this tuple, but it does seem to accept some optional parameter. What parameter does it actually expect? The docs reveal:
We're not passing in a canvas. The intent is to pass that tuple to our
Paddleinitializer, which is not passing it along tosuper().__init__(). Weirdly, in the stack trace, we don't even see ourPaddleconstructor being invoked. Adding a print statement where control should flow to confirms this:Does this print? No. Examine the initializer again:
Looks like a typo:
__init__was intended rather than__int__. That's the fix!Paddle()was invoking the initializer from the superclass rather than our initializer, which didn't exist as far as Turtle was concerned due to the typo.However, since an ounce of prevention is worth a pound of cure, you'll save yourself a good deal of future stress if you avoid subclassing Turtle entirely. Always use composition, not inheritance with turtle. See this post for a detailed explanation. The error is much easier to debug with composition ("Paddle has a Turtle" rather than "Paddle is a Turtle"):
Here's the code that produces the above, far clearer error: