環境:python3.6+windows
開發工具:pycharm/sublime 什麼的都可以
模組:
1安裝cocos模組如果直接pip install cocos 會安裝不了import
cocos
2import
define
3from arena import
arena
4from gameover import gameover
![](https://pic.w3help.cc/0c9/d730fc67b988322415dfa1bcf6bbd.jpeg)
**詳解:
1雖然python開發遊戲不是 python的主流方向,但是對於自己開發的小遊戲玩起來也還是蠻有意思的!import
cocos
2import
define
3from arena import
arena
4from gameover import
gameover
5class
helloworld(cocos.layer.layer):
6 is_event_handler =true78
def__init__
(self):
9 super(helloworld, self).__init__
()10 self.arena =arena()
11self.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)
1819 self.gameover =gameover()
20 self.add(self.gameover, 100000)
2122
defupdate_score(self):
23 self.score.element.text =str(self.arena.snake.score)
2425
defend_game(self):
26 self.gameover.visible =true
27 self.gameover.score.element.text =str(self.arena.snake.score)
2829
defon_mouse_press(self, x, y, buttons, modifiers):
30if
self.gameover.visible:
31 self.gameover.visible =false
32self.arena.unschedule(self.arena.update)
33self.remove(self.arena)
34 self.arena =arena()
35self.add(self.arena)
36self.update_score()
37 cocos.director.director.init(caption="
gluttonous python")
38 cocos.director.director.run(cocos.scene.scene(helloworld()))
![](https://pic.w3help.cc/b06/777ea6899f9fafe6c9f2c07bb7be2.jpeg)
web開發
網路爬蟲
自動化測試/測試開發/自動化運維/運維開發
資料分析/大資料/資料探勘
機器學習/深度學習/人工智慧
Python 貪吃蛇遊戲
coding utf 8 引入turtle庫,製作snake,food的動畫 from turtle import 引入random庫,使food隨機出現 from random import randrange 引入freegames庫,這個庫中封裝了許多元素 我們可以使用正方形元素表示food,...
Python 遊戲 貪吃蛇
貪吃蛇是個非常簡單的遊戲,適合練手。先來看一下我的遊戲截圖 玩法介紹 回車鍵 開始遊戲 空格鍵 暫停 繼續 方向鍵 或 wsad 鍵 控制移動方向。食物分紅 綠 藍三種,分別對應 10 分 20 分 30 分,每吃乙個食物增加對應分值,每增加 100 分速度加快一級,沒有設定關卡,我玩到 1100 ...
學習Python開發小遊戲(四) 貪吃蛇
前提條件 需要安裝pygame,pgzero,numpy windows 1.19.3 功能 1.初始化介面顯示小蛇和食物 2.鍵盤控制角色的上下左右 wasd或者上下左右 移動,當小蛇吃掉食物後蛇身長度加1同時重置食物位置 3.統計小蛇吃掉食物的個數,每吃掉乙個食物分數加1,同時在介面顯示分數 5...