Python (civ4 scripting language) tutorial thread

started to read this thread yesterday night and downloaded python. went through lesson 1-5.

I definitely would be interested to mod this time round. Will keep on reading this thread. A great idea with a great teacher here :goodjob:
 
To celebrate the stickying of this thread, the site is going through another redesign. This time, the pages will be generated from a script, so to make it easier to add extra lessons.

Why is this important? I may add lessons on certain python modules that may be useful to programming in civIV. Plus, when the game is released, I can add lessons that are more specific to the game inline with other lessons.

Can't wait!
 
What do you plan to teach next ?

I've been through your text adventure game,
and I found I have some things to say or to add.

I think the structure deserves to be a little bit different :
container being a class, (add_item,remove_item,list_items + description + ...)
room another one inheriting the container class, (same + list_of_exits)
and items being another class.
there simply

I could post to discuss my approach, and how I chose to structure objects and classes.

but I'm not familiar with modding. Is it mainly about modifying parameters,
or is there some room for actual programmation ?
 
Is it too late to join?

I'm a programmer and have quite a bit of experience with various technologies, but no contact with Python yet. I'll be interested to learn and help out other people to the best of my abilities.

--start shameless plug--
Also, for those interested, I have started a thread for creating an AI mod here.
--end shameless plug--
 
Brain said:
Is it too late to join?
Never! I initially had the lessons structured so that I could see how people progressed through them, and where they got stuck.

Now that the lessons have slowed down, people can do them at their own pace, whenever they want to.
Old_Lion said:
What do you plan to teach next ?
Whatever is asked of me. I do plan on writing further lessons on classes, because classes are the bread, butter, and dinner plate of python programming. Beyond that, user asks, I write.
 
well, I'm hooked

I found about default value for function arguments, which I like.

example :
Code:
#a description of those items you'd find in this great game text
class item:
      def __init__(name,number=1,plural_name=None):
            self.name=name
            self.number=number
            if plural_name==None:
                  self.plural_name=name
            else:
                  self.plural_name=plural_name            
      description = "item yet to be descibed

# an new item : the stone
stone = item("stone")

#an other one
paper = item("piece of paper",4,"pieces of paper")



perhaps a little chapter about "standart" libraries would be useful
(handling some basic graphics and pop-up windows for example ?
is it provided ? I can't check from here, and if someone already went through the doc about it...)
 
Hi! Found this thread, downloaded Python. Completed up to Lesson 5.

I have one question that I didn't see mentioned in the next few pages from there, but why does the function div(a,b) return a result that's really INT(a/b). I got no decimals from division when I tried it.

I've had a little bit of basic and pascal 20 years ago, and I can copy and paste javascript. :)

Thanks for the lessons. Bookmarking this thread.

Edit 1:

Ok, I figured out that if you put the decimal point in your inputs for the div function, it does floating point math instead of the default integer math. So that 3 / 2 = 1, where as 3. / 2. = 1.5

So, I've rewritten the div function. :)

Code:
#this divides two numbers given
def div(a,b):
    # exception for dividing by zero
    if b == 0:
        print "cannot divide by zero"
    #converts integer inputs to floating point
    else:
        a = a * 1.0 
        b = b * 1.0
        print a, "/", b, "=", a / b

Edit 2:

Ok, after reading thru lessons 8 Classes, 9 Modules, and 10 Pickles. My head is going to explode. I think what we need are some really simple game related examples and exercises. Thanks for your continued support.
 
homegrown said:
Ok, I figured out that if you put the decimal point in your inputs for the div function, it does floating point math instead of the default integer math. So that 3 / 2 = 1, where as 3. / 2. = 1.5
Seems like a handy thing. I always thought you had to have something after the decimal point, but it seems not.

homegrown said:
Ok, after reading thru lessons 8 Classes, 9 Modules, and 10 Pickles. My head is going to explode. I think what we need are some really simple game related examples and exercises. Thanks for your continued support.
It's now #1 on my next lesson list!
 
Huge Civ fan here, I'm 16, a game/computer programmer hopeful, and am dying to get my hands on civ4! very late joiner! Have done extensive modding on Red Alert 1 and 2, making big mods but haven't published them online. No programming experience.
 
HEY GB. I was a t your website. And i was wondering if you could change your background color. I don't know if i am the only one. But that white really strains the eyes. Maybe (off-white) would be better. If it is no trouble. Thank you GB
Korey
 
Hi GB. I am doing lesson 8 on classes now. I have got to the part were it tells you to run the program then start adding code. When i run a program it goes to python shell, but then the program ends, so it won't let me add anything else. Can you try getting the new python version and testing it out? it doesn't overwrite the previous one
 
JG99_Korab said:
HEY GB. I was a t your website. And i was wondering if you could change your background color. I don't know if i am the only one. But that white really strains the eyes. Maybe (off-white) would be better. If it is no trouble. Thank you GB
Korey
I understand your problem - when I got a new LCD monitor, the brightness was excellent, but often headache-inducing. I will fix it in the redesign.

I'll probably release a preview soon, but the current site is not worth changing. The reason is that in the new design, all the pages are generated from a script, so it is really easy to make site-wide changes. Currently, there is only handwritten html.

Maybe I can use a more civilization-ish colour scheme. I want to keep images to a minimum, though.
 
Well, I've been self guiding and I came up with a game related (although, not Civ specifically) exercise that I've been working with. Basically, the game has a metalworking component, with a number of different forges, and each can make different items. I'm creating a calculator that asks the user what project they want to make, how many they want to make, and how many forges they have access to, then it returns how much of what kind of metal, how much charcoal they will consume, and the total cooling time for the whole project.

Here's what I have so far.

Gonna have to attach a text file. Look for it. :)

First, the admin function really does work, but it's not persistant across sections. I'm guessing I'm gonna pickle the new dictionary if it's changed then somehow tell the program to writeback that dictionary when confirmed, but I'm not there yet.

Also, the program part, it occurred to me that I might be better off with several small functions, getinput(), calculate(), and printoutput() might clean everything up some, but I run into problems with the class objects because they end up being created in one function but need to be used in two others.

So, I guess I finally got brain around lessons 8 Classes and 9 Importing Modules (notice the import math and the math.ceil() call in the calculations area? :) But there are still some problems I'm trying to work out.

By the way, this little made up homework exercise, has been the best experience I've had, if only because I do have a couple friends that have been making me figure stuff out on my own, but are quick with the encouragement and praise when I have a light bulb moment. Maybe a neat Civ exercise would be a Culture Calculator. If I build this, this, and this, how long until the City expands to Size 1, 2, 3, 4, etc. In case anyone needed an idea of something to do. :)

Oh, also, now I know why programmers like :beer:
 

Attachments

Homegrown - that's some really good work you've got there.

attached are the python files for my text adventure game - you could probably borrow some concepts from it.

While your program probably doesn't need multithreading and queues, as is used in the text adventure (a.k.a. pytex) game, it would be advantageous to use classes more effectively, as opposed to lots of dictionaries.

The way the pytex game works, is that there is one class that is accessable by all players - the world class. This class contains a dictionary full of classes refering to items in the game.

When the client thread (that is, the person playing the game) wants to access some data about the game world from the world class, it sends a request for information, and the world class, when it gets around to it, processess the request and returns the resulting data.

In your project, I would cut the dictionaries a_project and project, and simply input the data directly into class instances that are inside a dictionary instead. E.g. change the __init__ function of Project to this:
Code:
class Project:

    def __init__(self, item,bldg,upgrade,metaltype,metalqty,waxqty,cool): 

        self.item = item
        self.bldg = bldg
        # etc etc etc
then, create a dictionary of all the projects you could have:
Code:
projects = {}
projects['iron bar'] = Project(0,0,'iron',2,1,0,15)
projects['gold wire'] = Project(2,0,'gold',1,10,0,10)
projects['steel sheeting'] = Project(2,0,'steel',2,6,0,10)
# etc etc etc
Do the same for a_charcoal. This might make the program longer, but it makes it easier to understand.

Here is my text adventure game below. Have both files in the same folder, and run pytexgame.py .

In pytexgame, all the items (which are class instances) in the game world are put into a dictionary called playerroom. At the bottom of pytexgame, we create a game world with all the items we have defined in it, then we create a player that plays a game in this world.

For one of my next exercises, I'll write some documentation for pytex, and get you people to do whatever you want with it. It still has some work to go, so it might be later on in the piece.
 

Attachments

Hello to all, hello Gingerbread Man.
First I want to say that I have been with you and this thread when I first discovered it a short time after it has been announced that cIV would offer Python scripting. I read (almost) everything. Very good work! I'm happy to see this interested community and I value your great work and your time spent here :goodjob: .
I acually bought a book the instance I heard about it :D . I came in contact with Python the first time through "Blender" a freeware-"open source"-abandoned-type 3D modeler. But I didn't do any scripting back then.

I have gathered some knowlegde in (almost chronoligical order :) ) Basic, Pascal/Turbo Pascal, HTML, C, C++, Java, C#, XML, PHP and Python (actually still working on that ... hm, more actually on ALL of them :D except the very old ones). No really great projects so far, but I am programming since I was 10 (or so) ... and as you can see: coming around a lot :) .

I'd like to a tiny piece of information to your post on classes. I became very confused when I got in contact with classes in C++ but this explanation helped me a lot to understand what they really do (this is not intended to correcting you but to make classes a little more transparent to the community):

Think of classes as your BLUEPRINTS for later manufactured objects! In your class definition you describe how you would build i. e. a car ... but since then there is no real car, right(only the pice of paper with your blueprint on it)? So you have to build a car according to this BP. This is done by making an instance of it.
In Python: myCar = blueprint_of_a_car()
Now the little "data elves" in your computer actually build a car named "myCar". Now you would be able to drive that car or accellerate it and so an (ever drove a blueprint :crazyeye: !?).

This brings us to inheritance and polymorphism.
If you would like to build a new car with special tires, seats or a different motor you wouldn't design a whole new car, now would you? Instead you would just change your motor or your tires and leave the rest as it is.
This is done by creating a new class (or blueprint) which inherits all the attributes and methods from your original car class (copy all information of your original blueprint to another) and then overrides the information i. e. of the type of motor what should be in the car (erase the data of your motor on your copied blueprint and specify another one).

I am working right now on an civ related interface mod. Remember that you had to explicitly change single leaderheads in your civ3 foreign advisor screen if too many civs were in the game!? I'm working on a demo solution for that problem in Python where the leaderheads will organize themselves on a circle and scale down when necessary.
I want to try that using Tkinter objects, canvas, simple shapes (not even real images to scale ... keeping it as simple as possible). After all they said the INTERFACE would be modable.
So I WILL come back (for support :D ) and I am also planing on posting the solution here on cfc.

(See second part)
 
How do I pick a random variable out of something like C1-C52. Could I do something like Cvariblename or can I make a list like cards = [C1,C2,C3...,C51,C52] and then call a random card using cards[random] where random is a random number?

Also what is the function (if there is) that generates a random number?
 
Back
Top Bottom