Animation issues with my 2d game?

This is a discussion on Animation issues with my 2d game? within the C Programming forums, part of the General Programming Boards category; Code: void runGame(){ int area =imagesize(0, 0, 799, 799); int level = 1; //howtoplay();//video sequence yet to be made //start ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    20

    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!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Shooting Projectile From Character Animation Trouble. - C And C++ | Dream.In.Code
    You missed the very last line of the commentary.

    [quote myself]
    mostly, this will be setting the appropriate directions of objects to be either .drawable = true (or not
    [/quote]

    The first field you've renamed to draws was called drawable.
    It's a boolean, and only ONE of the 4 directions should be true at any one time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    Hmm... could you explain in your code the significance of MAX_THINGS 10 in your code? Is having it equal 10 essentially important? And can you explain the use of the for() loop at the beginning of your doOneAnimation() (my character_animation()) function that uses MAX_THINGS. I suppose that is one part of your code that I did not quite understand what it was doing. Haha, this may help me understand better what 'i' is referring to exactly through each loop run, and thus help me understand how to specifically refer to one drawing animation.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Well MAX_THINGS is just the number of animated objects in your game, but this is distinct from the number of objects - which in this case is just two.
    - one is the player (which uses 4 slots for the 4 possible directions)
    - the other is the powerball, again using 4 slots for each of the 4 directions.

    But the powerball is the same in all directions, so perhaps you could optimise it to only one slot.

    The key detection reads something like
    Code:
        if(kbhit()){
            int c = getch();
            switch ( c ) {
            case 'a': direction = 0; break; // 
            case 'd': direction = 1; break; // 
            case 'w': direction = 2; break; // 
            case 's': direction = 3; break; // 
            }
        }
        if ( olddirection != direction ) {
            // stop the animation going one way
            character[olddirection].drawable = false;
            // and make it go another.
            character[direction].drawable = true;
            character[direction].currentFrame = 0; // start animation in a known state
            // set other parameters, like position (say the last position in the olddirection)
    
            olddirection = direction;
        }
    The idea of an array is just to get you going. More generally, you would keep a dynamic list of the game objects which grows/shrinks as the game develops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    lol . Utilizing what you have given me... using character[i].xMove never works, therefore I had to increment movement in the switch statement you put above. And no matter what I end up doing, it ALWAYS draws all 4 direction images on top of each other. Each button moves a different one in a different direction haha.

    I am kind of at a loss as far as ONLY producing one image on the screen at one time. It apparently wants to just ignore my if()'s and my else()'s...

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    Does anyone know how I should fix my xCoord and yCoord so when I press up, my character moves up. Then when I press over, my character that moved up is still there, but a new one is drawn at the starting position and moves left, same for right. I feel it is because I have same starting position for each struct, that it just restarts it there (xCoor and yCoor).

    Does anyone know what I should do with these to fix the issue?

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you want to have only one character then you should have only one position as well.
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    Haha this is what I am trying to do... When one direction button is pressed, one direction value is given. That direction value points to one struct, which animates one character. At least this is the logic of what I have, but of course it doesn't work.

    Right now, going left and right work just fine, but going up and down does not. Character doesn't move when going up and doesn't move when going down. just wiggles. Even though it is THE EXACT SAME as my left and right, just changed the movement direction. Ugh... frustrating

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Here
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <termios.h>
    #include <unistd.h>
    #include <sys/select.h>
    
    int mygetch ( void ) 
    {
      int ch = 0;
      struct termios oldt, newt;
      struct timeval tmo;
      fd_set  rfd;
    
      FD_ZERO(&rfd);
      FD_SET(0,&rfd);
      tmo.tv_sec = 0;
      tmo.tv_usec = 500000; // 1/2 second
      
      tcgetattr ( STDIN_FILENO, &oldt );
      newt = oldt;
      newt.c_lflag &= ~( ICANON | ECHO );
      tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
      int result = select(1,&rfd,NULL,NULL,&tmo);
      if ( result > 0 ) {
        ch = getchar();
      }
      tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
      
      return ch;
    }
    
    #define TOTAL_FRAMES 3
    struct animate {
        int drawable;
        int numFrames;
        char *frames[TOTAL_FRAMES];
        int xCoord;
        int yCoord;
        int xMove;
        int yMove;
        int currentFrame;
    };
    #define N_FRAMES  4
    struct animate npc[N_FRAMES] = {
      {
        1,
        3,
        { "Ll", "Ls", "Lr" }, // Left, left-foot, standing, right-foot
        20, 20,
        -1, 0,  // moving left, add -1 to x, add 0 to y
        0
      },
      {
        0,
        3,
        { "Rl", "Rs", "Rr" }, // Right, left-foot, standing, right-foot
        0, 0,
        +1, 0,  // moving right, add +1 to x, add 0 to y
        0
      },
      {
        0,
        3,
        { "Ul", "Us", "Ur" },
        0, 0,
        0, -1,  // moving up, add 0 to x, add -1 to y
        0
      },
      {
        0,
        3,
        { "Dl", "Ds", "Dr" },
        0, 0,
        0, +1,  // moving down, add 0 to x, add +1 to y
        0
      },
    };
    
    void animate ( void ) {
      for ( int i = 0 ; i < N_FRAMES ; i++ ) {
        if ( !npc[i].drawable ) continue;
        printf("(%02d,%02d)%s ", 
               npc[i].xCoord, npc[i].yCoord, 
               npc[i].frames[npc[i].currentFrame] );
        fflush(stdout);
        if ( ++npc[i].currentFrame == npc[i].numFrames ) {
          npc[i].currentFrame = 0;  // back to the start of the animation sequence
        }
        // move the character
        npc[i].xCoord += npc[i].xMove;
        npc[i].yCoord += npc[i].yMove;
      }
    }
    
    int main(void)
    {
      int ch;
      int oldDir = 0, newDir = 0;
      do {
        ch = mygetch();
        if ( ch != 0 ) {
          ch = toupper(ch);
          switch ( ch ) {
            case 'L':
              newDir = 0;
              break;
            case 'R':
              newDir = 1;
              break;
            case 'U':
              newDir = 2;
              break;
            case 'D':
              newDir = 3;
              break;
          }
        }
        if ( oldDir != newDir ) {
          // start off new direction from last old position
          npc[newDir].xCoord = npc[oldDir].xCoord;
          npc[newDir].yCoord = npc[oldDir].yCoord;
          // make respective directions not drawable and drawable
          npc[oldDir].drawable = 0;
          npc[newDir].drawable = 1;
          // make sure animation starts in a known state
          npc[newDir].currentFrame = 0;
          // now heading in this direction
          oldDir = newDir;
          printf("\n");
        }
        animate();
      } while ( ch != 'Q' );
      return 0;
    }
    
    
    $ gcc -std=c99 foo.c
    $ ./a.out 
    (20,20)Ll (19,20)Ls (18,20)Lr (17,20)Ll (16,20)Ls (15,20)Lr (14,20)Ll (13,20)Ls (12,20)Lr (11,20)Ll (10,20)Ls 
    (09,20)Rl (10,20)Rs (11,20)Rr (12,20)Rl (13,20)Rs (14,20)Rr (15,20)Rl (16,20)Rs (17,20)Rr (18,20)Rl (19,20)Rs 
    (20,20)Dl (20,21)Ds (20,22)Dr (20,23)Dl (20,24)Ds 
    (20,25)Ul (20,24)Us (20,23)Ur (20,22)Ul (20,21)Us (20,20)Ur (20,19)Ul (20,18)Us (20,17)Ur (20,16)Ul (20,15)Us (20,14)Ur (20,13)Ul (20,12)Us 
    (20,11)Ll (19,11)Ls (18,11)Lr (17,11)Ll (16,11)Ls (15,11)Lr 
    (14,11)Dl (14,12)Ds (14,13)Dr (14,14)Dl (14,15)Ds (14,16)Dr (14,17)Dl 
    (14,18)Rl (15,18)Rs 
    (16,18)Dl (16,19)Ds (16,20)Dr (16,21)Dl (16,22)Ds
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 09-26-2012, 05:20 AM
  2. Simple game with timing issues atm
    By Swarvy in forum Game Programming
    Replies: 1
    Last Post: 08-06-2009, 09:52 PM
  3. Functino Issues Ending Game
    By StoryGame464 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2009, 06:06 PM
  4. Major game issues
    By VOX in forum Game Programming
    Replies: 0
    Last Post: 01-18-2005, 07:40 AM
  5. any good way to write sprite/animation classes for a game?
    By compjinx in forum Game Programming
    Replies: 2
    Last Post: 03-28-2002, 12:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21