Reputation: 63
I'm following a tutorial for making pong in the turtle API & for some reason I'm getting an error which seems to stop "paddle_a" from colliding. It errors out every time I close the window. The paddle a collision detection seems identical to the paddle b collision detection other than numbers being tweaked.
I'm not that great at debugging but I know it's at line 69 and it seems to affect just the collision detection with the ball of paddle_a but not the nearly identical code of paddle_b.
Traceback (most recent call last): File "c:\Users\harba\OneDrive\Desktop\New folder\Personal Projects\Games\Pong.py", line 69, in ball.setx(ball.xcor() + ball_dx) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1809, in setx self._goto(Vec2D(x, self._position[1])) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 3160, in _goto screen._pointlist(self.currentLineItem), File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 754, in _pointlist File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 1809, in setx self._goto(Vec2D(x, self._position[1])) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 3160, in _goto screen._pointlist(self.currentLineItem), File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\turtle.py", line 754, in pointlist cl = self.cv.coords(item) File "", line 1, in coords File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\tkinter_init.py", line 2795, in coords_tkinter.TclError: invalid command name ".!canvas"
import turtle
window = turtle.Screen()
window.title("Pong but cool")
window.bgcolor("black")
window.setup(width=800,height=600)
window.tracer(0)
#Padles
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 = turtle.Turtle()
paddle_b.speed(0)
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len=1)
paddle_b.penup()
paddle_b.goto(350, 0)
#Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.05
ball.dy = 0.05
#kooky Functions
def paddle_a_up():
y = paddle_a.ycor()
y += 20
paddle_a.sety(y)
def paddle_a_down():
y = paddle_a.ycor()
y -= 20
paddle_a.sety(y)
def paddle_b_up():
y = paddle_b.ycor()
y += 20
paddle_b.sety(y)
def paddle_b_down():
y = paddle_b.ycor()
y -= 20
paddle_b.sety(y)
#keyboard binding
window.listen()
window.onkeypress(paddle_a_up, "w")
window.onkeypress(paddle_a_down, "s")
window.onkeypress(paddle_b_up, "Up")
window.onkeypress(paddle_b_down, "Down")
while True:
window.update()
#balls
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
if ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
#collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 50 and ball.ycor() > paddle_b.ycor() - 50):
ball.setx(340)
ball.dx *= -1
if (ball.xcor() > -340 and ball.xcor() < -350) and (ball.ycor() < paddle_a.ycor() + 50 and ball.ycor() > paddle_a.ycor() - 50):
ball.setx(-340)
ball.dx *= -1
The final if statement inside the game loop doesn't seem to work & in the balls section is where the error seems to originate.
Upvotes: 1
Views: 118
Reputation: 41872
This can never be true:
ball.xcor() > -340 and ball.xcor() < -350
You have to reverse your geometric thinking when working with negative coordinates:
-340 > ball.xcor() > -350
My rework of your code:
from turtle import Screen, Turtle
def paddle_a_up():
paddle_a.sety(paddle_a.ycor() + 20)
def paddle_a_down():
paddle_a.sety(paddle_a.ycor() - 20)
def paddle_b_up():
paddle_b.sety(paddle_b.ycor() + 20)
def paddle_b_down():
paddle_b.sety(paddle_b.ycor() - 20)
def play():
x, y = ball.position()
ball.setposition(x + ball.dx, y + ball.dy)
# border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
elif ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
elif ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
# collisions
if 340 < ball.xcor() < 350 and paddle_b.ycor() - 50 < ball.ycor() < paddle_b.ycor() + 50:
ball.setx(340)
ball.dx *= -1
elif -340 > ball.xcor() > -350 and paddle_a.ycor() - 50 < ball.ycor() < paddle_a.ycor() + 50:
ball.setx(-340)
ball.dx *= -1
screen.update()
screen.ontimer(play, 10)
screen = Screen()
screen.title("Pong but cool")
screen.bgcolor('black')
screen.setup(width=800, height=600)
screen.tracer(0)
# Padles
paddle_a = Turtle()
paddle_a.shape('square')
paddle_a.color('white')
paddle_a.shapesize(stretch_wid=5, stretch_len=1)
paddle_a.penup()
paddle_a.setx(-350)
paddle_b = paddle_a.clone()
paddle_b.setx(350)
# Ball
ball = Turtle()
ball.shape('square')
ball.color('white')
ball.penup()
ball.dx = 2.0 # user-defined properties
ball.dy = 2.0
# keyboard binding
screen.onkeypress(paddle_a_up, 'w')
screen.onkeypress(paddle_a_down, 's')
screen.onkeypress(paddle_b_up, 'Up')
screen.onkeypress(paddle_b_down, 'Down')
screen.listen()
play()
screen.mainloop()
Upvotes: 1