Final Project 期末项目: PySnake

Ginger ·
更新时间:2024-09-20
· 795 次阅读

贪吃蛇的PyGame实现 Snake game with PyGame

一个期末作业的贴,模仿诺基亚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.

组分 Subjects 菜单 menu 单人模式 single player mode 双人模式 multi player mode 要求 Requirements 主菜单 menu: 有基本的用户界面 basic user interface in terminal 允许用户在界面下进行模式选择 allow player to choose mode 程序会根据用户选择执行相应的源代码 run code according to player input 单人模式 有用户界面 basic user interface in window 蛇会动(看似简单)the snake moves(harder than you think) 蛇会根据用户输入改变方向 snake changes firection with player input 蛇吃树莓后增加长度 snake grow longer after eating raspberry 树莓的位置会随机生成 raspberry regenerate after eaten 多人模式 有2.的这些功能 all functions as single mode 两条蛇,一红一绿。一个玩家用WASD控制,另外一个用上下左右控制 one player control with WASD while the other with arrow keys 蛇死了之后,不结束游戏,重新生成蛇 snake regenerate after death, instead of game over 有多个树莓,避免双龙戏一珠 multiple raspberries 游戏主菜单 PySnakeMenu 1.导入必要的库和文件 import modules and files

这里,导入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()

从一个python文件运行另外一个python文件也是一种学问 Running one python file from another

方法一,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.

单人模式 Single-player Mode

大致思路:
Rough outline:
原创文章 1获赞 0访问量 88 关注 私信 展开阅读全文
作者:Richard Browning



final project

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章