使基于文本的游戏产生怪物时出错

用户名

我正在和朋友一起参加编程课的文字游戏,我们避免使用面向对象的东西,所以请避免使用这些建议。要使用该程序,我通常会键入“ help”,然后当我尝试使用这些命令时,我可以朝任何方向移动,如“向右”,“向左”,“向下”和“向上”。这是在我添加了spawnMonster命令部分之后发生的

#Connor and Griffin's text based adventure
import os
import random

############################
####---Variable setup---####




#########ADD COMMAND VARIATION############

#########ADD ITEM LISTS#################




commands = 1 #for debuging only please
maxHealth = 100 #Default begin health
health = 100 #Current health
mana = 0 #THERES NO MAGIC
mapS = [5, 5]
objects = {}
color = "0f"
output = "" #this is where whats happening is told to you
level = 1



canMove = 1

playerSym = "P"


# for activeQuests remember that if the value is 0 its not completed and if its 1 its completed
activeQuests = {"Journey To Riverwood": 0}
# Add new quest names above^
commandList = {"help", "legend", "color", "show inv", "quests", "console", "up", "left", "right", "down", "clear", "map"}
#Default inventory
inv = {"apple(s)":2, "shortsword":1, "gold":50,"cloth shirt":1,"pair of cloth pants":1,"pair of shoes":1}
clearedSpaces = []

##### "Name":baseDMG #####
monsters = {"Goblin":1, "Troll":3, "Bear":2, "Giant Spider": 1, "Bandit":1, "Goblin Chief":3}

###########################
###########################



#####   Name:lv:monsterSpawnRate    #####
zones = {"Forest":[1,90]}



#######################
#---Quest Log Stuff---#


def checkQuest():
    for questn in activeQuests:
        print("\n------", questn, "------")

        if activeQuests[questn] == 0:
            print("\nNot  Complete")

        else:

            print("\nComplete")

            ######Description for quests######
        if questn == "Journey To Riverwood":
            print("""
Welcome to Connor and Griffins excellent adventure!
try out some of the commands like; help, quests,
color, inv or show inv. now using your new found
commands move to the city of riverwood.\n\n""")


#########################
#########################


############################
###---Scenes/Functions---###


def mapSize(x, y):
    global mapS
    mapS = [x, y]

####Ads point to map
def addObject(name, x, y, symbol):
    objects[name] = [x, y, symbol]
    legend[symbol] = name

#### Clears some variables
def roomStart():
    global objects
    objects = {}
    global legend
    legend = {"░":"Unknown area"}
    global roomName
    roomName = "BLANK"

def newArea():
    for area in zones:
        global spawnChance
        spawnChance = zones[area][1]


def spawnMonster():
    enemy, DMG = random.choice(monsters)
    return enemy


def moveToNewSpace():
    rand = random.randint(1,100)
    if rand <= spawnChance:
        global spawnedMonster
        spawnMonster()

###Move player
def changePos(name, newx, newy):
    objects[name][0] += newx
    objects[name][1] += newy
    global clearedSpaces
    clearedSpaces.append([objects[name][0],objects[name][1]])
    moveToNewSpace()




###First room
def roomBegin():
    roomStart()
    mapSize(15,10)
    global roomName
    roomName = "Forest"
    newArea()
    addObject("Riverwood",10,5,"R")
    addObject("Griffin's House",2,2,"G")
    addObject("Player",2,3,playerSym) #######Remember to make a "ChangePos" command to change the pos of the player when they move#######
    clearedSpaces.append([2,3])




    ################### MAPPING HERE ##################
def makeMap():
    print("\n------"+roomName+"------")
    for y in range(mapS[1]):
        line = ""
        numy = y+1
        for x in range(mapS[0]):
            numx = x + 1
            for place in objects:
                if objects[place][:2] == [numx, numy]:
                    line += objects[place][2] 
                    break
            else:

                if [numx, numy] in clearedSpaces:
                    line += " "
                else:
                    line += "░"

        print(line)
    print("\n----Legend----\n")
    for thing in legend:
        print(thing + " - " + legend[thing])



############################
############################


#######################
###--- MAIN LOOP ---###

roomBegin()

while 1 == 1:
    makeMap()
    print("\n\n" + output + "\n\n")
    print("\n\nHealth is at ",health,"/",maxHealth)
    command = input("Enter action: ")

    if command.lower() == "quests":
        os.system("cls")
        checkQuest()

    elif command.lower() == "legend":
        os.system("cls")
        print("\n----Legend----\n")
        for thing in legend:
            print(thing + " - " + legend[thing])


    elif command.lower() == "help":
        os.system("cls")
        print("\n\n------HelpMenu------\n")
        for comd in commandList:
            print(comd)

    elif command.lower() == "color":
        newc = input("new color: ")
        os.system("color 0" + newc)
        os.system("cls")

    elif command.lower() == "show inv" or command.lower() == "inv" or command.lower() == "inventory":
        os.system("cls")
        print("\n------Inventory------\n")
        for item in inv:
            print(" ", inv[item]," ", item)

    elif command.lower() == "console":
        if commands == 1:
            consolecmd = input("Enter a command: ")
            os.system(consolecmd)
        else:
            print("Sorry, you dont have permition to use that command.")

    elif command.lower() == "up":
        if canMove == 1:
            os.system("cls")
            changePos("Player", 0,-1)

        else:
            os.system("cls")
            output += "\nCant move that way right now!"

    elif command.lower() == "down":
        if canMove == 1:
            os.system("cls")
            changePos("Player", 0,1)
        else:
            os.system("cls")
            output += "\nCant move that way right now!"


    elif command.lower() == "left":
        if canMove == 1:
            os.system("cls")
            changePos("Player", -1,0)
        else:
            os.system("cls")
            output += "\nCant move that way right now!"

    elif command.lower() == "right":
        if canMove == 1:
            os.system("cls")
            output = "There are some trees here, and a small pond"
            changePos("Player", 1,0)
        else:
            os.system("cls")
            output += "\nCant move that way right now!"

    elif command.lower() == "clear":
        os.system("cls")

    else:
        os.system("cls")
        print("Previous attempt was an invalid command, try again.")


#######END MAIN#######
######################
阿巴内特

random.choice函数需要一个序列

在您的代码的先前版本中(或者也许是您同班同学的代码在使用相似代码时遇到相似但不相同的问题?),您将怪物存储在列表中,例如[("Goblin", 1), ("Troll", 3), …]对于该代码,random.choice因为列表是序列,所以有效。

但是现在你有了一个命令。字典不是序列。

您只需编写即可获得字典中所有键的列表list(d)因此,您可以这样做:

return random.choice(list(monsters))

但是,monsters从我能告诉的内容来看您实际上并没有从中获得任何收益,所以为什么不首先使用列表呢?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

发布游戏时出错

来自分类Dev

如何使我的怪物在游戏中随机移动

来自分类Dev

在为基于文本的RPG游戏制作清单时遇到困难

来自分类Dev

创建基于文本的游戏的更好方法

来自分类Dev

在基于文本的游戏中添加年龄幻觉

来自分类Dev

基于文本的游戏非分支if语句

来自分类Dev

创建基于文本的游戏的更好方法

来自分类Dev

用于基于文本的游戏的PHP开关

来自分类Dev

基于文本的游戏中的C ++输入

来自分类Dev

基于文本的游戏代码不起作用?

来自分类Dev

创建基于安卓文本的游戏

来自分类Dev

基于Python的基于文本的游戏未调用正确的房间

来自分类Dev

运行Screeps游戏脚本时出错

来自分类Dev

C ++创建Pong游戏时出错

来自分类Dev

lua metatable帮助(在游戏中生成怪物)

来自分类Dev

在基于Java文本的游戏中解析命令的最佳方法

来自分类Dev

为基于Java文本的游戏制作控制台

来自分类Dev

在python中基于文本的游戏苦苦挣扎

来自分类Dev

为基于Java文本的游戏制作控制台

来自分类Dev

基于文本的冒险游戏循环到上一行?

来自分类Dev

我如何确定这个基于文本的游戏的赢家?

来自分类Dev

在随机页面访问/刷新时产生文本

来自分类Dev

当文本为“> =”时,IsNullEmptyOrWhiteSpace产生True

来自分类Dev

从主菜单过渡到游戏pygame时出错

来自分类Dev

使用playscape SDK 1.11构建游戏时出错

来自分类Dev

产生多个boundingBoxes“ System.ArgumentOutOfRangeException”时出错

来自分类Dev

在编译时产生招摇JSON文件基于springfox项目

来自分类Dev

在eclipse中添加编辑文本时出错

来自分类Dev

对文本值使用SUM时出错

Related 相关文章

热门标签

归档