python简单游戏设计

来源:互联网 发布:罗马全面战争火攻算法 编辑:程序博客网 时间:2024/06/11 05:01

python简单游戏设计

用python脚本对对象编程,结合其他方式,简单编辑一个模拟游戏。该游戏以王者荣耀的地图为背景,可以简单实现以下功能:

  • 以单机的形式运行,用户没有密码,只需要输入玩家名称即可进入,退出后还原;
  • 游戏有三个默认玩家:”zhangqincheng”、”huliting”、”anjinchuan”。如果需要加入新的玩家名称,可以自己创建,输入玩家名称,年龄,性别即可,战斗值默认为1500;
  • 可以显示所有玩家的名称;
  • 输入玩家名称登陆游戏后,自动显示玩家信息。游戏地图是以消耗战斗值为基础的,可以选择五个地图,这五个地图是:

    • 墨家机关道,消耗100战斗值。游戏中地图名为”mojiajiguandao”;
    • 长平攻防战,消耗200战斗值。游戏中地图名为”changpinggongfangzhan”;
    • 王者峡谷,消耗150战斗值。游戏中地图名为”wangzhexiagu”;
    • 深渊大战斗,消耗300战斗值。游戏中地图名为”shenyuandazhandou”;
    • 火焰山大战,消耗500战斗值。游戏中地图名为”huoyanshandazhan”;
  • 玩家可以选择退出登录,也可以选择退出游戏;

该模拟游戏脚本如下所示:

import timeclass Game(object):    def __init__(self, name, age, gendar, com_value):        self.name = name        self.age = age        self.gendar = gendar        self.com_value = com_value    def mojia(self):        print "Welcome to mojiajiguandao !"        time.sleep(1)        print "You have {} combat value !".format(self.com_value)        time.sleep(1)        print "This map will reduce 100 combat value !"        self.com_value -= 100        time.sleep(2)        print "20 min pass...."        time.sleep(5)        print "Game over ! "        time.sleep(2)        print "You have {} combat value now !".format(self.com_value)        return time.sleep(1.5)    def changping(self):        print "Welcome to changpinggongfangzhan !"        time.sleep(1)        print "You have {} combat value !".format(self.com_value)        time.sleep(1)        print "This map will reduce 200 combat value !"        self.com_value -= 200        time.sleep(2)        print "25 min pass...."        time.sleep(5)        print "Game over ! "        time.sleep(2)        print "You have {} combat value now !".format(self.com_value)        return time.sleep(1.5)    def wangzhe(self):        print "Welcome to wangzhexiagu !"        time.sleep(1)        print "You have {} combat value !".format(self.com_value)        time.sleep(1)        print "This map will reduce 150 combat value !"        self.com_value -= 150        time.sleep(2)        print "15 min pass...."        time.sleep(5)        print "Game over ! "        time.sleep(2)        print "You have {} combat value now !".format(self.com_value)        return time.sleep(1.5)    def shenyuan(self):        print "Welcome to shenyuandazhandou !"        time.sleep(1)        print "You have {} combat value !".format(self.com_value)        time.sleep(1)        print "This map will reduce 300 combat value !"        self.com_value -= 300        time.sleep(2)        print "30 min pass...."        time.sleep(5)        print "Game over ! "        time.sleep(2)        print "You have {} combat value now !".format(self.com_value)        return time.sleep(1.5)    def huoyanshan(self):        print "Welcome to huoyanshandazhan !"        time.sleep(1)        print "You have {} combat value !".format(self.com_value)        time.sleep(1)        print "This map will reduce 500 combat value !"        self.com_value -= 500        time.sleep(2)        print "40 min pass...."        time.sleep(5)        print "Game over ! "        time.sleep(2)        print "You have {} combat value now !".format(self.com_value)        return time.sleep(1.5)zhangqincheng = Game("zhangqincheng", 18, "male", 1000)huliting = Game("huliting", 18, "female", 2000)anjinchuan = Game("anjinchuan", 18, "male", 2500)User_List = {"zhangqincheng": zhangqincheng,             "huliting": huliting,             "anjinchuan": anjinchuan             }def Login():    User = raw_input("Please input your username : ")    if User in User_List:        print """        Login successfully !            username : {}            age : {}            gendar : {}            combat value :{}        """.format(User_List[User].name, User_List[User].age, User_List[User].gendar, User_List[User].com_value)        def mo():            User_List[User].mojia()        def chang():            User_List[User].changping()        def wang():            User_List[User].wangzhe()        def shen():            User_List[User].shenyuan()        def huo():            User_List[User].huoyanshan()        Game_List = {            "1": mo,            "2": chang,            "3": wang,            "4": shen,            "5": huo        }        while True:            print """            Select game that you want to play !            1: mojiajiguandao (-100)            2: changpinggongfangzhan (-200)            3: wangzhexiagu (-150)            4: shenyuandazhandou (-300)            5: huoyanshandazhan (-500)            6: quit            """            game_sel = raw_input("Please input 1-6 : ").strip()            if game_sel in Game_List.keys():                Game_List[game_sel]()            else:                if game_sel == "6":                    return "Exit game !"                else:                    print "Please input 1-6 !"                    print "\n"    else:        return "Username is not exist !"def Exist(username):    if username in User_List.keys():        return False    else:        return Truedef Create():    while True:        New_User = raw_input("Please input your username that you want to create : ")        New_Age = input("Please input your age : ")        New_Gendar = raw_input("Please input your gendar (male or female) : ").strip()        if New_Gendar in ["male", "female"]:            if Exist(New_User):                New_User_Name = Game(New_User, New_Age, New_Gendar, 1500)                User_List.setdefault(New_User, New_User_Name)                return """                Create new player successfully !                    username : {}                    age : {}                    gendar : {}                    combat value : 1500                """.format(New_User_Name.name, New_User_Name.age, New_User_Name.age)            else:                print "%s is exist ! " % (New_User)                print "\n"        else:            print "Please input male or female !"            print "\n"def Show():    print "All players are :"    for i in User_List.keys():        print i    return ""def Quit():    print "Good bye !"    return exit(0)Select_List = {    "1": Login,    "2": Create,    "3": Show,    "4": Quit}while True:    print """                        welcome to xxx gamePlease select what you want to do !    1: Login    2: Create New Player    3: Show All Players    4: Quit"""    sec = raw_input("Please input 1-4 : ")    if sec in Select_List.keys():        print Select_List[sec]()    else:        print "Please input 1,2,3,4 !"

该模拟游戏脚本的效果图如下所示:
创建玩家效果图:

这里写图片描述

由图可以看出,可以创建自己想要的玩家名称,但是如果该名称存在,会显示该玩家已经存在。

显示所有玩家效果图:

这里写图片描述

选择显示所有玩家后,可以将所有玩家名称显示出来。

玩家登陆效果图:

这里写图片描述

玩家通过玩家名登陆后,会显示该玩家的信息。

玩家登陆后游戏选择效果图:

这里写图片描述

这里写图片描述

玩家可以选择想要进入的地图或者选择退出,执行时以地图1”mojiajiguandao”为例。

退出游戏效果图:

这里写图片描述

选择退出游戏后,显示”Good bye !”并退出。
因为是简单模拟一个游戏,所以可能会存在一些BUG或者某些不合理的地方,敬请见谅!