Python仿手机游戏开发贪吃蛇大作战

本文介绍了一个使用Python和cocos2d库开发的简单游戏案例,包括游戏环境搭建、代码实现及运行过程。通过实例展示了如何处理游戏得分更新、游戏结束状态及鼠标事件响应等关键环节。

环境:Python3.6+Windows

开发工具:Pycharm/Sublime 什么的都可以

模块:

1 import cocos
2 import define
3 from arena import Arena
4 from gameover import Gameover

安装cocos模块

如果直接pip install cocos 会安装不了

 

1 pip install cocos2d

 

代码详解:

 1 import cocos
 2 import define
 3 from arena import Arena
 4 from gameover import Gameover
 5 class HelloWorld(cocos.layer.Layer):
 6     is_event_handler = True
 7 
 8     def __init__(self):
 9         super(HelloWorld, self).__init__()
10         self.arena = Arena()
11         self.add(self.arena)
12         self.score = cocos.text.Label('30',
13                                       font_name='Times New Roman',
14                                       font_size=24,
15                                       color=define.GOLD)
16         self.score.position = 20, 440
17         self.add(self.score, 99999)
18 
19         self.gameover = Gameover()
20         self.add(self.gameover, 100000)
21 
22     def update_score(self):
23         self.score.element.text = str(self.arena.snake.score)
24 
25     def end_game(self):
26         self.gameover.visible = True
27         self.gameover.score.element.text = str(self.arena.snake.score)
28 
29     def on_mouse_press(self, x, y, buttons, modifiers):
30         if self.gameover.visible:
31             self.gameover.visible = False
32             self.arena.unschedule(self.arena.update)
33             self.remove(self.arena)
34             self.arena = Arena()
35             self.add(self.arena)
36             self.update_score()
37 cocos.director.director.init(caption="Gluttonous Python")
38 cocos.director.director.run(cocos.scene.Scene(HelloWorld()))

 虽然Python开发游戏不是 Python的主流方向,但是对于自己开发的小游戏玩起来也还是蛮有意思的!

那么Python的主流方向有哪些呢?

  1. web开发
  2. 网络爬虫
  3. 自动化测试/测试开发/自动化运维/运维开发
  4. 数据分析/大数据/数据挖掘
  5. 机器学习/深度学习/人工智能

 

转载于:https://www.cnblogs.com/pythonfm/p/9060632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值