Game Timing?

This is a discussion on Game Timing? within the Game Programming forums, part of the General Programming Boards category; Hello all! I was hoping someone could help me with a problem. I have only been programming for a while ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    118

    Game Timing?

    Hello all! I was hoping someone could help me with a problem. I have only been programming for a while now so the answer is probably easy, and I am probably doing things all wrong. Anyways, here I go. I created a simple game with an alian ship that travels from left to right at the top of the screen. With each pass it gets closer to the player. The players ship is a sprite that can travel left and right with the arrow keys. The problem is when I hit the fire button, I have to animate the missle (which is a single pixel at this moment) by using a loop. So, when the missle is fired the program loops to animate the projectile which is fine, but the game haults all action until the missle has hit something, or goes off the screen with a miss. How can I animate a single pixel missle without using a loop that stops game play? Or, if I have to use a loop, how can I position it in the main game loop and have it work out properly? Thanks for your time reading this and I hope someone can.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Try something like this:

    Code:
    //for a single pixel blast
    struct LaserBlast  //better as a class but for I'm going for brevity
    {
        bool bShotFired;
        int iX;
        int iY;
    }; 
    
    LaserBlast lb;
    
    //in game/level initialization
    lb.bShotFired=false;
    lb.iX=0;
    lb.iY=0;
    
    //in your input routine, when the player hits the "fire" key
    void FireLaserBlast(void)
    {
       lb.bShotFired=true;
       lb.iX=Ship.iX/2;  //fired from center of ship
       lb.iY=Ship.iY;     //start as top of ship
    }
    
    //this function would go in your main game loops blitting code
    void DrawLaserBlast(void)
    {
       if(lb.bShotFired)
       {
          lb.iY--; //move the laserblast up towards the top of the screen
          Blt(lb,lb.iX,lb.iY); //of course this is generic, depending on what
                                    //graphics API you use, you get the idea
       }
    }
    
    //see if you hit anything
    void CheckForLaserBlastCollision(void)
    {
       if(lb.bShotFired)
       {
         POINT pt;
         pt.lX=lb.iX;
         pt.lY=lb.iY;
         //cycle through all the enemies on the screen
         for(int i=0;i<TotalEnemies;i++)
         {
            if(PtInRect(&Enemy[i].rcRect,&pt)
            {
               KillEnemy(i);
               IncreaseScore();
               lb.bShotFired=false;
            }
          //if you haven't hit an enemy check to see if you hit the top
          if(lb.iY==0) lb.bShotFired=false;
         }
       }
    }

  3. #3
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    In case you don't understand that code, I can explain why this is happening. Lets say your main() has a loop that gets input and moves the player ship accordingly. It also test for the fire button, when that is hit your program branches to fire(). The program will halt the loop in main() untill fire() returns. Therefore if you have fire() make your missile move untill it hits something you won't be able to move the ship.

    Try having a loop that does the following:

    1. Get user input (left, right, or fire)
    2. Draws the ship and missile (if there is a missile)

    Let me know if I did not explain it good enough.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    I kind of understand. I think your explanation is fine, it's just my knowledge is not quit up to par yet. I do think I understand what your getting at. I thought may be I could give you a little more info on what I am doing. Here is how my input from keyboard is done:
    Code:
    if(d=='f')
    {
    	reseta=a;
    	resetb=b;
    	PutSprite(a,b,sprite);
    	for(;;)
    		{
    		colour=Peek(a+10,b-1);
    		if(colour==4)
    			{
    			gotoxy(1,1);
    			score=score+1;
    			printf("%i!",score);
    			break;
    			}
    		if(colour==0)
    			{
    			SetPixel(a+10,b-1,5);
    			WaitForVerticalRetrace();
    			SetPixel(a+10,b-1,0);
    			b=b-1;
    			if(b==0)
    				{
    				break;
    				}
    			}
    		}
    	a=reseta;
    	b=resetb;
    }
    The above code is used in a loop with the direction keys inputed in the same way (I didn't want to include it all). My main game loop is outside of this loop, and that is where all of my alien enemy ships are located. The problem is when the "F" key is pressed it sarts in this loop and stops all other motion. Could I put this loop some how in the main game loop and have it run through the program at the same time my alien ships are being drawn? I hope this showed a little insight on how I am going about this. There is probably another way, but I don't know. Thanks lots for your time to read this, and for the help.


    Code tags added by Hammer
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    Sorry to reply again to my own posting, but the above WAS spaced properly! I don't know what happened! Sorry - It was readable. I hope you can still make sense of it.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  6. #6
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    Use the [ CODE ] /* insert code here */ [/ CODE ] tags.
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    Sorry - Im new to this board. I will use the tags next time.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  8. #8
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    You could edit your post and put them in now
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  9. #9
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    In order to have your space ship drawn while your main loop is still going, you need to have your main loop draw the space ****. Or draw one frame.

    Code:
    while(!quit)
    {
      if(UserHitSpace())
        if(bullet.exist != true)
        {
          bullet.exist = true;
          bullet.x = ship.x; bullet.y = ship.y;
        }
    
      ClearScreen();
      DrawFrame();
    }
    
    DrawFrame()
    {
      if(bullet.exist == true)
      {
        blit(....); /* Draw your sprite for the bullet */
      }
    
      blit(...); /* Draw your spaceship */
    }

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    I finally got it working, but now a few other problems came about. First off, thanks for all your help. Without it I would have been scratching my head for weeks. The answer was so obvious I feel stupid. I quess that's the way you learn - to ask stupid questions. Speaking of stupid questions, may be you guys could help me out with another problem along the same lines. Now that my ship does not freeze when the fire button is pressed, I want to be able to fire numerous times before the missle hits a sprite or the top of the screen. All goes well when I let the missle finish it's path until it ends, but if I press fire before the missle has ended, I get a copy of my main character sprite at the exact location as my missle is at the time pressed. It's another problem that came after I fixed what I thought would be my last obstacle. If anyone has any answers, I would appreciate the help, again. Thanks!
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  11. #11
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    How are you handling the missiles? Using an array of structures to keep track of them, or a linked list? I am sure your problem is somewhere along there.

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    Actually I am just drawing the pixel on the screen at the proper location each pass of the main game loop. So that when a collision is detected, the sprite is erased. When the missle goes off the screen, the missle loop stops and returns to the main game loop. I used a variable that could be either 1 or 0. When it is 1 it means a certain range of code can be accessed with an if(fire==1) sort of thing. If it equals 0 then the range of code is cut out of the main loop. Pretty basic, and probably all wrong, but it works pretty good except for the fact I can't fire multiple times without the main screen sprite redrawing where the missle is located at the time the fire button is pressed. I have some pretty strange problems. My problems are many because I have not read any books on programming games, so I am winging it with the little C knowledge I have. I think it's time I read some books eh? Anyways, if you can help any suggestions would be appreciated - thanks.
    "The distinction between past, present and future is only an illussion, even if a stunning one."
    -Albert Einstein

  13. #13
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    How are you keeping track of each missile and its location?

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    118
    The missles location is the given by the variables for the main ships sprite, just offset a little to put the missle in the center. I do not keep track of the missles by any means. The missle just uses the loop until it goes off the screen or hits something. I hope that clarifies it a little. Am I doing it wrong? Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. Open GL - glTranslate + Game Timing
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 10-24-2006, 03:20 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM

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