I am trying to create a sort of pingpong game in python using turtle.The player moves his paddle by dragging his finger on the screen but now I need a function that will be giving me the current position of the paddle in terms of x and y so that I can use it to detect collision of paddle with ball
I tried to use the tur.pos() function but it had no effect here's a part of the code
import turtle
from random import choice
from random import seed
import threading
scn = turtle.Screen()
scn.setup(width = 500, height =1000)
scn.bgcolor("black")
#bar_a
bar_a = turtle.Turtle()
bar_a.color("blue")
bar_a.speed('fastest')
bar_a.penup()
bar_a.goto(-320, 600)
bar_a.right(90)
bar_a.width(20)
bar_a.pendown()
bar_a.forward(1200)
#bar_b
bar_b = turtle.Turtle()
bar_b.color("blue")
bar_b.speed('fastest')
bar_b.penup()
bar_b.goto(320, 600)
bar_b.right(90)
bar_b.width(20)
bar_b.pendown()
bar_b.forward(1200)
#middle
mid =turtle.Turtle()
mid.color('blue')
mid.speed('fastest')
mid.penup()
mid.goto(-320, 0)
mid.pendown()
mid.width(5)
mid.forward(640)
#paddle A
pad_A= turtle.Turtle()
pad_A.shape("square")
pad_A.shapesize(stretch_len =7, stretch_wid =2.5)
pad_A.penup()
pad_A.goto(0, -700)
pad_A.color('grey')
#mouse drag fxn
def fxn(x, y):
pad_A.ondrag(None)
pad_A.goto(x, y)
pad_A.ondrag(fxn, 5, True)
return x
return y
def bxn(x, y):
pad_B.ondrag(None)
pad_B.goto(x, y)
pad_B.ondrag(bxn, 5, True)
return x
return y
#paddle B
pad_B= turtle.Turtle()
pad_B.shape("square")
pad_B.shapesize(stretch_len =7, stretch_wid =2.5)
pad_B.penup()
pad_B.goto(0, 700)
pad_B.color('grey')
#ball
ball = turtle.Turtle()
ball.shape("circle")
ball.shapesize(stretch_len =3, stretch_wid = 3)
ball.color("green")
ball.penup()
ball.goto(0, 0)
#Variables
num =[20, -20]
y = 0
x =0
#generating random numbers to serve as dx and dy for ball
set_x = int(choice(num))
set_y = int(choice(num))
while True:
scn.update()
#calling drag fxns
pad_A.ondrag(fxn)
pad_B.ondrag(bxn)
#moving ball
ball.goto(x+set_x, y+set_y)
y =y+set_y
x =x+set_x
#ball collision with sides
if (x >=320 and y <=600 and y >=-600) or (x <=-320 and y <=600 and y >=-600):
set_x=set_x*-1
i, j = pad_A.pos()
if x<=i+70 and x>=i-70 and y==j+50:
set_y = set_y*-1