Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with C++ Snake Game

Options
  • 07-03-2003 4:51pm
    #1
    Registered Users Posts: 455 ✭✭


    I've been coding an MS-DOS Snake game like the one you've probably seen on many a Nokia mobile phone, but I'm having some trouble with unwanted delays midgame.

    Every time the player pushes a button, be it a valid ingame direction or a button bound to no function in the game, the movement speed of the snake becomes slow and jagged for a second or two before suddenly returning to normal speed.

    I'm using the following function to create a delay each game loop (this regulates the speed at which the snake moves, and thus the game difficulty):

    void Delay()
    {
    unsigned long StartTime = GetTickCount();
    while (StartTime + DelayValue > GetTickCount())
    {
    if (kbhit())
    {<< input code goes here >>}
    }
    }

    DelayValue is defined as 60 at the start of the program. I've tried slowing it down, and the jagged delay becomes less noticeable then, but the game also becomes too easy, since its hard to crash a sluggishly moving snake into a wall unless you've been sniffing paint :)

    Anyone have any idea what could be the problem here?


Comments

  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Try bumping up the tickcount (or dropping the StartTime) in the block controlled by kbhit(). Then the delay caused by processing that will be offset, and on balance the game should seem smoother.

    It may be necessary to do one such bump for all actions in response to kbhit(), and another if it is a key you react to.


  • Registered Users Posts: 1,931 ✭✭✭Zab


    There is nothing really wrong with your code. It does allow for slightly longer delay periods ( if a kbhit is registered late in the period and your input code is very slow ), but, unless your input code is very slow indeed, I doubt it would be noticable. Have you verified that the problem is in the Delay function? ( which is a strange place to have input code anyway! ) It may be your response to the input that is causing a delay, later on in the game loop.

    Zab.


  • Registered Users Posts: 455 ✭✭Lyconix


    Talliesin, thanks for the suggestion but I had no luck with that...

    I tried removing all of the input code, kbhit and all, from the Delay function earlier, leaving only the empty While loop intact... but whenever I pressed a key, the snake movement slowed down for a second or two then caught up to normal, even though there was no input routine to pick up the keypress!

    :confused:


  • Closed Accounts Posts: 285 ✭✭marauder


    Like Zab said,, this is a strange place for this.

    Put the long StartTime = GetTickCount(); right at the start of your game loop



    Put while ((GetTickCount())-StartTime < 33); at the end of the game loop.
    This way the timer test delays long enough to meet your required frame rate. (1/30fps=33ms)

    This will account for all delays in your code, not just the keyboard handling....

    You should also try to cut down on the function call overhead where you can....


  • Registered Users Posts: 455 ✭✭Lyconix


    Thanks for the advice marauder, I altered the code as suggested and removed the delay function - but unfortunately, the delay remains :confused:

    I wasn't aware that a little inefficient coding could have that dramatic an effect on a MS-DOS console window app such as this - seems that wasn't the case this time.

    I doublechecked the gameloop - now that I removed the Delay call, no functions are called apart from a function that makes a new food item when the snake eats one.

    Any other ideas?


  • Advertisement
  • Closed Accounts Posts: 285 ✭✭marauder


    You are using the delay to modify the speed of the snake and set the difficulty...

    Try separating the movement speed from delay that controls the framerate. So instead of redrawing faster to increase difficulty, increase how much the snake moves each frame and keep the framerate constant.


Advertisement