通过 tkinter 采用非面相对象式实现弹球小游戏(使用蹩脚式面相对象实现)

#!/user/bin env python
# author:Simple-Sir
# time:2020/8/3 17:12
import tkinter,time,random

x = 1
y = 1
score = 0  # 分数
level = 1  # 关卡

# 创建颜色列表
color_li = ['#feeeed','#f391a9','#fab27b','#454926','#181d4b','#a3cf62','#45b97c','#008792','#2585a6']

# 创建窗口
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)  # 创建一个“画布”

# 创建画布组件

filename = tkinter.PhotoImage(file="bg.png")  # 获取一张图片
canvas.create_image(300, 250,image=filename)  # 将图片添加到画布,作为背景

id_ball = canvas.create_oval(10, 10, 30, 30, fill=random.choice(color_li),width=0)  # 定义一个球
id_paddle = canvas.create_rectangle(0,400,150,420,fill=random.choice(color_li),width=0)  # 定义木板
canvas.move(id_paddle,225,0)
canvas.create_text(400, 20, text='关卡:',fill='white',font=('宋体', '20'))
canvas.create_text(500, 20, text='得分:',fill='white',font=('宋体', '20'))

id_level = canvas.create_text(450, 20, text=1,fill='white',font=('宋体', '20'))
id_score = canvas.create_text(550, 20, text=0,fill='white',font=('宋体', '20'))
id_start_game = canvas.create_text(300,200,text='Start Game.',font=('宋体', '30'),fill=random.choice(color_li))

canvas.pack()  # 将画布添加到窗口中
tk.update()  # 刷新窗口

wh = canvas.winfo_height()  # 获取窗口高度(update刷新之后才能获取)
ww = canvas.winfo_width()  # 获取窗口宽度(update刷新之后才能获取)

def turn_left(event):
    canvas.move(id_paddle, -10, 0)

def turn_right(event):
    canvas.move(id_paddle, 10, 0)

def start_game(event):
    canvas.move(id_start_game,0,10)

canvas.bind_all('<KeyPress-Left>', turn_left)
canvas.bind_all('<KeyPress-Right>', turn_right)
canvas.bind_all('<Button-1>', start_game)

# 保持起始界面
while 1:
    tk.update()  # 刷新窗口
    p_start_game = canvas.coords(id_start_game)
    print(p_start_game)
    if p_start_game[1] == 210:
        canvas.delete(id_start_game)
        break

# 让球跑起来
while 1:
    tk.update()  # 刷新窗口
    time.sleep(0.01/level)  # 通过level控制速度(也考虑过用球的颜色控制速度)
    p_ball = canvas.coords(id_ball)  # 获取球当前位置坐标
    p_paddle = canvas.coords(id_paddle)  # 获取木板的坐标

    print('当前坐标:',p_ball)  # 打印当前坐标
    if p_ball[0] <= 0:  # 当球落到右边框时:左上角x坐标判断
        x = 1
    elif p_ball[2] >= ww:  # 当球落到右边框时,右下角x坐标判断
        x = -1
    if p_ball[1] <= 0:  # 当球落到上边框时,左上角y坐标判断
        y = 1
    elif p_ball[3] >= wh:  # 当球落到下边框时,右下角y坐标判断
        y = -1
    print(p_ball[2],p_paddle[0],p_paddle[2],p_ball[3])
    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坐标
        y = -1  # 让球向上移动
        score += 10  # 得分加10分
        canvas.itemconfig(id_ball, fill=random.choice(color_li))  # 修改球的颜色,随机颜色
        canvas.itemconfig(id_paddle, fill=random.choice(color_li))  # 修改木板的颜色,随机颜色
        canvas.itemconfig(id_score, text=score)  # 修改分数
        if score > 0 and score % 50 == 0:  # 每50分升一级
            level += 1  # 升级
            canvas.itemconfig(id_level, text=level)  # 修改等级
    canvas.move(id_ball, x, y)  # 移动
    if p_ball[3] == wh:  # 当球与下边框接触时,游戏失败。
        canvas.create_text(300, 250, text='Game Over !', font=('宋体', '30'), fill='red')  # 添加游戏结束界面
        break

# 游戏结束,继续保持界面
tk.mainloop()
弹球小游戏

相关文章:

  • 2021-04-03
  • 2021-08-24
  • 2022-12-23
  • 2021-10-31
  • 2021-09-20
  • 2021-11-14
  • 2022-02-25
  • 2021-12-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2023-01-17
  • 2022-12-23
  • 2022-01-11
  • 2021-06-11
  • 2021-10-31
相关资源
相似解决方案