user3666755
user3666755

Reputation: 45

text game can't get charactor to move

I'm trying to make a text based adventure game and I can't get the movement working I know there is a lot of stuff that's unnecessary but this is what I have.

When you run the program type "help" and the "map" the problem is that I thought I got the movement working by using "move" and then "right"/"left"/"up"/"down" but it does nothing, no errors or anything it just does nothing to the player position. I can't figure out why.

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

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


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"


canMoveForward = 1
canMoveBack = 1
canMoveLeft = 1
canMoveRight = 1
ableToMove = 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", "move", "go", "clear", "map"}
#Default inventory
inv = {"apple(s)":2, "shortsword":1, "gold":50,"cloth shirt":1,"pair of cloth pants":1,"pair of shoes":1}


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



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


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

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

        else:

            print("\nComplete")

            ######Description for quests######
        if questn == "Journey To Riverwood":
            print("""
Stories are stories that do things to make games
seem better when they really aren't it just adds
to some of the effect, hmm I need more stuf to
put in this description to make it look longer
without actually writing out a story just yet.\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"}

###Move player  ##########Change##########
def changePos(name, newx, newy):
    objects[name] += [newx,newy]


###First room
def roomBegin():
    roomStart()
    mapSize(15,10)
    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#######





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


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

roomBegin()

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

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

    if 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)

    elif command.lower() == "show inv":
        os.system("cls")
        print("------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.")

    ######MAPPING SYSTEM HERE#######
    elif command.lower() == "map":
        os.system("cls")
        print("\n")
        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:
                    line += "░"      
            print(line)
        print("\n----Legend----\n")
        for thing in legend:
            print(thing + " - " + legend[thing])



    elif command.lower() == "move" or command.lower() == "go":
        if ableToMove == 1:
            direction = input("Which way? forward, left, right, back? ")
            if direction.lower() == "forward":
                if canMoveForward == 1:
                    moveForward = 1
                    print("You have moved forward")
                    changePos("Player", 0,1)
                else:
                    print("Cant move that way!")

            elif direction.lower() == "back":
                if canMoveBack == 1:
                    moveBack = 1
                    print("You have moved back")
                    changePos("Player", 0,-1)
                else:
                    print("Cant move that way!")


            elif direction.lower() == "left":
                if canMoveLeft == 1:
                    moveLeft = 1
                    print("You have moved left")
                    changePos("Player", -1,0)
                else:
                    print("Cant move that way!")

            elif direction.lower() == "right":
                if canMoveRight == 1:
                    moveRight = 1
                    print("You have moved right")
                    changePos("Player", 1,0)
                else:
                    print("Cant move that way!")


            else:
                print("Whoops! Thats invalid. ")

        else:
            print("You cant move!")


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



    else:
        print("Thats an invalid command, try again.")


#######END MAIN#######
######################

Upvotes: 0

Views: 67

Answers (1)

Kevin
Kevin

Reputation: 76234

def changePos(name, newx, newy):
    objects[name] += [newx,newy]

Here's your problem. You're adding relative coordinates to the object, but this doesn't modify the existing coordinates. For example, [2,4,"P"] + [1,0] doesn't equal [3,4,"P"], it equals [2,4,"P",1,0]. If you want to add the numbers up, you'll have to do so explicitly.

def changePos(name, newx, newy):
    objects[name][0] += newx
    objects[name][1] += newy

Upvotes: 1

Related Questions