Python实现弹球小游戏
作者:吃着东西不想停 时间:2022-10-06 21:21:16
本文主要给大家分享一个实战项目,通过python代码写一款我们儿时大多数人玩过的游戏---小弹球游戏。只不过当时,我们是在游戏机上玩,现在我们通过运行代码来玩,看看大家是否有不一样的体验,是否可以重温当年的乐趣呢!
整个游戏实现比较简单,只需在安装python的电脑上即可运行,玩游戏,通过键盘键控制弹球挡板的移动即可。原理不多说,且让我们去看看吧。
1、代码运行后,游戏界面如下所示:
2、游戏过程中,界面如下所示:
3、游戏结束后,界面如下所示:
游戏实现部分源码如下:
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()
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
来源:https://www.cnblogs.com/zwhy8/p/13411702.html
标签:Python,弹球,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何批量消除网页超级链接上的“虚线框”
2008-10-15 12:55:00
python 实现的截屏工具
2023-05-15 00:20:39
![](https://img.aspxhome.com/file/2023/5/86215_0s.png)
js控制文本框输入的字符类型方法汇总
2024-04-10 13:57:03
重新认识CSS的权重
2011-05-24 17:06:00
php截取utf-8中文字符串乱码的解决方法
2024-04-29 13:56:52
python数据分析matplotlib的基础绘图使用
2022-09-08 18:07:08
![](https://img.aspxhome.com/file/2023/8/78548_0s.png)
Python Web服务器Tornado使用小结
2023-06-25 23:07:21
Ubuntu 16.04/18.04 安装Pycharm及Ipython的教程
2023-11-03 03:49:29
![](https://img.aspxhome.com/file/2023/1/63461_0s.png)
python工具之清理 Markdown 中没有引用的图片
2022-02-14 11:44:39
![](https://img.aspxhome.com/file/2023/0/67300_0s.png)
Python+Kepler.gl实现时间轮播地图过程解析
2021-03-27 00:20:52
![](https://img.aspxhome.com/file/2023/0/90320_0s.jpg)
IE6 bug: 消失的绝对定位元素
2009-12-04 12:11:00
vue.js选中动态绑定的radio的指定项
2024-04-27 16:13:25
![](https://img.aspxhome.com/file/2023/8/132938_0s.png)
python获取指定目录下所有文件名列表的方法
2022-03-24 08:05:39
![](https://img.aspxhome.com/file/2023/9/104879_0s.png)
python数组过滤实现方法
2021-09-11 20:28:44
Python通过websocket与js客户端通信示例分析
2021-03-23 08:35:49
![](https://img.aspxhome.com/file/2023/4/107674_0s.png)
详解python中eval函数的作用
2022-06-03 07:46:31
关于python环境变量如何配置
2023-06-02 14:23:09
![](https://img.aspxhome.com/file/2023/7/98937_0s.jpg)
apache集成php5.6方法分享
2023-09-06 09:03:54
![](https://img.aspxhome.com/file/2023/7/55477_0s.png)
javascript内置对象Math案例总结分析
2024-04-10 13:56:18
![](https://img.aspxhome.com/file/2023/7/136877_0s.png)
Python3.5面向对象与继承图文实例详解
2021-05-14 15:48:42
![](https://img.aspxhome.com/file/2023/4/102944_0s.png)