Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Ideas To Avoid Infinite Loop?

loop

  • Please log in to reply
4 replies to this topic

#1 kumogoro

kumogoro

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:C++

Posted 11 May 2012 - 12:40 AM

hi guys,
I'm coding a snake game for my uni course using an API provided by our module leader. The code below shows how i'm moving the snake:

bool AppState::onKey(const KeyEvent& key_event)
{
p.snakeDir(key_event);
if(p.snakeDir(key_event) == 1 && key_event.key_state == 2)
   p.MoveLeft();

else if(p.snakeDir(key_event) == 0 && key_event.key_state == 2)
{
  p.MoveRight();
}
else if(p.snakeDir(key_event) == 2 && key_event.key_state == 2)
{
  p.MoveUp();
}
else if(p.snakeDir(key_event) == 3 && key_event.key_state == 2)
{
  p.MoveDown();
}
    switch( key_event.key )
{
	    case KeyEvent::KB_ESC_KEY:
		    application.exit();
		    break;
    }
    return true;
}

key_event.key_state = 2 represents the key being released and snakeDir just returns an int value relative to whichever arrow key you pressed. So while this all works nicely the snake only moves the once and i'm fresh out of ideas on how to avoid making an infinite loop to keep it moving until you push a different key. Any inspiration would be awesome! :)
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 11 May 2012 - 12:55 AM

Does the 'onKey' event continuously fired (get called) when a key keep pressed? I guess this 'onKey' event is fired continously. But you need to use 'key_event.key_state' value to decide that? What are the other values for key_state variable? Try to use other values than 2 for this variable.
  • 0

#3 kumogoro

kumogoro

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:C++

Posted 11 May 2012 - 01:05 AM

if you hold down an an arrow key onKey is indeed continuously fired. The reason for the 'key_event.key_state == 2' is that without it onKey is called for both the pressing and release of the key, when i only want the snake to move on key release, and to keep moving until you press a different key.

The only other value for key_state is 1 and this represents the key being pressed rather than released.
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 11 May 2012 - 01:08 AM

So I think you achieve this by change the direction only of the snake in onKey event. Then in a timer, you can just call the snake to move on the current direction.
  • 0

#5 kumogoro

kumogoro

    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Learning:C++

Posted 11 May 2012 - 01:58 AM

The MoveLeft() etc functions are where the change in the snake's direction actually occurs
void Player::MoveUp()
{
_snake_bit.pop_back();
Iterator pos = _snake_bit.begin();
Vector2D nh;
nh._y = (pos.get()._y + 20);
nh._x = (pos.get()._x);
cout << "UP" << endl;
_snake_bit.push_front(nh);
}

the snake is a linked list which takes this struct as its node data
struct Vector2D
{
int _x;
int _y;
Vector2D(int x = 0, int y = 0) : _x(x), _y(y) {};
};

However you're timer idea has given me some thoughts, thank you :) i'll post up if i get a solution
  • 0





Also tagged with one or more of these keywords: loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download