Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help!
#1
Hi, I'm working on a game for a school project, and I've been trying to solve this problem for over a week now
(I'm working in Python 2.7, and I'm kind of new to Python, so...)
 
global world
global shovel
global pickaxe
global detector
global posx
global posy
world=[["a","b"],["c","d"],["e","f"]]
shovel=0
pickaxe=0
detector=0
posx=0
posy=0

def Menu_Screen():
    print "MENU" #Its in another program (i have to copy and paste this into that, but it works perfectly from there)

def show_loc():
    global posx
    global posy
    world_map=[["_","_"],["_","_"],["_","_"]]
    world_map[2-(posy)][posx]="X"
    for i in world_map:
        for j in i:
            print j,'\t',
        print

def move_place():
    global posx
    global posy
    global tempx
    global tempy
    tempx=posx
    tempy=posy
    show_loc()
    while True:
        m=raw_input("Which direction do you want to go? (N/S/E/W): ")
        while (posx==1 or posx==0) and (posy<=2 and posy>=0):
            if m.upper()=="E":
                tempx=tempx+1
            elif m.upper()=="W":
                tempx=tempx-1
            elif m.upper()=="N":
                tempy=tempy+1
            elif m.upper()=="S":
                tempy=tempy-1
            else:
                print "Invalid Input! Try again!"
            m=""
            break
        if (tempx==1 or tempx==0) and (tempy==0 or tempy==1 or tempy==2):
            posx=tempx
            posy=tempy
            show_loc()
        else:
            print "You can't move that way as it is blocked by a wall!"
            tempx=posx
            tempy=posy

def Game_TRAPPED():
    global posx
    global posy
    global pickaxe
    global shovel
    global detector
    global world
    move_place()
    p=world[posy][posx]
    if p=="a":
        if detector==0:
            print "You found a metal detector!"
            print "You've equipped the detector. Now to find something..."
            detector=detector+1
        else:
            move_place()
    elif p=="c":
        if shovel==0 and detector==1:
            print "Your metal detector is beeping!"
            print "It looks like something is under the big rock in front of you"
            print "You push it as hard as you can..."
            print "You found a shovel!"
            print "Maybe you can find something else using this..."
            shovel=shovel+1
        else:
            move_place()
    elif p=="d":
        if pickaxe==0 and (detector==1 and shovel==1):
            print "Your detector is beeping again!"
            print "But there's nothing there..."
            print "Maybe something is buried in this spot"
            print "You use the shovel and dig..."
            print "You found a pickaxe!"
            print "If only you knew the right place to strike..."
            pickaxe=pickaxe+1
        else:
            move_place()
    elif p=="f":
        if pickaxe==0:
            print "The wall here seems kind of weak..."
            print "Mabye you could break this down and escape, but the rocks are too hard..."
        else:
            print "You used the pickaxe on the wall and escaped"
            print "YOU WON"
            print ""'\n'
            Menu_Screen()

Game_TRAPPED()
The game works fine except the player cannot "retrieve" any objects placed on the map.
So even if i travel around the whole map, I can't "equip" anything
Please help!
Reply
#2
line 66 you call a function with an infinite while loop and never leave it.
Recommended Tutorials:
Reply
#3
(Nov-04-2016, 09:32 PM)metulburr Wrote: line 66 you call a function with an infinite while loop and never leave it.

Thank you so much! That was the problem the whole time! Big Grin
I moved the code from move_place() function into the main game function and I added a variable so that when the player escapes, it stops and doesn't ask for which direction to go
Thank you so much again! Smile
Reply
#4
You may be happy with your results now, but you should know that the way you are using the global keyword is really bad.  Basically until you are a more mature programmer you should think of the global keyword as off-limits.  Information should be given to functions via argument passing and gotten from functions via return values.  As it stands you might as well not be using functions if you are just going to make everything global.
Reply
#5
For convenience, you can put print() functions in several places in the code to get some output. In that way, it should be easier to see where the problem is, my padawan.  Cool
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(Nov-05-2016, 06:46 AM)Mekire Wrote: You may be happy with your results now, but you should know that the way you are using the global keyword is really bad.  Basically until you are a more mature programmer you should think of the global keyword as off-limits.  Information should be given to functions via argument passing and gotten from functions via return values.  As it stands you might as well not be using functions if you are just going to make everything global.

Thanks for the advice! Smile
I'm still in the baby steps, but I shall keep this in mind for future programs  Thumbs Up
Reply
#7
"global" and "eval" are big bright red flags that there's almost always a better way to do something. Sure, they get the job done, but sooner or later you'll hate yourself for using it. For example, if something goes wrong, you'd have no idea where to start looking for why it's not working right, because everything is modifying the same variable(s). Once you start writing things so they work the same regardless of external state, it becomes much easier to make sure your code does the right things at the right times, which makes it easier to write new things instead of fixing old things, so you write a lot more code faster.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020