Racket catch, racket can move around, the ball landed, the game over.
From tkinter Import * Import random Import time Class Ball:def __init__ (self,canvas,color): Self.canvas=canva S Self.paddle=paddle self.id=canvas.create_oval (10,10,25,25,fill=color) self.canvas.move (self.id,2
45,100) starts=[-3,-2,-1,1,2,3] Random.shuffle (starts) self.x=starts[0] Self.y=-3 Self.canvas_height=self.canvas.winfo_height () self.canvas_width=self.canvas.winfo_width () self.hit_bottom= False def hit_paddle (self,pos): Paddle_pos=self.canvas.coords (self.paddle.id) if Pos[2]>=paddle_pos [0] and pos[0]<=paddle_pos[2]: if POS[3]>=PADDLE_POS[1] and Pos[3]<=paddle_pos[3]: RE Turn True return False def draw (self): Self.canvas.move (SELF.ID,SELF.X,SELF.Y) pos=self.ca Nvas.coords (self.id) if pos[1]<=0:self.y=3 if pos[3]>=self.canvas_height:s
Elf.hit_bottom=true If Self.hit_paddle (POS) ==true:self.y=-3 if pos[0]<=0:self.x=3 if POS[2]&G
T;=self.canvas_width:self.x=-3 class Paddle:def __init__ (self,canvas,color): Self.canvas=canvas
Self.id=canvas.create_rectangle (0,0,100,10,fill=color) self.canvas.move (self.id,200,300) self.x=0
Self.canvas_width=self.canvas.winfo_width () self.canvas.bind_all (' <KeyPress-Left> ', self.turn_left) Self.canvas.bind_all (' <KeyPress-Right> ', self.turn_right) def draw (self): Self.canvas.move (self.id,self.x , 0) pos=self.canvas.coords (self.id) if pos[0]<=0:self.x=0 elif Pos[2]>=self.can Vas_width:self.x=0 def turn_left (self,evt): Self.x=-2 def turn_right (self,evt): self. x=2 tk=tk () tk.title ("Game") tk.resizable (0,0) tk.wm_attributes ("-topmost", 1) Canvas=canvas (tk,width=500,height=400 , bd=0,highlightthickness=0) canVas.pack () tk.update () paddle=paddle (canvas, ' Blue ') ball=ball (canvas, ' red ') while 1:if Ball.hit_bottom==false: Ball.draw () Paddle.draw () Tk.update_idletasks () tk.update () time.sleep (0.01)
Result: