Python Practice Item 1 Pinball game

Source: Internet
Author: User

These days to learn the basic knowledge of Python, and then refer to some online information, completed a small game of their own, pinball game is relatively simple, but has a number of common features of the game, for beginners is a more appropriate exercise project.

Here are the following:

  

Complete program:

  

# python learningpython.py
# python learningpython.py
Import Tkinter #引入GUI工具包
Import Random #引入随机函数
Import Time #引入定时器

#创建一个界面和配置界面的一些基本属性
TK = Tkinter. Tk ()
Tk.title (' Game ')
Tk.resizable (0, 0) #表示不能被拉伸
Tk.wm_attributes ('-topmost ', 1) #通知管理器调整布局大小
Canvas = Tkinter. Canvas (TK, width=500, height=400, Bd=0, highlightthickness=0)
Canvas.pack () #使能器件
Tk.update () #更新界面


Game_startsigal_text=canvas.create_text (430,10,text= ' Down:start ', font= (' Times ', ten), state= ' hidden ')
Game_endsigal_text=canvas.create_text (430,30,text= ' Up:end ', font= (' Times ', ten), state= ' hidden ')
Game_leftsigal_text=canvas.create_text (430,50,text= ' Left:left ', font= (' Times ', ten), state= ' hidden ')
Game_rightsigal_text=canvas.create_text (430,70,text= ' Right:right ', font= (' Times ', ten), state= ' hidden ')

Game_over_text=canvas.create_text (250,200,text= ' GAME over ', font= (' Times ', '), state= ' hidden ')
Game_start_text=canvas.create_text (430,110,text= ' GAME start ', font= (' Times ', ten), state= ' hidden ')
Game_score_text=canvas.create_text (430,90,text= ' score: ', font= (' Times ', ten), state= ' hidden ')


#定义木板类
Class Paddle:
def __init__ (self,canvas,color): #初始化
Self.canvas=canvas
Self.id=canvas.create_rectangle (0,0,150,10,fill=color)
Self.canvas.move (self.id,150,380)
Self.started=false
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)
Self.canvas.bind_all ("<KeyPress-Down>", Self.game_start)
Self.canvas.bind_all ("<KeyPress-Up>", Self.game_stop)
def turn_left (self,evt): #左转
Self.x=-4
def turn_right (self,evt): #右转
Self.x=4
def game_start (self,evt): #开始
Self.started=true
def game_stop (self,evt): #停止
self.started = False
def draw (self): #画出木板
Self.canvas.move (self.id,self.x,0)
Pos=self.canvas.coords (self.id)
If pos[0]<=0:
Self.x=0
If pos[2]>=500:
Self.x=0
Class Ball ():
def __init__ (Self,canvas,paddle,color):
Self.canvas=canvas
Self.paddle=paddle
Self.id=canvas.create_oval (10,10,25,25,fill=color)
Self.canvas.move (self.id,245,100)
starts=[-3,-2,-1,1,1,2,3]
Random.shuffle (starts)
Self.x=starts[0]
Self.y=-3
Self.canvas_height=self.canvas.winfo_height ()
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]:
Return True
Return False
def draw (self):
Self.canvas.move (SELF.ID,SELF.X,SELF.Y)
Pos=self.canvas.coords (self.id)
If Pos[3]>=self.canvas_height:
Self.hit_bottom=true
If Self.hit_paddle (POS) ==true:
Self.y=-3
Score.addscore ()
If pos[0]<=0:
Self.x+=1
If pos[1]<=0:
Self.y=1
If pos[2]>=500:
Self.x-=1
If Pos[3]>=self.canvas_height:
Self.y=-1

Class Score:
def __init__ (Self,canvas,color):
self.score=0;
Self.canvas=canvas
Canvas.itemconfig (Game_score_text, state= ' normal ')
Self.id=canvas.create_text (470,90,text=self.score,fill=color)

Canvas.itemconfig (Game_startsigal_text, state= ' normal ')
Canvas.itemconfig (Game_endsigal_text, state= ' normal ')
Canvas.itemconfig (Game_leftsigal_text, state= ' normal ')
Canvas.itemconfig (Game_rightsigal_text, state= ' normal ')
def addscore (self):
Self.score+=1
Self.canvas.itemconfig (Self.id,text=self.score)

Paddle=paddle (canvas, ' Blue ')
Ball=ball (Canvas,paddle, ' red ')
Score=score (canvas, ' black ')


While 1:
If Ball.hit_bottom==false and paddle.started==true:
Ball.draw ()
Paddle.draw ()
Canvas.itemconfig (Game_start_text, state= ' normal ')
If Ball.hit_bottom==true:
Canvas.itemconfig (Game_over_text, state= ' normal ')
Canvas.itemconfig (Game_start_text, state= ' hidden ')
Tk.update_idletasks ()
Tk.update ()
Time.sleep (0.01)

Welcome to the Exchange!

Python Practice Item 1 Pinball game

Related Article

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.