一个期末作业的贴,模仿诺基亚3310上的贪吃蛇游戏,有基于指令行的菜单,和基于图形界面的的双人和单人模式。这个程序使用第三方PyGame库,采用GUI界面,并且有像素化的风格,是个看似简单,但却满是学问的小游戏,希望对大家初入PyGame有所帮助。
A final project, in imitation of the snake game on Nokia 3310 with help of pygame. Simple but not easy-to-make game.
这里,导入sys是为确保程序中的退出等功能可以正常运行。
Imports sys to ensure program functions like quit run normally
也导入了另外两个文件,是游戏的两个模式的源代码。至于为甚要在这里导入,笔者稍后解释是为什么。
Other two files imported are source code of the two modes.
Will explain later why import here.
import sys, pygame, PySnakeSingle, PySnakeMulti
2.显示用户界面 Print out welcome interface
这里是主程序的开始。打印出一个欢迎界面和一个选项列表。
Main function starts here.
这丑丑的蛇和树莓就不过多解释了,唯一值得一提的是用到了罕见的上划线符号“ ̄”。
There are nothing special about the ugly snake and raspberry, but the use of “ ̄”.
选项列表列出了单人模式,双人模式和退出。用户输入的数据会在下一步用到。
The single, multi mode of the game and the quit are listed as options.
def main():
print('================================================================================================')
print('Welcome to PySnake, a Python-based snake-eating-raspberry game with help of the Pygame module')
print('================================================================================================')
print(' _= ̄=_ ')
print(' // \\\ _**_ ')
print(' // \\\ / ̄O-_ // \\')
print(' // \\\ /// _=--< || ||')
print(' // \\\ // --= ̄ \\ //')
print(' // \\\ //  ̄ ')
print('-==/ =__= ')
print('===================================================== ')
print('Please select game mode:\n [1]Single player mode\n [2]Multi player mode\n [3]Quit\n')
3.提取用户输入并判断
这里是菜单程序的精华。先提取用户输入,再判断输入并进行相应的操作(即运行相应的程序(函数))
The essense of PySnake Menu. Here the user input is taken and program will be ran accordingly.
一、先用input()函数提取输入,外嵌套的int()转换成整数
First, get input with input() and convert to int with int().
二、下面的判断语法根据用户输入执行程序,the boolean expression judges user input and ran functions.
如果输入:
1,就是单人模式 Input 1 will start single mode
2,就是双人 Input 2 will start multi mode
3,就会退出,刚才导入的sys就是在用在这里:sys.exit(),这句作用是退出当前程序 Input 3 and the program will quit, with the help of sys.exit()
方法一,import
将该文件视作头文件导入,例如导入了file.py:
import as module
import file
运行file中的main是就用:
and run functions in that file
file.main()
这种方法最为高效,稳定,安全
efficient, stable and safe
方法二,exec
顾名思义,直接运行,
run with python function dedicated to run another file
python2里:execfile('file.py')
python3里:exec(open('file.py').read())
这种方式危险,不稳定,而且慢一丢丢,建议尽可能不用
dangerous and hacky, use with caution
方法三,os.system
这种方法调取系统指令行运行,
run with system command line
os.system('python file.py')
是没办法的办法
desperate way
注:来自StackOverflow,如何从一个python程序运行另外一个 How can I make one python file run another? [duplicate]
这里,我们用方法一,先前导入的py文件就派上用场了,
Here we use way 1
这里如果提示导入失败,在同一文件夹下建立一个叫__init__.py的空文件可以修复,两条下划线哦
if cannot import, put a empty file called __init__.py in the same directory, which tells python interperator it is okay to include
选择1时,就运行单人模式代码中的主程序;
Chosen 1, run single mode.
选择2时,就运行双人模式代码中的主程序;
Chosen 2, run multi mode.
选择3,退出。
Chosen 3, quit.
如果输入123之外的数,会提示再次输入。
Chosen something else, will be prompt to input again.
while True:
select=int(input())
if select==1:
PySnakeSingle.singlemain()
elif select==2:
PySnakeMulti.multimain()
elif select==3:
print('Thank you for using.')
sys.exit()
else:
print('Please select again.')
最后,用这个执行上述的代码
And run the main function above.
if __name__=='__main__':
main()
菜单就这样大功告成了!接下来讲一下单人模式。
So much for the Menu! Lets talk about single-player mode next.
大致思路:
Rough outline:
原创文章 1获赞 0访问量 88
关注
私信
展开阅读全文
作者:Richard Browning