Click here to Skip to main content
15,887,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I found this code online with no explanation, i figured out how to use it but i could not understand the logic behind it. It works fine, the class is called input so i can simply do.

if(Input.Keypressed(Keys.Enter))
                {
                    //Do somethings
                    
                }


What I have tried:

public static Hashtable KeyBoard = new Hashtable();

        public static bool KeyPressed(Keys key)
        {
            if(KeyBoard[key] == null)
            {
                return false;
            }
            else
            {
                return (bool)KeyBoard[key];
            }

        }
        public static void ChangedState(Keys key, bool state)
        {
            KeyBoard[key] = state;
        }
Posted
Updated 19-May-20 0:30am
Comments
Richard MacCutchan 19-May-20 6:27am    
So what is your question?

1 solution

At a guess, I'm going to say that you found this snippet of code in this repository: GitHub - mirkoBastianini/Snake-Game: Snake game written in C#, with Visual Studio 2015.[^]. This works because the code is using something known as a game loop. What is happening is that there is a timer event that is triggering several times a second. Each time the timer event is fired, the event checks to see what key is pressed at that point.

This is the game logic at the timer tick:
C#
private void UpdateScreen(object sender, EventArgs e)
{
            //Controlla se è game over
            if (Settings.GameOver)
            {
                //Controlla se un tasto è premuto
                if (Input.KeyPressed(Keys.Enter))
                {
                    StartGame();
                }
            }
            else
            {
                if (Input.KeyPressed(Keys.Right) && Settings.direction != Direction.Left)
                    Settings.direction = Direction.Right;
                else if (Input.KeyPressed(Keys.Left) && Settings.direction != Direction.Right)
                    Settings.direction = Direction.Left;
                else if (Input.KeyPressed(Keys.Up) && Settings.direction != Direction.Down)
                    Settings.direction = Direction.Up;
                else if (Input.KeyPressed(Keys.Down) && Settings.direction != Direction.Up)
                    Settings.direction = Direction.Down;

                MovePlayer();
            }

            pbCanvas.Invalidate();
}
An alternative mechanism is to use the KeyPressed or KeyDown event (in WinForms) to detect when a key has been pressed and handle that.
 
Share this answer
 
Comments
Richard MacCutchan 19-May-20 7:04am    
Is this an admission that you are an expert at snake?
Pete O'Hanlon 19-May-20 7:24am    
So many answers, so many of them unsuitable.... :wink:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900