弹球游戏:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
from tkinter import * import time
import random
tk = Tk() #创建一个界面
tk.title( "弹球游戏" )
canvas = Canvas(tk,width = 800 ,height = 600 ,bg = "skyblue" ,bd = 0 ,highlightthickness = 0 )
tk.resizable( 0 , 0 ) #表示边框不能被拉伸
canvas.pack() #使部件放在主窗口中
tk.update() #刷新界面
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 , 240 , 100 ) #初始球的位置
stat = [ - 3 , - 2 , - 1 , 1 , 2 , 3 ]
random.shuffle(stat)
self .x = stat[ 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 ]:
return True
return False
def draw( self ): #小球移动
self .canvas.move( self . id , self .x, self .y)
pos = self .canvas.coords( self . id )
if pos[ 1 ]< = 0 : #判断小球是否碰到边框,如果碰到回弹
self .y = 3
if pos[ 3 ]> = self .canvas_height: #判断球拍是否有接到球 ,如果没接到游戏结束
self .hit_bottom = True
if self .hit_paddle(pos) = = True : #判断求是否碰到了球拍,如果碰到了使小球回弹
self .y = - 3
if pos[ 0 ]< = 0 : #来判断球拍是不是碰到了边框,,
self .x = 3
if pos[ 2 ]> = self .canvas_width:
self .x = - 3
class Paddle: #球拍的的类
def __init__( self ,canvans,color):
self .canvas = canvas
self . id = canvas.create_rectangle( 0 , 0 , 150 , 10 ,fill = color)
self .canvas.move( self . id , 400 , 450 )
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 turn_left( self ,event): #每次按键球拍移动的距离
self .x = - 5
def turn_right( self ,event):
self .x = 5
def draw( self ): #球拍移动的方法
pos = self .canvas.coords( self . id )
self .canvas.move( self . id , self .x, 0 )
if pos[ 0 ]< = 0 :
self .x = 0
if pos[ 2 ]> = self .canvas_width:
self .x = 0
paddle = Paddle(canvas, "blue" )
ball = Ball(canvas,paddle, "red" )
while True : #用循环 如果球怕没有接到球就推出
if ball.hit_bottom = = False :
ball.draw()
paddle.draw()
else :
break
tk.update_idletasks() # 不停的刷新画布
tk.update()
time.sleep( 0.01 )
|
总结
以上所述是小编给大家介绍的python编写弹球游戏的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/qq_41655148/article/details/79523800