4 Replies - 706 Views - Last Post: 22 July 2015 - 06:03 AM Rate Topic: -----

#1 respect7   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 16-October 12

User input 2 buttons at the same time

Posted 20 July 2015 - 02:02 PM

So I made the snake game from the project list, which was an awesome experience, learned a lot. The game is finished, even did levels with obstacles but also freemode.

I do have 1 tiny lil bug, when say the snake moves from left to right on the screen and say you want to go left. You would first need to press Down arrow and then left. Works correctly, but when your press Down arrow and left at the same time within 1 tick, it registers as if the snake hits itself which results in gameover.

Currently I have set the user controls like so:

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {            
            switch (e.KeyData)
            {
                case Keys.Down: if (up == false && down == false)
                    {                       
                        up = false;
                        left = false;
                        right = false;
                        down = true;
                    }
                    else { return; }
                    break;
                case Keys.Up: if (down == false && up == false)
                    {
                        down = false;                        
                        left = false;
                        right = false;
                        up = true;
                    }
                    else { return; }
                    break;
                case Keys.Left: if (right == false && left == false)
                    {
                        down = false;
                        up = false;                       
                        right = false;
                        left = true;
                    }
                    else { return; }
                    break;
                case Keys.Right: if (left == false && right == false)
                    {
                        down = false;
                        up = false;
                        left = false;
                        right = true;
                    }
                    else { return; }
                    break;
                default: return;
            }
        }

        private void MoveDown()
        {
            snake.DrawSnake();
            snake.SnakeRectangle[0].Y += 10;
        }

        private void MoveUp()
        {
            snake.DrawSnake();
            snake.SnakeRectangle[0].Y -= 10;
        }

        private void MoveLeft()
        {
            snake.DrawSnake();
            snake.SnakeRectangle[0].X -= 10;
        }

        private void MoveRight()
        {
            snake.DrawSnake();
            snake.SnakeRectangle[0].X += 10;
        }
    }

        private void tmrSnake_Tick(object sender, EventArgs e)
        {  
            if (up == true) { MoveUp(); }
            if (down == true) { MoveDown(); }
            if (left == true) { MoveLeft(); }
            if (right == true) { MoveRight(); }
        }    



So basicly I think that after every button press I need to set a wait 1 tick before the user can press the next button. I am just not sure on how I can achieve that in a nice way. Also I made it so the user can set the interval between ticks so that they can change the speed of the snake.

Is This A Good Question/Topic? 0
  • +

Replies To: User input 2 buttons at the same time

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: User input 2 buttons at the same time

Posted 21 July 2015 - 06:38 AM

Instead of using 4 Booleans up, down, left, right, your should just have a single enum that can have one of 4 values. Ex:
enum Direction { Up, Right, Down, Left };


(Forgive the ordering of directions. It's a habit I built up when I was learning to program on a Commodore 64 and that ordering of directions matches up nicely with joystick inputs and bitmaps.)

Next one you an enum, for every key down event you get, put an enum value into a Queue.

And then lastly, for every tick in your event loop, pull a direction from the queue.
Was This Post Helpful? 0
  • +
  • -

#3 respect7   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 16-October 12

Re: User input 2 buttons at the same time

Posted 21 July 2015 - 02:23 PM

Sounds like something that makes sense, thanks a lot I am gonna change the code and will let you know if I managed to get it to work
Was This Post Helpful? 0
  • +
  • -

#4 respect7   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 16-October 12

Re: User input 2 buttons at the same time

Posted 21 July 2015 - 09:28 PM

I am going around this the wrong way I am sure, but to show I have been trying and experimenting using queue and and enum for the user input I will show my embarassing code :) :

Queue<Enum> queue = new Queue<Enum>();

        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Down: if (GameSettings.direction != GameSettings.Direction.up)
                    {
                        queue.Enqueue(GameSettings.direction = GameSettings.Direction.down);
                    }
                    break;
                case Keys.Up: if (GameSettings.direction != GameSettings.Direction.down)
                    {
                        queue.Enqueue(GameSettings.direction = GameSettings.Direction.up);
                    }
                    break;
                case Keys.Left: if (GameSettings.direction != GameSettings.Direction.right)
                    {
                        queue.Enqueue(GameSettings.direction = GameSettings.Direction.left);
                    }
                    break;
                case Keys.Right: if (GameSettings.direction != GameSettings.Direction.left)
                    {
                        queue.Enqueue(GameSettings.direction = GameSettings.Direction.right);
                    }
                    break;
            }

class GameSettings
    {    
        public enum Direction
        {
            up,
            down,
            left,
            right
        }
        
        public static int SnakeSpeed { get; set; }
        public static int Score { get; set; }
        public static Direction direction { get; set; }

        public GameSettings()
        {           
            SnakeSpeed = 7;
            Score = 0;
            direction = Direction.right;                            
        }
    }

private void UpdatePb()
        {       
            if (queue.Count > 0)
            {
                queue.Dequeue();
            }

            if (GameSettings.direction == GameSettings.Direction.down)
            {
                snake.DrawSnake();
                snake.SnakeRectangle[0].Y += 10;
            }
            if (GameSettings.direction == GameSettings.Direction.up)
            {
                snake.DrawSnake();
                snake.SnakeRectangle[0].Y -= 10;
            }
            if (GameSettings.direction == GameSettings.Direction.left)
            {
                snake.DrawSnake();
                snake.SnakeRectangle[0].X -= 10;
            }
            if (GameSettings.direction == GameSettings.Direction.right)
            {
                snake.DrawSnake();
                snake.SnakeRectangle[0].X += 10;
            }


My update function gets called every tick, interval depends on whatever snakespeed is set to by the user. Anyhow I am doing something completely wrong because even if I delete the part where I Dequeue(), the snake will still go the direction I want it to, I need to add them to the queue in a different way and maybe it's just because I am tired, I have been awake for way to long but I wanted to get this to work, but I can't see the solution.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7244
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: User input 2 buttons at the same time

Posted 22 July 2015 - 06:03 AM

You are throwing away the return value of Dequeue().

You shouldn't really need any global variables. You effectively have them by your use of static with your GameSettings class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1