Sign in to follow this  
yurian

Looks like it's pressing keys without my knowing?

Recommended Posts

yurian    182
This is my program =) Sorry I include the whole thing and made it difficult, but I don't know what is totally important. My problem is that when I compile this program, the bitmap failx.bmp draws itself in the corner. But in this program, the failx has nothing to do with bugs, exception, whatever it is. It is used for when the player press the attack button before signal is given. So I don't understand. And nothing else is working, it's hard to say because that is the only thing I can see. Sorry if I sound confusing! I think that this program is supposed to first draw the title picture on the screen and wait for my orders, but is skipping past that step... Aaaaah I cannot understand. Need help, have been working for a long time.
#include <allegro.h>
#include <cstdlib>
#include <time.h>
#define edeadplace 250
#define pdeadplace 70
#define p 100
#define e 540
#define y 240
#define center 320

int attackcount = 0;
int strikecount = 0;
int attacktime = 0;
int striketime = 0;

void attackplus()
{attackcount++;}END_OF_FUNCTION(attackplus);
void strikeplus()
{strikecount++;}END_OF_FUNCTION(strikeplus);

bool gamenotquit = true;

class allthings
{
      int level;
      bool haswon;
      bool failure;
      
      BITMAP*player,*playera,*playerd,*zyars,*zyarsa,*zyarsd,*vandy,*vandya,*vandyd,*mitsu,*mitsua,*mitsud,*ryoku,*ryokua,*ryokud,*drksp,*drkspa,*drkspd;
      BITMAP*champion,*valley,*redraw,*start,*failx;
      SAMPLE*wavplayera,*wavzyarsa,*wavvandya,*wavmitsua,*wavryokua,*wavdrkspa,*wavwind,*attacksignal;
      public:
             allthings();
             ~allthings();
             void declaration();
             void faillevel();
             void attacktie();
             void enemyattack();
             void playerattack();
             void nextlevel();
             void menu();
             void resetlevel();
             void warscreen();
};



allthings::allthings()
{
    
                      level = 1;
                      attackcount = 0;
                      strikecount = 0;
                      attacktime = 0;
                      striketime = 0;
                      haswon = false;
                      failure = false;
      
                      player = load_bitmap("player.bmp",NULL);
                      playera = load_bitmap("playera.bmp",NULL);
                      playerd = load_bitmap("playerd.bmp",NULL);
                      
                      zyars = load_bitmap("zyars.bmp",NULL);
                      zyarsa = load_bitmap("zyarsa.bmp",NULL);
                      zyarsd = load_bitmap("zyarsd.bmp",NULL);
                      
                      vandy = load_bitmap("vandy.bmp",NULL);
                      vandya = load_bitmap("vandya.bmp",NULL);
                      vandyd = load_bitmap("vandya.bmp",NULL);
                      
                      mitsu = load_bitmap("mitsu.bmp",NULL);
                      mitsua = load_bitmap("mitsua.bmp",NULL);
                      mitsud = load_bitmap("mitsud.bmp",NULL);
                      
                      ryoku = load_bitmap("ryoku.bmp",NULL);
                      ryokua = load_bitmap("ryokua.bmp",NULL);
                      ryokud = load_bitmap("ryokud.bmp",NULL);
                      
                      drksp = load_bitmap("drksp.bmp",NULL);
                      drkspa = load_bitmap("drkspa.bmp",NULL);
                      drkspd = load_bitmap("drkspd.bmp",NULL);
                      
                      champion = load_bitmap("champion.bmp",NULL);
                      valley = load_bitmap("valley.bmp",NULL);
                      redraw = load_bitmap("redraw.bmp",NULL);
                      start = load_bitmap("start.bmp",NULL);
                      start = load_bitmap("failx.bmp",NULL);
}
allthings::~allthings()
{
                       destroy_bitmap(player);
                       destroy_bitmap(playera);
                       destroy_bitmap(playerd);
                       
                       destroy_bitmap(zyars);
                       destroy_bitmap(zyarsa);
                       destroy_bitmap(zyarsd);
                       
                       destroy_bitmap(vandy);
                       destroy_bitmap(vandya);
                       destroy_bitmap(vandyd);
                       
                       destroy_bitmap(mitsu);
                       destroy_bitmap(mitsua);
                       destroy_bitmap(mitsud);
                       
                       destroy_bitmap(ryoku);
                       destroy_bitmap(ryokua);
                       destroy_bitmap(ryokud);
                       
                       destroy_bitmap(drksp);
                       destroy_bitmap(drkspa);
                       destroy_bitmap(drkspd);
                       
                       destroy_bitmap(champion);
                       destroy_bitmap(valley);
                       destroy_bitmap(redraw);
                       destroy_bitmap(start);
                       destroy_bitmap(failx);
                       
}


void allthings::declaration()
{
     draw_sprite(screen,champion,0,0);
     readkey();
}

void allthings::nextlevel()
{
     rest(2500);
     if(level < 5)
     {
              level ++;
              warscreen();
     }
     else if(level >= 5)
     {
          rest(1000);
          level = 1;
          declaration();
     }
}

void allthings::resetlevel()
{
     rest(2500);
     warscreen();
}

void allthings::faillevel()
{
     rest(4000);
     level = 1;
     menu();
}    

void allthings::playerattack()
{
     clear_bitmap(screen);
     draw_sprite(screen,valley,0,0);
     draw_sprite(screen,playera,center,y);
     play_sample(wavplayera,255,0,1000,0);
          if(level == 1)
     {draw_sprite(screen,zyarsd,edeadplace,y);}
     else if(level == 2)
     {draw_sprite(screen,vandyd,edeadplace,y);}
     else if(level == 3)
     {draw_sprite(screen,mitsud,edeadplace,y);}
     else if(level == 4)
     {draw_sprite(screen,ryokud,edeadplace,y);}
     else if(level == 5)
     {draw_sprite(screen,drkspd,edeadplace,y);}
     haswon = true;
}

void allthings::attacktie()
{
     clear_bitmap(screen);
     draw_sprite(screen,valley,0,0);
     draw_sprite(screen,playera,edeadplace,y);
     draw_sprite(screen,redraw,center, 100);
     play_sample(wavplayera,255,0,1000,0);
          if(level == 1)
     {draw_sprite(screen,zyarsa,pdeadplace,y);
      play_sample(wavzyarsa,255,0,1000,0);}
     else if(level == 2)
     {draw_sprite(screen,vandya,pdeadplace,y);
      play_sample(wavvandya,255,0,1000,0);}
     else if(level == 3)
     {draw_sprite(screen,mitsua,pdeadplace,y);
      play_sample(wavmitsua,255,0,1000,0);}
     else if(level == 4)
     {draw_sprite(screen,ryokua,pdeadplace,y);
      play_sample(wavryokua,255,0,1000,0);}
     else if(level == 5)
     {draw_sprite(screen,drkspa,pdeadplace,y);
      play_sample(wavdrkspa,255,0,1000,0);}
}

void allthings::enemyattack()
{
     clear_bitmap(screen);
     draw_sprite(screen,valley,0,0);
     draw_sprite(screen,playerd,pdeadplace,y);
          if(level == 1)
     {draw_sprite(screen,zyarsa,center,y);
      play_sample(wavzyarsa,255,0,1000,0);}
     else if(level == 2)
     {draw_sprite(screen,vandya,center,y);
     play_sample(wavvandya,255,0,1000,0);}
     else if(level == 3)
     {draw_sprite(screen,mitsua,center,y);
     play_sample(wavmitsua,255,0,1000,0);}
     else if(level == 4)
     {draw_sprite(screen,ryokua,center,y);
     play_sample(wavryokua,255,0,1000,0);}
     else if(level == 5)
     {draw_sprite(screen,drkspa,center,y);
     play_sample(wavdrkspa,255,0,1000,0);}
}
void allthings::warscreen()
{
     play_sample(wavwind,255,0,1000,5);
     draw_sprite(screen,valley,0,0);
     
          if(level == 1){draw_sprite(screen,zyars,e,y);}
     else if(level == 2){draw_sprite(screen,vandy,e,y);}
     else if(level == 3){draw_sprite(screen,mitsu,e,y);}
     else if(level == 4){draw_sprite(screen,ryoku,e,y);}
     else if(level == 5){draw_sprite(screen,drksp,e,y);}
     
     draw_sprite(screen,player,p,y);
     
          if(level == 1){striketime = 1500;}
     else if(level == 2){striketime = 1300;}
     else if(level == 3){striketime = 1100;}
     else if(level == 4){striketime = 1000;}
     else if(level == 5){striketime =  900;}
     
     attacktime = rand() % 10 + 4;
     attackcount = 0;
     
     while(attackcount < attacktime)
     {
                       if(key[KEY_SPACE])
                       {
                                         failure = true;
                                         draw_sprite(screen,failx,p,y);
                       }
     }
     play_sample(attacksignal,255,1000,0,1);
     while(strikecount < striketime - 100)
     {
                       if(key[KEY_SPACE])
                       {
                                         stop_sample(wavwind);
                                         playerattack();
                                         nextlevel();
                       }
     }
     while(strikecount > striketime - 100 && strikecount < strikecount)
     {
                       if(key[KEY_SPACE])
                       {
                                         stop_sample(wavwind);
                                         attacktie();
                                         resetlevel();
                       }
     }
     if(strikecount >= striketime && haswon == false)
     {
                    stop_sample(wavwind);
                    enemyattack();
                    faillevel();
     }
}

void allthings::menu()
{
     draw_sprite(screen,start,0,0);
     if(key[KEY_ENTER])
     {
                       warscreen();
     }
}

int main()
{
    allegro_init();
    
    install_keyboard();
    
    install_timer();
    install_int_ex(attackplus,SECS_TO_TIMER(1));
    install_int_ex(strikeplus,MSEC_TO_TIMER(100));
    LOCK_VARIABLE(attackcount);
    LOCK_FUNCTION(attackplus);
    LOCK_VARIABLE(strikecount);
    LOCK_FUNCTION(strikeplus);
    
    install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
    
    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
    
    srand(time(NULL));
    
    allthings*control=new allthings;
    
    while(!key[KEY_ESC])
    {
                      control->menu();
    }
    
     delete control;
     clear_keybuf();
     return 0;
}END_OF_MAIN();


Share this post


Link to post
Share on other sites
speciesUnknown    527
Feed all button presses through a single function, and get this function to dump the key press before passing the key to allegro. Then you will know what is being pressed and when.

Share this post


Link to post
Share on other sites
yurian    182
Oh sorry, I mean I don't know why the program is drawing the failx.bmp before I even press any key. The failx is only in warscreen function, but I am still in menu, or should be.

But if it is in warscreen, where is valley and player bitmap... I do not understand.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:

if(level == 1){draw_sprite(screen,zyars,e,y);}
else if(level == 2){draw_sprite(screen,vandy,e,y);}
else if(level == 3){draw_sprite(screen,mitsu,e,y);}
else if(level == 4){draw_sprite(screen,ryoku,e,y);}
else if(level == 5){draw_sprite(screen,drksp,e,y);}


Er, have you by any chance heard of arrays?

Let the 'level' range from 0 to 4, inclusive, instead. (You can change 'level' to 'level + 1' when you need to *display* that number.) Then have your sprites in an array:


BITMAP* enemies[] = { zyars, vandy, mitsu, ryoku, drksp };


And then you just select the appropriate sprite from the array to draw:


draw_sprite(screen, enemies[level], e, y);


Quote:

class allthings
{
int level;
bool haswon;
bool failure;

BITMAP*player,*playera,*playerd,*zyars,*zyarsa,*zyarsd,*vandy,*vandya,*vandyd,*mitsu,*mitsua,*mitsud,*ryoku,*ryokua,*ryokud,*drksp,*drkspa,*drkspd;
BITMAP*champion,*valley,*redraw,*start,*failx;
SAMPLE*wavplayera,*wavzyarsa,*wavvandya,*wavmitsua,*wavryokua,*wavdrkspa,*wavwind,*attacksignal;


Apparently you have heard of classes, but maybe it wasn't pointed out to you that one of the main purposes of a class is to bundle together *related* things, for the purpose of *organization*. You don't gain much by dumping everything in one place.

Let's look at those bitmap and sample names. See a pattern? Looks to me like there is a set of 'normal, attack, defense' bitmaps, and a sample, for each of several "characters". Let's make a structure for that:


struct Character {
BITMAP* normal, attack, defense;
SAMPLE* attack_sound;
};


Notice how now, since each name is not "decorated" with information about "who" it belongs too, we have more room to write a better descriptive name. Then we clean up the main class (fix things bit by bit, you know):


class allthings
{
int level;
bool haswon;
bool failure;

Character player, zyars, vandy, mitsu, ryoku, drksp;
BITMAP*champion,*valley,*redraw,*start,*failx;
SAMPLE*wavwind,*attacksignal;


Except... we remember that array hint. If we made that array *here*, we could make use of it everywhere that we are checking the 'level'. Oh, but we'd lose the names, you protest? Well, do we really care about them any more? :) We only need them when we load the bitmaps.


class allthings
{
int level;
bool haswon;
bool failure;

Character player, enemies[5];
BITMAP*champion,*valley,*redraw,*start,*failx;
SAMPLE*wavwind,*attacksignal;


As it happens, there's a whole bunch of repeated code needed for creating and cleaning up all those bitmaps, too. All the names follow a repeated pattern. Fortunately, C++ lets us easily construct those names.


struct Character {
BITMAP* normal, attack, defense;
SAMPLE* attack_sound;

// We add a constructor, which accepts the "name":
Character(const std::string& name) :
normal(load_bitmap((name + ".bmp").c_str(), NULL)),
attack(load_bitmap((name + "a.bmp").c_str(), NULL)),
defense(load_bitmap((name + "d.bmp").c_str(), NULL)), attack_sound(NULL) {}

// and a destructor:
~Character() {
destroy_bitmap(normal);
destroy_bitmap(attack);
destroy_bitmap(defense);
}

// Since we are going to put Characters in an array, they need a default
// constructor:
Character() : normal(NULL), attack(NULL), defense(NULL), attack_sound(NULL) {}

// These things shouldn't get copied, BTW, because it would end up trying to
// clean up the bitmap multiple times and mess up. So we prevent copying:
private:
Character(const Character&);
Character& operator=(const Character&);
// Now the compiler will complain if we accidentally try to copy a Character.
};


That lets us clean up the constructor and destructor for 'allthings' considerably:


allthings::allthings()
{
level = 1;
attackcount = 0;
strikecount = 0;
attacktime = 0;
striketime = 0;
haswon = false;
failure = false;

player = Character("player");
std::string enemy_names[5] = { "zyars", "vandy", "mitsu", "ryoku", "drksp" };
for (int i = 0; i < 5; ++i) {
enemies[i] = Character(enemy_names[i]);
}

champion = load_bitmap("champion.bmp",NULL);
valley = load_bitmap("valley.bmp",NULL);
redraw = load_bitmap("redraw.bmp",NULL);
start = load_bitmap("start.bmp",NULL);
start = load_bitmap("failx.bmp",NULL);
}

allthings::~allthings()
{
// All the Character destructors will get called automatically here.
destroy_bitmap(champion);
destroy_bitmap(valley);
destroy_bitmap(redraw);
destroy_bitmap(start);
destroy_bitmap(failx);
}





By now, you must be getting very fed up with me, because I'm showing you all this code cleanup technique when you just want your bug fixed. "What's the point," you ask?

Well, now that the code is that much shorter, why don't you take a look at the new, shortened allthings::allthings() and see if the problem isn't a teeny bit more obvious. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this