YOU ARE VIEWING ARCHIVAL DOCUMENTATION FOR ioL VERSIONS 0.9x. Go to doc.iol.science for the latest version.
ioL

Programming manual (doc0.iol.science)



Game over!

Congratulations on getting to the end of this tutorial and language-tour of ioL!

On this website, you'll find a complete reference to all the tags, fields and syntactical constructs that are available in ioL. Have fun!

Review

Here is the source code for a very simple game written in C. It is not a particularly difficult game, and you wouldn't find it in stores, yet it is still more exciting than anything EA has put out in the last ten years.

A box of random size and color will appear at a random position. When the user clicks inside the box, a new box will appear (actually, the same box is moved to a new position). After the user clicks 10 boxes, the game is over.


// GAME OVER!
// A simple game to demonstrate ioL.
// The user must click a series of boxes, and then must defeat a special boss
// character at the end of the game.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void mainMenu() {
//sets up the main menu
    printf("<box inline=false border=0 width=100,{%%} bottom=10,{%%}\n");
    printf("  text-align={center} { \n");
    //we need to use "%%" instead of "%" because the printf() function thinks
    //that "%" means something else.

    printf("  <mission:span {YOUR MISSION</p>Click 10 boxes. Beat the boss.</p>Then it's}></n>");
    printf("  <span size=75,{pt} {GAME OVER!}></p>\n");
    printf("  <button {PLAY} onclick=<putln {p}>> \n");
    printf("  <button {EXIT} onclick=<putln {q}>> \n");
    printf("}>\n");
}

void setupGame() {
//sets up the gameplay in a box element which can be hidden before and after the game.
    printf("<game:box inline=false top=0 left=0 right=0 bottom=0 border=0 visible=false background-color={black} {");
    //wrap the gameplay in a box that fills the screen.
    
    //set up a box element with all the field attributes we will manipulate.
    printf("  <clickthis:box width=0 height=0 background-color=0 rotate=0");
    printf("    inline=false top=0 left=0 onclick=<putln {B}>>");
    //request a "B" as input whenever the user clicks a box.

    //keep track of how many more boxes the user needs to click to finish the game.
    printf("  <score:span size=18,{pt} {BOXES LEFT: 10}>");
    
    printf("}>");
}

void randomizeBox() {
//moves the box to a new location, changes its size and also changes its 
//background color to random values.
//The box has already been created and is named clickthis

    //Use percentage values so the gameplay scales nicely with the window size.

    printf("<game.clickthis.width %d,{%%}>", (rand() % 16) + 10);
    //choose a random width between 10% and 25%

    printf("<game.clickthis.height %d,{%%}>", (rand() % 21) + 5);
    //choose a random height between 5% and 25%

    printf("<game.clickthis.left %d,{%%}>", (rand() % 76) + 5);
    printf("<game.clickthis.top %d,{%%}>", (rand() % 76) + 5);
    //choose top and left coordinates between 5% and 80%

    printf("<game.clickthis.rotate -%d,{deg}>", (rand() % 90));
    //choose a random rotation from 0 to -89 degrees.

    printf("<game.clickthis.background-color %d>", (rand() % 0xFFFFFF));
    //choose a random color from x000000 (full black) to xFFFFFF (full white)
}

void playGame() {
//start a new game - reset everything.

    printf("<game.background-color {black}>");
    printf("<game.visible true>"); //show the gameplay.
    printf("<game.score {BOXES LEFT: 10}>"); //assign new text to score box
    printf("<game.clickthis.clear>"); //clear any text on the box
}


int main(int argc, char* argv[]) {
    srand(time(NULL)); //set up the random number generator
    int boxesLeft = 10; //keep track of the player's progress.

    //set up the color scheme for our game...
    printf("<program.background-color {black}><program.color {white}>");
    printf("<program.margin 0>");

    //set the program title...
    printf("<program.title {GAME OVER!}>");

    //request the console to send us a simple "q" as input when the user wants
    //to quit...
    printf("<program.onexit.push <putln {q}>>");

    mainMenu();
    setupGame();

    int quit = 0;
    while(!quit) {
        boxesLeft = 10; //player has to click 10 boxes to finish the game.
        do {
            char input;
            input = getchar(); //get user input, but don't count newlines.
            if(input == 'q') { //user wants to quit.
                quit=1;
                break;
            } else if(input == 'p') { //user wants to start the game.
                boxesLeft = 10;
                playGame();
                randomizeBox();
            } else if(input == 'B') { //user has knocked down one box in the game.
                boxesLeft--;
                printf("<game.score {BOXES LEFT: %d}>", boxesLeft);
                randomizeBox();
                if(boxesLeft == 1) { //this is the last box of the game...
                    printf("<game.clickthis {BOSS}>"); //time to get serious!
                    for(int i=0; i<3; i++) {
                        nanosleep((const struct timespec[]){{0, 200000000L}}, NULL);
                        randomizeBox(); //cycle over a few possibilities with a short delay between
                    }
                    //assign the box's background color to the container object.
                    printf("<game.background-color game.clickthis.background-color>");

                    //the boss always wears black.
                    printf("<game.clickthis.background-color {black}>");
                }
            }
        } while(boxesLeft);
        if(!quit) {  //player won the game like we always believed they could.
            printf("<mission {Your heart racing, you move the mouse pointer to the last remaining box...</p>");
            printf("and click the left mouse button.</p>The final boss is defeated.}>");

            printf("<game.visible false>"); //reveal the main menu at the end of each game.
        }
    }

    return 0;
}

Copy and paste this code into a file named gameover.c. Compile and run the game through an ioL console environment. Then go through the source code and try to understand what is going on.

Alternatively, you can download the code listed above from the Sample Programs page.