初识python:tkinter 实现 弹球小游戏(面向对象)

使用蹩脚式面相对象,实现弹球小游戏(非面向对象实现,主要介绍tk基础用法)。

初识python:tkinter 实现 弹球小游戏(面向对象)初识python:tkinter 实现 弹球小游戏(面向对象)
#!/user/bin env python
# author:Simple-Sir
# time:2020/8/7 10:09 import tkinter,time,random # 创建颜色列表
COLOR = ['#f173ac','#73b9a2','#fdb933','#d71345','#fffef9','#afdfe4','#ffe600'] # 创建窗口
tk = tkinter.Tk() # 声明一个TK,初始化一个“窗口”(画一个窗口)
tk.title('弹球游戏') # 窗口名称
tk.resizable(width=False,height=False) # 窗口是否可变(长、宽),也可用0,1表示
tk.wm_attributes('-topmost',1) # 窗口永远在前 # 创建画布
canvas = tkinter.Canvas(tk,width=600,height=500,bd=0) # 创建一个“画布”
canvas.pack() # 将画布添加到窗口中
tk.update() # 创建背景
class Bg():
def __init__(self,canvas,file):
self.filename = tkinter.PhotoImage(file=file) # 获取一张图片
canvas.create_image(300, 250,image=self.filename) # 将图片添加到画布,作为背景 # 创建球
class Ball():
def __init__(self,canvas,COLOR):
self.COLOR = COLOR
self.id_ball = canvas.create_oval(10,10,30,30, fill=random.choice(COLOR), width=0) # 定义一个球
canvas.move(self.id_ball,random.randint(-10,300),random.randint(-20,300))
self.ball_p_text = canvas.create_text(120, 20, text='球的坐标:{0}'.format(canvas.coords(self.id_ball)), fill='white')
self.x = 1
self.y = 1
self.score = 0
self.level = 1
self.wh = canvas.winfo_height() # 获取窗口高度(update刷新之后才能获取)
self.ww = canvas.winfo_width() # 获取窗口宽度(update刷新之后才能获取) def run_ball(self):
p_ball = canvas.coords(self.id_ball)
if p_ball[0] <= 0: # 当球落到右边框时:左上角x坐标判断
self.x = 1
elif p_ball[2] >= self.ww: # 当球落到右边框时,右下角x坐标判断
self.x = -1
if p_ball[1] <= 0: # 当球落到上边框时,左上角y坐标判断
self.y = 1
elif p_ball[3] >= self.wh: # 当球落到下边框时,右下角y坐标判断
self.y = -1 def touch(self):
p_ball = canvas.coords(self.id_ball)
canvas.itemconfig(self.ball_p_text,text='球的坐标:{0}'.format(p_ball))
p_paddle = canvas.coords(paddle.id_paddle) # 获取木板的坐标
if p_ball[2] >= p_paddle[0] and p_ball[2] <= p_paddle[2] and p_ball[3] == p_paddle[1]: # 球与模板接触:球的右下角x坐标在木板右上角x坐标内,且球的右下角x坐标在木板左下角x坐标内,球的右下角y坐标等于木板的左上角y坐标
self.y = -1 # 让球向上移动
self.score += 10 # 得分加10分
canvas.itemconfig(self.id_ball, fill=random.choice(self.COLOR)) # 修改球的颜色,随机颜色
canvas.itemconfig(paddle.id_paddle, fill=random.choice(self.COLOR)) # 修改木板的颜色,随机颜色
if self.score > 0 and self.score % 50 == 0: # 每50分升一级
self.level += 1 # 升级
canvas.move(self.id_ball, self.x, self.y) # 移动 class Paddle():
def __init__(self,canvas,COLOR):
self.canvas=canvas
self.COLOR=COLOR
self.x = 0
self.id_paddle = canvas.create_rectangle(0,400,150,420,fill=random.choice(COLOR),width=0) # 定义木板
self.canvas.move(self.id_paddle,225,0)
self.ww = canvas.winfo_width() # 获取窗口宽度(update刷新之后才能获取)
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
self.canvas.bind_all('<KeyPress-Right>', self.turn_right) def run_paddle(self):
canvas.move(self.id_paddle, self.x, 0) # 先移动,再判断位置。若先判断,在移动,则位置永远是0或最大
p_paddle = canvas.coords(self.id_paddle) # 获取木板的坐标
if p_paddle[0] <= 0 or p_paddle[2] >= self.ww:
self.x = 0 def turn_left(self,event):
p_paddle = canvas.coords(self.id_paddle) # 获取木板的坐标
if p_paddle[0] <= 0:
self.x = 0
else:
self.x = -2 def turn_right(self,event):
p_paddle = canvas.coords(self.id_paddle) # 获取木板的坐标
if p_paddle[2] >= self.ww:
self.x = 0
else:
self.x = 2 class Score():
def __init__(self,canvas):
canvas.create_text(400, 20, text='关卡:',fill='white',font=('宋体', '15'))
canvas.create_text(500, 20, text='得分:',fill='white',font=('宋体', '15'))
self.id_level = canvas.create_text(440, 20, text=1,fill='white',font=('宋体', '15'))
self.id_score = canvas.create_text(540, 20, text=0,fill='white',font=('宋体', '15')) def update_lv_sc(self):
canvas.itemconfig(self.id_level, text=ball.level) # 修改等级
canvas.itemconfig(self.id_score, text=ball.score) # 修改分数 class Sys_game():
is_start = False
def __init__(self,canvas):
self.id_start_game = canvas.create_text(300, 200, text='Start Game !', font=('宋体', '30'), fill='white')
canvas.bind_all('<Button-1>', self.start_game)
self.wh = canvas.winfo_height() # 获取窗口高度(update刷新之后才能获取) def start_game(self,event):
if self.is_start:
self.is_start = False
canvas.itemconfig(self.id_start_game,text='Stop Game !',state='normal',fill='red')
else:
self.is_start = True
canvas.itemconfig(self.id_start_game,state='hidden') def is_play(self):
p_ball = canvas.coords(ball.id_ball)
if p_ball[3] == self.wh: # 当球与下边框接触时,游戏失败。
canvas.create_text(300, 250, text='Game Over !', font=('宋体', '30'), fill='red') # 添加游戏结束界面
self.is_start = False # 搞起
bg = Bg(canvas,'bg.png')
ball = Ball(canvas,COLOR)
paddle = Paddle(canvas,COLOR)
score = Score(canvas)
sys_game = Sys_game(canvas)
while 1:
sys_game.is_play()
if sys_game.is_start == True:
tk.update()
ball.run_ball()
ball.touch()
score.update_lv_sc()
paddle.run_paddle()
time.sleep(0.01/ball.level)
else:
tk.update()

弹球小游戏

运行结果:

初识python:tkinter 实现 弹球小游戏(面向对象)

初识python:tkinter 实现 弹球小游戏(面向对象)

初识python:tkinter 实现 弹球小游戏(面向对象)

初识python:tkinter 实现 弹球小游戏(面向对象)

上一篇:win10 Faster-RCNN训练自己数据集遇到的问题集锦 (转)


下一篇:初识python:tkinter 实现 弹球小游戏(非面相对象)