重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Python实现弹球小游戏代码分享

这篇文章主要讲解了“Python实现弹球小游戏代码分享”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python实现弹球小游戏代码分享”吧!

成都创新互联公司始终坚持【策划先行,效果至上】的经营理念,通过多达十多年累计超上千家客户的网站建设总结了一套系统有效的营销推广解决方案,现已广泛运用于各行各业的客户,其中包括:软装设计等企业,备受客户称扬。

整个游戏实现比较简单,只需在安装python的电脑上即可运行,玩游戏,通过键盘键控制弹球挡板的移动即可。原理不多说,且让我们去看看吧。

1、代码运行后,游戏界面如下所示:

Python实现弹球小游戏代码分享

2、游戏结束后,界面如下所示:

Python实现弹球小游戏代码分享

游戏实现部分源码如下:

def main():
    tk = tkinter.Tk()

    # call back for Quit
    def callback():
        if mb.askokcancel("Quit", "Do you really wish to quit?"):
            Ball.flag = False
            tk.destroy()

    tk.protocol("WM_DELETE_WINDOW", callback)

    # Init parms in Canvas
    canvas_width = 600
    canvas_hight = 500
    tk.title("小弹球游戏V1版")
    tk.resizable(0, 0)
    tk.wm_attributes("-topmost", 1)
    canvas = tkinter.Canvas(tk, width=canvas_width, height=canvas_hight, bd=0, highlightthickness=0, bg='#00ffff')
    canvas.pack()
    tk.update()

    score = Score(canvas, 'red')
    paddle = Paddle(canvas, "magenta")
    ball = Ball(canvas, paddle, score, "grey")

    game_over_text = canvas.create_text(canvas_width / 2, canvas_hight / 2, text='Game over', state='hidden',
                                        fill='red', font=(None, 18, "bold"))
    introduce = '欢迎来到小弹球游戏 V1版:\n点击任意键--开始\n停止--回车键\n继续--回车键\n'
    game_start_text = canvas.create_text(canvas_width / 2, canvas_hight / 2, text=introduce, state='normal',
                                         fill='magenta', font=(None, 18, "bold"))
    while True:
        if (ball.hit_bottom == False) and ball.paddle.started:
            canvas.itemconfigure(game_start_text, state='hidden')
            ball.draw()
            paddle.draw()
        if ball.hit_bottom == True:
            time.sleep(0.1)
            canvas.itemconfigure(game_over_text, state='normal')
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)


if __name__ == '__main__':
    main()

感谢各位的阅读,以上就是“Python实现弹球小游戏代码分享”的内容了,经过本文的学习后,相信大家对Python实现弹球小游戏代码分享这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页名称:Python实现弹球小游戏代码分享
网站网址:http://cqcxhl.com/article/gdojio.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP