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


    环境: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. 机器学习/深度学习/人工智能
  • 相关阅读:
    决策树分类
    集群服务器 获取COOKIE错误
    React 自写Loading
    HTB-靶机-Unattended
    HTB-靶机-LaCasaDePapel
    HTB-靶机-FriendZone
    HTB-靶机-CTF
    HTB-靶机-FluJab
    HTB-靶机-Help
    HTB-靶机-Chaos
  • 原文地址:https://www.cnblogs.com/pythonfm/p/9060632.html
Copyright © 2020-2023  润新知