For some strange reason, my game's score counter is not working correctly.

I threw in an enemy, which just walks back and forth between two blocks in the map. I know I probably didn't program the enemy the smartest way, and I'm probably about to change it. Instead of programming it to use collision detection, I simply have it going back and forth between to coordinates right now.

The weird thing is that when I have the enemy running, my score counter gets thrown out of whack. If I comment out the enemy's code, the score counter will work properly. (start at 0, deduct 10 points for falling into the water, add 100 points for completing the level, etc.)

When I run the program with the enemy active, the score starts off at a very large random number. I have no idea why it would be doing this. Does anyone have any idea what is causing this??

The enemy's code bits are lines 90-94, 106-114, 213-242, 361-367.
I realize that the way I've done some of the things is probably the most ass-backwards and illogical way I could have done... so if you have any suggestions on other things, I'll take those too. :)

I've attached a .zip of the program's folder and entire contents, so you can try running it if you like.

Here is my code so far:

#include <stdio.h>
#include <stdbool.h>
#include <allegro.h>
#include "mappyal.h"
#include <time.h>

#define MODE GFX_AUTODETECT_WINDOWED
#define WIDTH 640
#define HEIGHT 480
#define JUMPIT 1600
#define RED makecol(255,0,0)
#define BLACK makecol(0,0,0)

_Bool objective1;

SAMPLE *splash;
SAMPLE *fanfare;

//define the sprite structure
typedef struct SPRITE
{
    int dir, alive;
    int x,y;
    int width,height;
    int xspeed,yspeed;
    int xdelay,ydelay;
    int xcount,ycount;
    int curframe,maxframe,animdir;
    int framecount,framedelay;
}SPRITE;

//declare the bitmaps and sprites
BITMAP *player_image[8];
BITMAP *enemy_image[5];
SPRITE *player;
SPRITE *enemy;
BITMAP *buffer;
BITMAP *temp;


//tile grabber
BITMAP *grabframe(BITMAP *source, 
                  int width, int height, 
                  int startx, int starty, 
                  int columns, int frame)
{
    BITMAP *temp = create_bitmap(width,height);
    int x = startx + (frame % columns) * width;
    int y = starty + (frame / columns) * height;
    blit(source,temp,x,y,0,0,width,height);
    return temp;
}


int collided(int x, int y)
{
    BLKSTR *blockdata;
	blockdata = MapGetBlock(x/mapblockwidth, y/mapblockheight);
	return blockdata->tl;
}


int main (void)
{
    int mapxoff, mapyoff;
    int oldpy, oldpx;
    int oldey, oldex;
    int facing = 1;
    int efacing;
    int jump = JUMPIT;
    int n;
    int volume = 128;
    int score, lvl1score, lvl2score, health, holding, deposit;
    
    MIDI *music1, *music2;
    
	allegro_init();	
	install_timer();
    	
    install_keyboard();
	set_color_depth(16);
	set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);

    //load player sprites
    temp = load_bitmap("guy.bmp", NULL);
    for (n=0; n<8; n++)
        player_image[n] = grabframe(temp,50,64,0,0,8,n);
    destroy_bitmap(temp);
    
    //load enemy sprites
    temp = load_bitmap("enemy.bmp", NULL);
    for (n=0; n<5; n++)
        enemy_image[n] = grabframe(temp,32,32,0,0,5,n);
    destroy_bitmap(temp);

    player = malloc(sizeof(SPRITE));
    player->x = 0;
    player->y = 100;
    player->curframe=0;
    player->framecount=0;
    player->framedelay=6;
    player->maxframe=7;
    player->width=player_image[0]->w;
    player->height=player_image[0]->h;
    
    enemy = malloc(sizeof(SPRITE));
    enemy->x = 840;
    enemy->y = 352;
    enemy->curframe=0;
    enemy->framecount=0;
    enemy->framedelay=6;
    enemy->maxframe=4;
    enemy->width=player_image[0]->w;
    enemy->height=player_image[0]->h;
    
    //install the sound driver
    if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0)
    {
       allegro_message("Error initializing sound system\n%s\n", allegro_error);
       return 1;
    }
    
    //load the wav files
    splash = load_sample("splash.wav");
    fanfare = load_sample("fanfare.wav");
    if (!splash || !fanfare)
    {
       allegro_message("Error reading .wav file!");
       return 1;
    }
    
    //load the MIDI music file
    music1 = load_midi("Head.mid");
    music2 = load_midi("Castle.mid");
    if (!music1 || !music2)
    {
       allegro_message("Error loading the MIDI file!");
       return 1;
    }
    //play the music
    if (play_midi(music1, 0) != 0)
    {
       allegro_message("Error playing the MIDI\n%s", allegro_error);
       return 1;
    }
    
    //load the map
	MapLoad("map1.fmp");
    
    //create the double buffer
	buffer = create_bitmap (WIDTH, HEIGHT);
	clear(buffer);
	
    //main loop
	while (!key[KEY_ESC])
	{
        if ((player->x >= 9440) && (player->x <= 9480) && (key[KEY_ENTER]))
        {
           score += 100;
           stop_midi();
           play_sample(fanfare, 200, 128, 1000, 0);
           textprintf_ex(screen, font, 150, 300, RED, BLACK, "LEVEL 1 COMPLETE! +100 points!");
           textprintf_ex(screen, font, 0, 0, RED, BLACK, "Score: %d", score);
           rest(5000);
           MapLoad("map2.fmp");
           play_midi(music2, 0);
           mapxoff = 0;
           facing = 1;
           efacing = 1;
           player->x = 80;
           player->y = 351;
           objective1 = false;
        }
        
        
        oldpy = player->y; 
        oldpx = player->x;
        oldey = enemy->y;
        oldex = enemy->x;
        
        textprintf_ex(screen, font, 0, 0, RED, BLACK, "Score: %d", score);
        textout_ex(screen, font, "Health:    ", 0, 10, RED, BLACK);
        textout_ex(screen, font, "Holding:   ", 0, 20, RED, BLACK);
        textout_ex(screen, font, "Deposit:   ", 0, 30, RED, BLACK);
        //textout_ex(screen, font, "Time:     ", 300, 0, RED, BLACK);
        textprintf_ex(screen, font, 0, 40, RED, BLACK, "player->x: %d", player->x);
        textprintf_ex(screen, font, 0, 50, RED, BLACK, "player->y: %d", player->y);
        
        //player falls in water
        if (player->y > 400)
        {                      
           score -= 10;
           play_sample(splash, 200, 128, 1000, 0);
           textprintf_ex(screen, font, 150, 300, RED, BLACK, "YOU HAVE FALLEN INTO THE WATER! -10 points!");
           textprintf_ex(screen, font, 0, 0, RED, BLACK, "Score: %d", score);
           rest(3000);
           player->x = 0;
           player->y = 351;         
        }
        
        //ADJUST THE MUSIC VOLUME
        if (key[KEY_EQUALS] && volume<255) 
        {
           volume++;
           set_volume(0,volume);
        }
        else if (key[KEY_MINUS] && volume>0) 
        {
             volume--;
             set_volume(0,volume);
        }
        
       //HANDLE ENEMIES' MOVEMENTS
        if (efacing)
        {
           enemy->x+=2;
           if (++enemy->framecount > enemy->framedelay)
           {
              enemy->framecount=0;
              if (++enemy->curframe > enemy->maxframe)
                 enemy->curframe=1;
           }
        }
        if (!efacing)
        {
           enemy->x-=2;
           if (++enemy->framecount > enemy->framedelay)
           {
              enemy->framecount=0;
              if (++enemy->curframe > enemy->maxframe)
                 enemy->curframe=1;
           }
        }
        
        if (enemy->x < 830)
        {
           efacing = 1;
        }
        if (enemy->x > 1185)
        {
           efacing = 0;
        }
        
        
        //PLAYER COLLISION DETECTION
		if (!facing) 
        { 
            if (collided(player->x, player->y + player->height)) 
                player->x = oldpx; 
        }
		else 
        { 
            if (collided(player->x + player->width, 
                player->y + player->height)) 
                player->x = oldpx; 
        }
        
                
        //HANDLE PLAYER MOVEMENT
		if (key[KEY_RIGHT]) 
        { 
            facing = 1; 
            if (player->x > 9550)
            {
               player->x=9550;
            }
            player->x+=12;
            if (++player->framecount > player->framedelay)
            {
                player->framecount=0;
                if (++player->curframe > player->maxframe)
                    player->curframe=1;
            }
        }
        else if (key[KEY_LEFT]) 
        { 
            facing = 0; 
            if (player->x < 1)
            {
               player->x=1;
            }
            player->x-=6;
            if (++player->framecount > player->framedelay)
            {
                player->framecount=0;
                if (++player->curframe > player->maxframe)
                    player->curframe=1;
            }
        }
        else player->curframe=0;

        //HANDLE PLAYER JUMPING
        if (jump==JUMPIT)
        { 
            if (!collided(player->x + player->width/2, 
                player->y + player->height + 5))
                jump = 0; 

		    if (key[KEY_SPACE]) 
                jump = 22;
        }
        else
        {
            player->y -= jump/3; 
            jump--; 
        }

		if (jump<0) 
        { 
            if (collided(player->x + player->width/2, 
                player->y + player->height))
			{ 
                jump = JUMPIT; 
                while (collided(player->x + player->width/2, 
                    player->y + player->height))
                    player->y -= 2; 
            } 
        }
        
        //PLAYER COLLISION DETECTION
		if (!facing) 
        { 
            if (collided(player->x, player->y + player->height)) 
                player->x = oldpx; 
        }
		else 
        { 
            if (collided(player->x + player->width, 
                player->y + player->height)) 
                player->x = oldpx; 
        }
		
        //update the map scroll position
		mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
		mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;


        //avoid moving beyond the map edge
		if (mapxoff < 0) mapxoff = 0;
		if (mapxoff > (mapwidth * mapblockwidth - WIDTH))
            mapxoff = mapwidth * mapblockwidth - WIDTH;
		if (mapyoff < 0) 
            mapyoff = 0;
		if (mapyoff > (mapheight * mapblockheight - HEIGHT)) 
            mapyoff = mapheight * mapblockheight - HEIGHT;

        //draw the background tiles
		MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1);
        
        //draw foreground tiles
		MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1, 1);

        //draw the player's sprite
		if (facing) 
            draw_sprite(buffer, player_image[player->curframe], 
                (player->x-mapxoff), (player->y-mapyoff+1));
		else 
            draw_sprite_h_flip(buffer, player_image[player->curframe], 
                (player->x-mapxoff), (player->y-mapyoff));
        
        //draw the enemy's sprite
        if (efacing)
            draw_sprite(buffer, enemy_image[enemy->curframe],
                (enemy->x-mapxoff), (enemy->y-mapyoff+1));
        else
            draw_sprite_h_flip(buffer, enemy_image[enemy->curframe],
                (enemy->x-mapxoff), (enemy->y-mapyoff));

        //blit the double buffer 
		vsync();
        acquire_screen();
		blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
		release_screen();

	} //while

    for (n=0; n<8; n++)
        destroy_bitmap(player_image[n]);
    free(player);
	destroy_bitmap(buffer);
	MapFreeMem ();
	allegro_exit();
	return 0;
}
END_OF_MAIN()

Recommended Answers

All 5 Replies

You never initialize score. If you don't initialize or assign a value to a variable, it's filled with crap. No one likes a crap filled variable. No one.

You never initialize score. If you don't initialize or assign a value to a variable, it's filled with crap. No one likes a crap filled variable. No one.

I'm not sure I understand what you mean exactly.
The score seems to work just fine, until I "activate" the enemy to start walking around. That is when it gets the really large random number.

Without the enemy, the score starts at zero like it should...and increments and decrements properly.

Would you mind running the code and showing me specifically what you think needs to be changed?

Thanks for your reply

on line 159 you have score += 100; and you haven't initialized it yet. then only time before line 159 that score is used is when it was declared as int score; .did you delete a score = 0; when you added the enemy part?

on line 159 you have score += 100; and you haven't initialized it yet. then only time before line 159 that score is used is when it was declared as int score; .did you delete a score = 0; when you added the enemy part?

Maybe I did on accident... I just went back to line 73 and initialized it to zero, and that seems to solve the problem.
I don't understand the technical reason behind it, but it worked! ;)

the reason is that when you declare a variable it is given a memory address and what ever happens to be in that address is what the variable is now equal to. so when you do int i; i has the value of whatever the memory its using has. so to avoid problems you normally want to set them to 0 before you and an manipulations with them.

int i;
i += 100;
cout << i;  // i could be whaterever

// safe approach
int i = 0;
i += 100;
cout << i;  // 100

If you are going to assign to a variable before manipulating it then it is fine to declare it without initializing it.

int i;
i = 100;
i += 1;
cout << i;  // 101

you just don't want to use a variable before you put something in it or as Sodabread said it will have crap in it and nobody wants to have crap.

commented: Good +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.