Python first game __python

Source: Internet
Author: User

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]&GT;=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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.