Help with first game - Tower Defense

Started by
1 comment, last by Zahlman 16 years, 2 months ago
Hello, everyone. I am hoping you all can help me with a problem I am having. Using the Allegro libraries, I am trying to program a very simplified version of a tower defense style game. I have the playfield, and the monsters walking the path, but when I try adding the mouse control, the check to see if the mouse button has been pressed seems to be continuously setting monster number 10's x-coordinate back to 0. Obviously this is ugly code, and it is still in its infancy, but any help you kind folks might be able to provide me will be appreciated. Thank you.

=======
Header file
=======
#ifndef _GAMESTUFF_H
#define _GAMESTUFF_H

#include "allegro.h"

// Monster function prototypes

void drawmonster(int num);
void erasemonster(int num);
void movemonster(int num);
void monsterexplode(int num);

// define the monsters
struct tagMonster
{
       int lifepoints;
       int moneyvalue;
       int walkspeed;
       int xlocation;
       int ylocation;
       bool stillalive;
       
}monsters[9];
#endif
=====
main.cpp
=====

#define ALLEGRO_STATICLINK

#include <stdlib.h>
#include "allegro.h"
#include <GameStuff.h>

int gamelevel = 1;
int CitizenCounter = 10;
int Money = 100;
bool GameOver = false;
int mx;
int my;
int mousebutton;

// Game recordkeeping functions
void UpdateCitizens()
{
    rectfill(screen,560,300,615,350,9);    
    textprintf_ex(screen, font, 580, 320, 15,9,"%d",CitizenCounter);
}
void UpdateMoney()
{
     rectfill(screen,0,440,100,480,10);
     textprintf_ex(screen,font,10,465,15,10,"Money Remaining: $%d",Money);
}

void movemonster(int n)
{
     if ((monsters[n].xlocation < 250) && (monsters[n].ylocation < 225))
     {
          monsters[n].ylocation++;
     }
     if ((monsters[n].xlocation < 250) && (monsters[n].ylocation == 225))
     {
          monsters[n].xlocation++;
     }
     if ((monsters[n].xlocation == 250) && (monsters[n].ylocation > 125))
     {
          monsters[n].ylocation--;
     }
     if ((monsters[n].xlocation < 425) && (monsters[n].ylocation == 125))
     {
          monsters[n].xlocation++;
     }
     if ((monsters[n].xlocation == 425) && (monsters[n].ylocation < 325))
     {
          monsters[n].ylocation++;
     }
     if ((monsters[n].xlocation < 530) && (monsters[n].ylocation == 325))
     {
          monsters[n].xlocation++;
     }
}

void createmonster(int n)
{
     monsters[n].xlocation = 45;
     monsters[n].ylocation = 0 - ((n - 1) * 50);
     monsters[n].moneyvalue = gamelevel;
     monsters[n].lifepoints = (gamelevel * 1.25);
     monsters[n].walkspeed = 1000;
     monsters[n].stillalive = true;     
}

void erasemonster(int n)
{
     circlefill(screen, monsters[n].xlocation, monsters[n].ylocation, 18, 6);
}

void drawmonster(int n)
{
     circlefill(screen, monsters[n].xlocation, monsters[n].ylocation, 18, 5);
}

void monsterexplode(int n)
{
     ;
}


int main()
{
    //Initialize Allegro
    allegro_init();
    
    //Initialize keyboard
    install_keyboard();
    
    //Install mouse
    install_mouse();

    //initialize video mode to 640x480
    int ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    if(ret != 0) {
           allegro_message(allegro_error);
           return 1;
           }

    //Make the background green
    rectfill(screen,0,0,640,480,10);
    
    //Draw the pathway
    rectfill(screen,0,0,70,200,6);
    rectfill(screen,0,200,230,250,6);
    rectfill(screen,230,100,275,250,6);
    rectfill(screen,275,100,400,150,6);
    rectfill(screen,400,100,450,350,6);
    rectfill(screen,450,300,550,350,6);
    
    //Draw the castle
    rectfill(screen,550,290,625,365,0);
    circlefill(screen,545,285,18,0);
    circlefill(screen,545,370,18,0);
    circlefill(screen,630,285,18,0);
    circlefill(screen,630,370,18,0);
    
    //Draw the rectangle that will contain the citizens
    rectfill(screen,560,300,615,350,9);
    
    //Draw the tower build section of the screen
    rectfill(screen,0,400,200,435,9);
    textprintf_ex(screen,font,5,415,15,9,"Click this area");
    textprintf_ex(screen,font,5,425,15,9,"to build a tower");
    
    
    //Draw the initial citizen total
    UpdateCitizens();
    
    //Draw the starting money
    UpdateMoney();
    //Cause the program to show the mouse
    show_mouse(screen);

    while(!GameOver)
    {
    //This is the main game loop
    
         //Create the monsters
         for(int x = 0; x < 10; x++)
         {
                 createmonster(x);
         }
         
         //Internal game loop to handle monster movement, shooting, etc.
         for(int walkcount = 0; walkcount < 1500; walkcount ++)
         {
//Did the player click on 'Build a tower'?
          mx = mouse_x;
          my = mouse_y;
          if((mx < 200) && (mx > 0) && (my < 435) && (my > 400))
          {
          mousebutton = (mouse_b & 1);
          if(mousebutton)
          {
               textprintf_ex(screen,font,5,415,9,9,"Click this area");
               textprintf_ex(screen,font,5,425,9,9,"to build a tower");
               textprintf_ex(screen,font,5,415,15,9,"Click any valid");
               textprintf_ex(screen,font,5,425,15,9,"area to build a tower");
          }
          }                 
                 rest(5);
                 //Move the monsters
                 for (int x = 0; x < 10; x++)
                 {
                     
                     if (monsters[x].stillalive)
                     {
                          erasemonster(x);
                          movemonster(x);
                          drawmonster(x);
                          if (monsters[x].xlocation > 529)
                          {
                               erasemonster(x);
                               monsters[x].stillalive = false;
                               monsters[x].xlocation = 0;
                               monsters[x].ylocation = 0;
                               CitizenCounter--;
                               UpdateCitizens();
                               if (CitizenCounter == 0)
                               {
                                    while(!key[KEY_ESC])
                                    {
                                         textprintf_ex(screen,font,310,240,15,10,"GAME OVER!! Press ESCape to Exit");
                                    }
                               }
                          }
                      }
                  }
         }
     }
     //end program
     allegro_exit();
     return 0;
}
END_OF_MAIN();

Edit: Sorry about that Edit 2:Okay, I changed the array loops. Shakedown: what crazy characters? [Edited by - Mordaris on February 14, 2008 3:18:17 PM]
Advertisement
Edit your post and put your source code in ['source']['/source'] tags (minus the ').

Now that you've edited your post and included source tags, there are a bunch of crazy characters inserted in your code. You might want delete your code and re-paste it again, that should get rid of those weird characters.

[Edited by - Shakedown on February 14, 2008 1:31:13 PM]
Quote:
// define the monstersstruct tagMonster{}monsters[10];void createmonster(int n){     monsters[n].xlocation = 45; // etc. }         for(int x = 1; x < 11; x++) // <--- >___<         {                 createmonster(x);         }


No. Array indices start at zero and go up to just less than the number of elements. The valid indices for an array of ten elements are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. This is done to make the math easier and avoid errors. And once you become familiar with how it works, you'll agree that it does make it a lot easier to avoid messing up :)

Quote:
                 for (int x = 0; x < 11; x++)                 {


Similarly... now we are trying to consider eleven elements of a ten-element array.

This topic is closed to new replies.

Advertisement