Python (civ4 scripting language) tutorial thread

Korab - the Python programming language lacks a GOTO command, simply because they are a complete mess when used.

What you need to do is put a loop from line 33 until the second last line, that continues to run until the variable loop == 0. Indent everything in that area one more level. I'll post how I did it:

Code:
#TEXT ADVENTURE GAME

#the menu function:
def menu(list, question):
    for entry in list:
        print 1 + list.index(entry),
        print ") " + entry

    return input(question) - 1

#Give the computer some basic information about the room:
items = ["pot plant","painting","vase","lampshade","shoe",]
actions =["drop the key","open the door"]

#The key is in the vase (or entry number 2 in the list above):
keylocation = 2

#You haven't found the key:
keyfound = 0

loop = 1

#Give some introductory text:
print "Last night you went to sleep in the comfort of your own home."
print "Now, you find yourself locked in a room. You don't know how"
print "you got there, or what time it is. In the room you can see"
print len(items), "things:"
for x in items:
    print x
print ""
print "The door is locked. Could there be a key somewhere?"
#Get your menu working, and the program running until you find the key:
###########
# LOOK HERE #
###########
# Notice the extra indents, and the while loop
while loop != 0:
    while loop == 1:
        choice = menu(items,"What do you want to inspect? ")
        if choice == 0:
            if choice == keylocation:
                print "You found a small key in the pot plant."
                print ""
                keyfound = 1
            else:
                print "You found nothing in the pot plant."
                print ""
        elif choice == 1:
            if choice == keylocation:
                print "You found a small key behind the painting."
                print ""
                keyfound = 1
            else:
                print "You found nothing behind the painting."
                print ""
        elif choice == 2:
            if choice == keylocation:
                print "You found a small key in the vase."
                print ""
                keyfound = 1
            else:
                print "You found nothing in the vase."
                print ""
        elif choice == 3:
            if choice == keylocation:
                print "You found a small key in the lampshade."
                print ""
                keyfound = 1
            else:
                print "You found nothing in the lampshade."
                print ""
        elif choice == 4:
            if choice == keylocation:
                print "You found a small key in the shoe."
                print ""
                keyfound = 1
            else:
                print "You found nothing in the shoe."
                print ""
    
        if keyfound == 1:
            loop = 2
            print "you have the key"
            print "There are", len(actions), "things you can do:"
            print ""
    
    while loop == 2:
        choice = menu(actions,"What do you want to do? ")
        if choice == 0:
            keyfound = 0
            loop = 1
            print "You dropped the key"
            print ""
        elif choice == 1:
           loop = 0
           print "You put the key in and the door unlocks"
           print ""
        
print "Light floods into the room as you open the door to your freedom."
 
Thatnk u GBM. That was exactly what i needed. So really what you said was while the loop is not 0. Why does that work and not the way i did it?
 
JG99_Korab said:
Thatnk u GBM. That was exactly what i needed. So really what you said was while the loop is not 0. Why does that work and not the way i did it?
The reason why mine worked is because a loop can only go back to the line where it started - no earlier. The loop you wanted to go back to, existed before the beginning of the loop that the program was currently in.

To solve this, I simply put both loops inside another loop. If the 'drop key/open door' loop ended and the door hadn't been opened yet, the program goes back to the 'find key' loop.

Don't worry if this seems complex - the more you play around with code, the easier it becomes to work with it. I've gotten to the point where two loops run at the same time, and send messages to each other. While this may seem complex for the people who are just starting out, in reality it is just a combination of the things you already know.
 
what about making a limit on choices? e.g. if you get three wrong. The game is over.
 
you could just define a variable that equals 0, and have it add 1 to it each time you got it wrong. Then, when the variable = 3, the game ends with a message.

ex.

x = 0

...
...

elif choice == 2:
...
x = x+1
elif choice == 3:
...
x = x+1
...
...
if x == 3:
loop1 = 0
loop2 = 0
print "..."
 
thescaryworker said:
you could just define a variable that equals 0, and have it add 1 to it each time you got it wrong. Then, when the variable = 3, the game ends with a message.

ex.

x = 0

...
...

elif choice == 2:
...
x = x+1
elif choice == 3:
...
x = x+1
...
...
if x == 3:
loop1 = 0
loop2 = 0
print "..."
hey scary. Will you look at this file and put in that?
 

Attachments

FYI, There is a free plugin for the Free IDE Eclipse that you can use for Python development.Eclipse Pydev. I like free. If you've never used Eclipse before it is an excellent tool, and I expect no less of the Pydev plugin, however I haven't tried it yet.
 
JG99_Korab said:
How can i make a random keylocation?
This would be a bit more difficult than the above example. It would be a good idea to store the key location in a variable and randomize it that way, rather than 'hard-coding' its location by giving a certain text output depending on which choice the player makes.
 
I think I will start to learn Python actually, maybe. I havnt decided yet, but I really want to be modding when Civ 4 comes out. And this will really help if I start.

I have some experience in using Java, C++, PHP, SQL and I know some about XML but never used it. Even learned some advanced stuff in C++ like binary trees and using pointers and stuff like that, but I've forgotten most of that now. I managed to create Tech Calc with my knowledge atleast. (alot of dirty coding in that program :p - barely knew what I was doing, but still got it to work :D)

But what I like to do most though is graphics (3D mostly) and game design, and programming bored me out.

Anyways, I'll post again if I start the lessons and need help or anything. ^^
 
And btw, anyone know of a good free editor except notepad for python? Prefarably with text coloring, or whatever that is called.
 
If you get the Windows version of Python (and I assume you will if you are thinking of modding Civ4), the PythonWin IDE that comes with it is excellent with syntax highlighting and debugging built in. The IDLE IDE that comes with all versions of Python is also good, just not quite as good as PythonWin.
 
Grey Fox said:
And btw, anyone know of a good free editor except notepad for python? Prefarably with text coloring, or whatever that is called.

text coloring ==> syntax highlighting

Eclipse with Pydev plugin.
 
Hi there !
I'm very interested in this thread.
I have some experience in ADA, C, and assembler (and a few other visual things like C or Basic in a remote past)
That used to be my job to write some kind of industrial electronic cards board drivers, real time execution. I'm a little bit rusted.

I'm at lesson 7 already. I havent' read the following post yet.
But i'm rather happy with my first try.
this game tries to find animal through(very simple) questions.
but what I'm proud of is it has a learning curve.
you won't fool him twice with the same animal !

it certainly isn't perfect. it lacks random functions,and I/O would be useful.
I guess next lesson will allow me to improve my baby.
I also found that booleans worked all right. good.

-- pardon my english, I'm no english speaker --

here is my code.

--------------------
Code:
#a guessing game

noise = {'meow':'cat','wouf':'dog'}
one_more_turn = True

def ask_yes_no(string):
    good_answers_list = ('y','n')
    answer = raw_input(string + "(y/n)")
    while not ( answer in good_answers_list):
        print "hello ! I want one of ",
        print  good_answers_list
        answer = raw_input(string + "(y/n)")
    return answer == 'y'
    
while one_more_turn:    

    listnoise = ['dummy'] + noise.keys()
    found = False

    print "think to an animal..."
    while (not found) and  len(listnoise) <> 1:       
        del listnoise[0]
        found = ask_yes_no("is this animal noise " + listnoise[0]+ " ? ")
    print "that's definitively a " + noise[listnoise[0]]

    if ask_yes_no("am i right with my "+ noise[listnoise[0]] + " ?"):
        print " I knew it ! computer rules !"
    else:
        valid = False
        while not valid:
            animal = raw_input("what was your animal ?")
            the_noise = raw_input("what is its noise ?")
            if noise.has_key(the_noise):
                print "sorry bud, this noise is owned by " + noise[the_noise]
            else:
                print "duly noted. I won't make that mistake again"
                noise[the_noise] = animal
                valid = True

    one_more_turn = ask_yes_no("one more try ? I'm getting better each time !")
     
print "thank you for playing"

edited for tabulations. thanks Trip
 
Old_Lion said:
uh ho: looks like tabulation disapeared. how should I do ?
If you put the [code] tag in front of and the [/code] tag after your it the forum will keep your tabs.
 
Back
Top Bottom