主要是练习使用类,顺便记录下它。因为python这东西有点强大。以后肯定会用得到。比方说,因为要搭建环境很多时候是在搞配置文件,所以可以用python写一个某种工具(假如是apache)的配置文件依赖选项检查程序(假如apache没有语法检查的话),这样就不需要每次检查很久配置文件了,避免很多错误(当然你可以用shell)。
还是那个贪吃蛇程序。截图就不放了。
code:这个贴代码好像会吃掉缩进 >_<
还是那个贪吃蛇程序。截图就不放了。
code:
- #!/usr/bin/env python
- """
- file: snake.py
- run: python snake.py
- written by Zhao Jie at 2016-1-16
- """
- from snake_module import *
- def main_loop():
- # create snake instance
- game_inst = Snake()
- pre_food = False
- first_run = True
- game_inst.drawEdge()
- game_inst.printInfo(flag=True)
- while game_inst.key != 'p':
- game_inst.makeFood(pre_food, first_run)
- pre_food = game_inst.moveSnake()
- first_run = False
- # snake hits the wall or itself, exit
- if not game_inst.detectPos():
- game_inst.printInfo('GAME OVER', flag=False)
- gameExit()
- game_inst.drawSnake()
- game_inst.printInfo(flag=True)
- #time.sleep(pnt_interval)
- game_inst.getKeys()
- game_inst.getDir()
- if game_inst.key == ' ':
- game_inst.key = ''
- while game_inst.key != ' ':
- game_inst.getKeys()
- game_inst.key = ''
- else:
- game_inst.printInfo('GAME EXIT', flag=False)
- gameExit()
- # self-test or be a module
- if __name__ == '__main__':
- main_loop()
编辑回复