Animation issues with my 2d game?
Code:
void runGame(){ int area =imagesize(0, 0, 799, 799);
int level = 1;
//howtoplay();//video sequence yet to be made
//start game music;
gen_world();//gens first area
//bkimage = malloc(area);//used to make bk image read
int move = 0;
while (level <= 10){
testKeys();
//putimage(0,0,bkimage,COPY_PUT);
character_animation();
//moveCharacter(); //char movement functions
//check_char();
//printf("ended movement");
free (bkimage);
//gen_char();//these may not even be needed
//gen_enemies();
//level_check();
}//end level loop
}//end project
//struct template for animating character movement
struct animate {
int draws;
int animationFrames;
char *frames[TOTAL_FRAMES];
int charWidth;
int charHeight;
int xCoord;
int yCoord;
int xMove;
int yMove;
int currentFrame;
};
int thunderx = x9;
//Actual struct for character movement
struct animate character[MAX_LOOPS] =
{
{
//Character moving left
2,
3,
{
"PIX\\Character\\left_r.gif",
"PIX\\Character\\left_still.gif",
"PIX\\Character\\left_l.gif",
"PIX\\Character\\left_still.gif",
},
50,
50,
x9,
y9,
x9 = x9 + 5,
0,
0,
},
{
//Character moving right
1,
3,
{
"PIX\\Character\\right_r.gif",
"PIX\\Character\\right_still.gif",
"PIX\\Character\\right_l.gif",
"PIX\\Character\\right_still.gif",
},
50,
50,
x9,
y9,
x9 = x9 + 5,
0,
0,
},
{
//Character moving up
3,
3,
{
"PIX\\Character\\up_r.gif",
"PIX\\Character\\up_still.gif",
"PIX\\Character\\up_l.gif",
"PIX\\Character\\up_still.gif",
},
50,
50,
x9,
y9,
0,
y9 = y9 - 5,
0,
},
{
//Character moving down
4,
3, //number of frames for array to consider
{
"PIX\\Character\\down_r.gif",
"PIX\\Character\\down_still.gif",
"PIX\\Character\\down_l.gif",
"PIX\\Character\\down_still.gif",
},
50, //width of character
50, //Height of character
x9, //starting position
y9,
0,
y9 = y9 + 5, //Character movement
0,
},
//thunderball structs!
{
5, //thunderball going right
3,
{
"PIX\\thunderball\\thunderball.gif"
"PIX\\thunderball\\thunderball2.gif"
"PIX\\thunderball\\thunderball3.gif"
},
30,
30,
x9+40,
y9+10,
thunderx = thunderx + 10,
0,
0,
},
{
6, //thunderball going left
3,
{
"PIX\\thunderball\\thunderball.gif"
"PIX\\thunderball\\thunderball2.gif"
"PIX\\thunderball\\thunderball3.gif"
},
30,
30,
x9-10,
y9+10,
x9 = x9 - 10,
0,
0,
},
{
7, //thunderball going up
3,
{
"PIX\\thunderball\\thunderball.gif"
"PIX\\thunderball\\thunderball2.gif"
"PIX\\thunderball\\thunderball3.gif"
},
30,
30,
x9+10,
y9-10,
0,
y9 = y9 - 10,
0,
},
{
8, //thunderball going down
3,
{
"PIX\\thunderball\\thunderball.gif"
"PIX\\thunderball\\thunderball2.gif"
"PIX\\thunderball\\thunderball3.gif"
},
30,
30,
x9+10,
y9+40,
0,
y9 = y9 + 10,
0,
},
};
void character_animation()
{
int area = imagesize(0, 0, 799, 799);
bkimage = malloc(area);
getimage(0, 0, 799, 799, bkimage);
for (int i = 0; i < 4; i++){
if( ! character[i].draws)
continue;
while( !kbhit())
Sleep(100);
//if(character[i].draws == 1){
//Puts image of character on screen based on inputs from struct
readimagefile(character[i].frames[character[i].currentFrame],
character[i].xCoord,
character[i].yCoord,
character[i].xCoord + character[i].charWidth,
character[i].yCoord + character[i].charHeight );
character[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
if(character[i].currentFrame == character[i].animationFrames){
character[i].currentFrame = 0; //When frame 4 is reached, it resorts back to first frame and restarts
character[i].draws = 0;}
//These next two lines of code dictate the movement of the character
//character[i].xCoord = character[i].xCoord + 5;
}
//putimage(0, 0, bkimage, COPY_PUT);
Sleep(10);
}
void testKeys(){
for(int i = 0; i < MAX_LOOPS; i++){
if(kbhit()){
int c = getch();
if(c == 'd'){
character[i].draws = 1;
character[i].xCoord = character[i].xCoord + 5;}
if(c == 'a'){
character[i].draws = 2;
character[i].xCoord = character[i].xCoord - 5;}
if(c == 's'){
character[i].draws = 4;
character[i].yCoord = character[i].yCoord + 5;}
if(c == 'w'){
character[i].draws = 3;
character[i].yCoord = character[i].yCoord - 5;}
if(c == '1'){
character[i].draws = 5;
character[i].xMove;}
}
}
}
Hey. This is my code for my game. Mainly my main rungame function then a struct to handle character and attack animation and character_animate() to handle the animation. I am using visual studio 2012 and using a graphics.h library (very new to C :P).
My issue is thus: When I use the wasd keys, the character moves correctly... the problem is that there are 4 of them drawn at once all facing different directions and moving all in the same direction haha...
I am wondering if anyone has tips or anything on what is going wrong with this code that is causing all 4 to be drawn...?
Thanks for any help!