温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-08-26 10:13:59 来源:亿速云 阅读:164 作者:chen 栏目:大数据

这篇文章主要讲解了“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实现弹球小游戏代码分享这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI