Joyk
@Joyk
Программист среднего уровня

Как реализовать нахождение положения в Python Turtle?

Не знаю как реализовать нахождение местонахождения объекта в Python Turtle
(Хочу сделать ping-pong)
Понимаю, что код плоховат)
from tkinter import *
from turtle import *
import random as ran

window = Tk()
window.title("Ping-pong")
window.geometry("2048x1536")

screen = Canvas(window, width = 2048, height = 1536)
screen.pack()
turtle = TurtleScreen(screen)

point1 = 0
point2 = 0

speed = 10
speedx = 15
speedy = 15

x = 0
y = 0

i = 0

x1 = -973
y1 = 300
y2 = -300

x11 = 973
y11 = 300
y12 = -300

l = Label(screen, text = str(point1), font = "Bold 31", background = "white")
l.place(x = 730, y = 50)

l1 = Label(screen, text = str(point2), font = "Bold 31", background = "white")
l1.place(x = 1230, y = 50)

tur = RawTurtle(turtle)
tur1 = RawTurtle(turtle)
tur2 = RawTurtle(turtle)
tur3 = RawTurtle(turtle)

tur.speed(0)
tur1.speed(0)
tur2.speed(0)
tur3.speed(0)

tur.up()
tur.goto(0, 768)
tur.width(7)
tur.down()
tur.goto(0,-768)

tur1.shape("circle")
tur1.shapesize(3)
tur1.penup()

tur2.up()
tur2.goto(x1, y1)
tur2.down()
tur2.width(27)
tur2.goto(x1, y2)

tur3.up()
tur3.goto(x11, y1)
tur3.down()
tur3.width(27)
tur3.goto(x11, y12)

def up():
    global x1, y1, y2, tur2, speed
    tur2.reset()
    tur2.speed(7)
    y1 = y1 + int(speed)
    y2 = y2 + int(speed)
    tur2.up()
    tur2.goto(x1, y1 + int(speed))
    tur2.down()
    tur2.width(27)
    tur2.goto(x1, y2)
    return y1, y2
    
def down():
    global x1, y1, y2, tur2, speed
    tur2.reset()
    tur2.speed(7)
    y1 = y1 - int(speed)
    y2 = y2 - int(speed)
    tur2.up()
    tur2.goto(x1, y1 - int(speed))
    tur2.down()
    tur2.width(27)
    tur2.goto(x1, y2 - int(speed))
    return y1, y2
    
def up1():
    global x11, y11, y12, tur3, speed
    tur3.reset()
    tur3.speed(7)
    y11 = y1 + int(speed)
    y12 = y2 + int(speed)
    tur3.up()
    tur3.goto(x11, y11 + int(speed))
    tur3.down()
    tur3.width(27)
    tur3.goto(x11, y12 + int(speed))
    return y11, y12
   
def down1():
    global x11, y11, y12, tur3, speed
    tur3.reset()
    tur3.speed(7)
    y11= y1 - int(speed)
    y12 = y2 - int(speed)
    tur3.up()
    tur3.goto(x11, y1 - int(speed))
    tur3.down()
    tur3.width(27)
    tur3.goto(x11, y12 - int(speed))
    return y11, y12

while True:
    if i == 0:
        tur1.hideturtle()
        tur1.speed(0)
        tur1.goto(0, ran.randint(-730, 730))
        i = i + 1
    else:
        tur1.showturtle()
        tur1.speed(10)
        x, y = tur1.position()
        if x == x1 and y == range(int(up()), int(up())):
            speedx = -speedx
        elif y == range(int(up1()), int(up1()))  and x == x11:
            speedy = -speedy   
        tur1.goto(x + speedx, y + speedy)

screen.bind("<w>", up())
screen.bind("<s>", down())
screen.bind("<o>", up1())
screen.bind("<l>", down1())

window.mainloop()
  • Вопрос задан
  • 121 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы