from tkinter import *
import time
a =Tk()
a1 = Canvas(a,width =400,height =500,bd = 0)
a1.pack()
class ball:
def __init__(self,a1,ba1,ball,c):
self.a1 =a1
self.ba1 = ba1
self.b1 = a1.create_oval(10,10,20,20,fill = c)
self.ball =ball
self.x1 = -3
self.y1 = 3
a1.move(self.b1,30,40)
def ft(self):
a1.move(self.b1,self.x1,self.y1)
p=a1.coords(ba1.a2)
p1 = a1.coords(self.b1)
q = 1
if p[0]<=p1[0] and p[2]>=p1[2]:
if p1[3] == 480:
self.y1 = -5
class ban:
def __init__(self,a1,c):
self.a1=a1
self.a2 = a1.create_rectangle(10,10,100,30,fill = c)
a1.move(self.a2,200,470)
self.a1.bind_all("<KeyPress-a>",self.zuo)
self.a1.bind_all("<KeyPress-d>",self.you)
self.x =0
def zuo(self,evt):
self.x = -5
def you(self,evt):
self.x = 5
def yd(self):
a1.move(self.a2,self.x,0)
p = a1.coords(self.a2)
if self.x == -5:
self.x = 0
if self.x == 5:
selfx = 0
class qiang:
def __init__(self,a1,ba,c):
self.a1 = a1
self.ba =ba
self.q = a1.create_rectangle(1,3,500,3,fill = c)
self.q1 =a1.create_rectangle(1,1,3,500,fill = c)
self.q2 =a1.create_rectangle(397,1,397,500,fill = c)
def fan(self):
p2 =a1.coords(self.q)
p3 =a1.coords(self.q1)
p4 =a1.coords(self.q2)
p5 =a1.coords(ba.b1)
if p5[0]<=p2[1]+2:
ba.x1 = 3
if p5[1]<=p3[1]+2:
ba.y1 = 3
if p5[2]>=p4[0]:
ba.x1 = -3
ba1=ban(a1,"yellow")
ba = ball(a1,ba1,ball,"red")
qi = qiang(a1,ba,"black")
while True:
time.sleep(0.003)
ba.ft()
qi.fan()
ba1.yd()
a1.update()
import time
a =Tk()
a1 = Canvas(a,width =400,height =500,bd = 0)
a1.pack()
class ball:
def __init__(self,a1,ba1,ball,c):
self.a1 =a1
self.ba1 = ba1
self.b1 = a1.create_oval(10,10,20,20,fill = c)
self.ball =ball
self.x1 = -3
self.y1 = 3
a1.move(self.b1,30,40)
def ft(self):
a1.move(self.b1,self.x1,self.y1)
p=a1.coords(ba1.a2)
p1 = a1.coords(self.b1)
q = 1
if p[0]<=p1[0] and p[2]>=p1[2]:
if p1[3] == 480:
self.y1 = -5
class ban:
def __init__(self,a1,c):
self.a1=a1
self.a2 = a1.create_rectangle(10,10,100,30,fill = c)
a1.move(self.a2,200,470)
self.a1.bind_all("<KeyPress-a>",self.zuo)
self.a1.bind_all("<KeyPress-d>",self.you)
self.x =0
def zuo(self,evt):
self.x = -5
def you(self,evt):
self.x = 5
def yd(self):
a1.move(self.a2,self.x,0)
p = a1.coords(self.a2)
if self.x == -5:
self.x = 0
if self.x == 5:
selfx = 0
class qiang:
def __init__(self,a1,ba,c):
self.a1 = a1
self.ba =ba
self.q = a1.create_rectangle(1,3,500,3,fill = c)
self.q1 =a1.create_rectangle(1,1,3,500,fill = c)
self.q2 =a1.create_rectangle(397,1,397,500,fill = c)
def fan(self):
p2 =a1.coords(self.q)
p3 =a1.coords(self.q1)
p4 =a1.coords(self.q2)
p5 =a1.coords(ba.b1)
if p5[0]<=p2[1]+2:
ba.x1 = 3
if p5[1]<=p3[1]+2:
ba.y1 = 3
if p5[2]>=p4[0]:
ba.x1 = -3
ba1=ban(a1,"yellow")
ba = ball(a1,ba1,ball,"red")
qi = qiang(a1,ba,"black")
while True:
time.sleep(0.003)
ba.ft()
qi.fan()
ba1.yd()
a1.update()