Site hosted by Angelfire.com: Build your free website today!

Annotated code

 

For those of you looking to download and edit my source code (see program downloads), here is some quick help for you—I know just how complicated it can get, trust me!  For the most part though, this is a basic introduction to the idea behind programming.

 

There are also a couple examples for those of you using Visual Basic to write programs like the Feigenbaum graph utility.

 

 

You can jump to a certain segment, or scroll down to view:

Gamemaker scripts == Style Conventions == Gamemaker Colors   

Comments on other languages == Graphing using odd variables

 

 

 

 

 

For those of you who are editing Gamemaker files (they end in .GML), the syntax rules are very similar to Java or Pascal.

 

A program basically consists of:

 

{ ßA start block

 

Variable=12; ßVariables (notice the semicolon at the end of each line of code, you can’t forget your end characters)

 

Variable=variable * expression ßSomething to modify the variables

 

} ßAn end block

 

 

 

Given that basic structure, you pretty much understand the flow of information within a program. At this year’s JSHS, I was trying to explain the idea of programming to a seventh-grader, and it seemed like the idea of writing his own programs was such a complicated and alien prospect that we were speaking different languages.

 

Hence this site.

 

For more help on Gamemaker scripts, the program has a built-in help section that’s fairly useful, and online help is available.

 

Click here to return to the top

 

 

 

Below is a section of code from the plant reproduction script.

 

This segment is meant to help you see my stylistic preferences. When writing your programs, it’s important to keep the same style all through your code, so that you don’t get confused.

 

{

i = random(global.mutations);

if (i <= 10.0){will_mute = true}; ßWhen only dealing with a one-line if statement, I like to keep it all on one line. Except for Visual Basic, most programming languages don’t care about carriage returns (the blank line made when you press [Enter]), but when you look at your code, they’re important.

 

p_genome = self.genome;

p_reptime = self.reptime;

p_health = self.health;

p_nutval = self.nutval;

p_midage = self.midage;

p_maxage = self.maxage;

 

if (will_mute = false  

      { ßThis is an example of a multiline statement.

      d_genome = p_genome;

      d_reptime = p_reptime;

      d_health = 1.0;

      d_nutval = p_nutval * (p_health * 2);

      d_midage = p_midage;

      d_maxage = p_maxage;

      } ßIf you look at the first If statement in this segment, you’ll see that it also has start and stop block characters (the squiggly lines), but that they’re on the same line.  This has the same effect, but it’s easier to read.

 

if (will_mute = true and random(global.mutations * 5) <= 10.0)

      {    

      p = round(random(3) + 1);

            if (p = 1) {d_genome = 'plantA'};

            if (p = 2) {d_genome = 'plantB'};

            if (p = 3) {d_genome = 'plantC'};

            if (p = 4) {d_genome = 'plantD'};

 

Look at the lines above, do you notice how they’re indented? Just like carriage returns, indents are a great way to make your code easier to read. When you enter a new statement, indent it to set it off from surrounding code.

 

      file_open_read(d_genome+'.gen');

      file_readln();

      d_reptime = file_read_real();

      file_readln();

      d_nutval = file_read_real();

      file_readln();

      d_midage = file_read_real();

      file_readln();

      d_maxage = file_read_real();

      file_close();

      d_health = 1.0;

      }

}

 

Those are just a few basic examples of how you can keep your code neat, tidy, and understandable. Personal preferences are different for everyone, but be sure you keep the same style throughout. And don’t forget to use comments!

 

Click here to return to the top

 

 

 

So, what do the colors mean when you’re editing something in Gamemaker?

 

The Gamemaker script editor uses a standard subset of colors to denote different things:

 

Self Bold text indicates a keyword. These are words like “self,” and “other.” They’re almost always used in the syntax “self.doSomething()”.  Other boldface words are reserved words like “if” and “else”.

 

X Light blue text indicates an object variable. These are built into the language. Every object you create has certain attributes, like X and Y coordinates, that describe it.

 

instance_create() Dark blue text indicates a function. Functions always require a set of parentheses behind them, even if the function doesn’t ask for an argument.

 

test_evo The names of objects show up in purple. This is a good way to know you haven’t misspelled anything—if you have, it won’t be the right color.

 

//comment Comments show up in green italics. There isn’t any way to make block comments within Gamemaker, so each line of comments has to have two forward slashes preceeding it. You can put comments after another statement. The computer doesn’t read anything to the right of the two slashes. Comments terminate with a carriage return (pressing [Enter] at the end of a commented line closes the comments, the next line down will be read normally).

 

variable=98; The rest of your code will show up in plain text.

 

 

Click here to return to the top

 

 

 

 

You shouldn’t ever limit yourself to just one programming language.

 

On top of Gamemaker, which is perfectly suited to Doops-style programs, other good languages to learn are HTML, Java, Basic, Visual Basic, C++, and Perl.  For lab environments, Labview is also a good language to know, but that’s very different form this sort of programming (Labview is a visual language, instead of a text language—you program by drawing pictures).

 

Visual basic suits itself very well to mathematical programs—it’s wonderfully flexible, fairly simple, and intuitive enough for beginners to grasp quickly.  If you’re a student, your school probably offers a course in either Visual Basic or C++, and I suggest taking anything that’s available.

 

 

 

 

So, in the Feigenbaum demo, how did I graph a recursive function?

 

This is something that people sometimes get confused on, so I’ll walk you through it. I’m going to do this the simplest way possible, which involves somewhat more typing, but relies on only the most basic commands.

 

 

(We’re going to use a variable, “iterations,” to keep track of how many values of “a” the function increments, and to stop the program after a certain number of iterations)

 

Dim iterations as integer

Dim initialN, initialA as double

 

(Next, you need a section of code inside a command button’s click sub, or something similar that says the following)

 

Timer1.enabled = true

initialN = some number (zero would work)

initialA = some number (again, zero would work)

 

(That turns on the timer that will call the feigfunc() sub repeatedly, and defines the initial values of N and A in our function “f(n)=f(n-1)^2 – a”)

 

(Next, you need to say what the timer will do. You put the following inside the timer’s sub)

 

Call feigfunc(initialN, initialA)

 

(that calls the feigfunc() sub, where all the real work happens)

 

Public sub feigfunc(Ni as double, Ai as double)

 

If iterations < 2000 then

newN = (Ni ^ 2) – Ai

 

(then you have to graph this somehow. Trick is that you want to graph your function relative to the size of the window; this takes some thinking, but the solution is below.)

 

h1, y1 as double

 

Picturebox1.width / 2 = h1

Picturebox1.height / 2 = y1

 

Picturebox1.pset((Ai + h1), (newN + y1), vbGreen)

(pset changes one pixel (x, y, color) to the color you specify. There is a pretty simple function embedded inside pset here: Ai + h1, and newN + y1 simple move the point (0,0) to the middle of your picturebox. You could also write an autoscaling function that would eliminate the possibility of errors when a value is larger than the width or height of your picturebox)

 

(finally, you have to set the variables to the values that the next iteration will require, and call the sub again, and then end)

 

initialN = newN

initialA = Ai + 1

Call feigfunc(initialN, initialA)

End if

End sub

 

 

 

And that’s all it takes. It might look confusing, but it’s actually very, very simple, and shouldn’t take more than fifteen or twenty minutes when you’re familiar with Visual Basic.

 

This sort of format is great to use, because the actual equation takes only one line—makes it wonderfully easy to change the equation on the fly, so that you can look at all sorts of different functions. Chaos Theory demonstrations are simplified particularly by this method.

 

Click here to return to the top

 

 

 

 

 

 

Click here to return to the welcome page