Top.Mail.Ru
Ответы

Помогите сделай коллизию в python для другой платформы в пинг понг

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
from tkinter import *
import time
root = Tk()
root.geometry("1280x1024")
root["bg"] = 'black'
root.title('pongli pangli')
canvas = Canvas(root, width=1280, height=1024, bg="black")
canvas.pack()

class Player:
    def __init__(self):
        self.id = None
        self.y =  None
    def draw(self):
        canvas.move(self.id, 0, self.y)
        _,y, _,y1 = canvas.coords(self.id)
        if y <= 0:
            self.y = 0

        if y1 >= 1024:
            self.y = 0
class Plaer1(Player):
    def __init__(self):
        super().__init__()
        self.id = canvas.create_rectangle(10, 512, 25, 570, fill = "white")
        self.y = 0
        self.speed = 5
    def move(self, event):
        if event.keysym == "w":
            self.y = -self.speed

        if event.keysym == "s":
            self.y = self.speed
    def stop(self, event):
        if event.keysym in "ws":
            self.y = 0
class Plaer2(Player):
    def __init__(self):
        super().__init__()
        self.id = canvas.create_rectangle(1270, 512, 1255, 570, fill = "white")
        self.y = 0
        self.speed = 5

    def move(self, event):
        if event.keysym == "Up":
            self.y = -self.speed

        if event.keysym == "Down":
            self.y = self.speed

    def stop(self, event):
        if event.keysym in ("Up", "Down"):
            self.y = 0

class Shar:
    def __init__(self):
        self.id = canvas.create_oval(640, 512, 645, 517, fill= "white")
        self.y = 5
        self.x = 10
    def draw(self):
        canvas.move(self.id, self.x, self.y)
        bx, by, bx1, by1 = canvas.coords(self.id)
        xp1, yp1, x1p1, y1p1 = canvas.coords(plaer1.id)
        xp2, yp2, x2p2, y2p2 = canvas.coords(plaer2.id)
        if bx1 >= xp2 and by > yp2 and by1 < y2p2:
            self.x = -self.x

        if bx <= 0:
            canvas.coords(self.id, 640, 512, 645, 517)

        if by <= 0:
            self.y = -self.y

        if bx1 >= 1280:
            canvas.coords(self.id, 640, 512, 645, 517)

        if by1 >= 1024:
            self.y = -self.y

shar = Shar()
plaer1 = Plaer1()
plaer2 = Plaer2()
root.bind_all("<KeyPress>", plaer1.move)
root.bind_all("<KeyPress>", plaer2.move, add = "+")
root.bind_all("<KeyRelease>", plaer1.stop)
root.bind_all("<KeyRelease>", plaer2.stop, add = "+")
while True:
    root.update()
    root.update_idletasks()
    shar.draw()
    plaer1.draw()
    plaer2.draw()
    time.sleep(0.01)