Need Help Fixing A Bug In Snake Game

Minato

So I've successfully created a Snake game that functions fine with the exception of one little thing that I don't know how to solve. Let me try to explain.

So the game works on a timer where each tick of the timer means moving each block of the snake in the specified direction. It works so that if you're moving right, you cannot move left (because then the head would intersect with the body and you'd lose). The same goes for the all the other possible directions. There is a direction variable that specifies the current direction of the snake. So the problem is that when you press the left key, it will check if the direction is set to right and if it is, nothing will happen. But if you were to press the down key and then the left key both within the same interval of time, then the snake will move left and you'll lose. Here's the code that handles the KeyDown event for the form.

    private void frmMain_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyData)
        {
            case Keys.Enter:
                if (lblMenu.Visible)
                {
                    lblMenu.Visible = false;
                    LoadSettings();
                    gameLoop.Start();
                }
                break;
            case Keys.Space:
                if (!lblMenu.Visible)
                    gameLoop.Enabled = (gameLoop.Enabled) ? false : true;
                break;
            case Keys.S:
                using (frmSettings f = new frmSettings())
                {
                    f.ShowDialog(this);
                }
                break;
            case Keys.Right:
                if (direction != Direction.Left)
                    direction = Direction.Right;
                break;
            case Keys.Down:
                if (direction != Direction.Up)
                    direction = Direction.Down;
                break;
            case Keys.Left:
                if (direction != Direction.Right)
                    direction = Direction.Left;
                break;
            case Keys.Up:
                if (direction != Direction.Down)
                    direction = Direction.Up;
                break;
        }
    }

Here's a download to the game if you want to experience the bug first-hand. Thank you for any help!

BradleyDotNET

What you have is actually a "feature". You want to be able to go down then left, but you are doing it within a timer tick, causing the "bug".

That means that you need to wait to change direction. Your code becomes:

if (direction != Direction.Right)
   intendedDirection = Direction.Left;

...

OnTimerTick()
{
    ...
    direction = intendedDirection;
}

Now, you can press as many keys as you want (that are valid) the last will take effect on the timer tick, which will then allow the next key press (left) to work correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need help fixing a game I made in Python

From Dev

Whack-A-Mole game with huge bug! Can I get some help fixing it?

From Dev

Simple console labirinth game: Need help to figure out a bug

From Dev

Need help in identifying and fixing SSLPeerUnverifiedException

From Dev

Need help fixing the syntax for this LEFT OUTER JOIN

From Dev

I need help fixing this hangman function

From Dev

Need help fixing strange cin behavior

From Dev

Need help fixing setInterval issue in JavaScript

From Dev

Need CSS/HTML help fixing navigation in IE

From Dev

I need help fixing this hangman function

From Dev

Need help fixing a segmentation fault (core dumped)

From Dev

Need help fixing my nginx server

From Dev

Snake game: fast response vs. collision bug

From Dev

need help fixing 500 error python/flask app

From Dev

Need help fixing my error on Notice: Array to string conversion in

From Dev

Need help fixing my implementation of RK4

From Dev

Need help fixing a vba Error '1004' after editing a section of code

From Dev

Need help fixing my error on Notice: Array to string conversion in

From Dev

Need help fixing CSS for a GTK theme for Gnome Shell

From Dev

Need help fixing top bar inside another div with scroll

From Dev

Need help fixing my isotope.js [jquery and html code]

From Dev

Need help fixing broken nautilus directory links on launcher

From Dev

I need help fixing my code - very fragile

From Dev

Need help fixing a random sentence generator that uses for loops and list's

From Dev

Need some help in fixing the Spark streaming dependency (Scala sbt)

From Dev

Need Help fixing incorrect output to console window.

From Dev

Need help fixing the output of this Higher Order Function.

From Dev

Need help to switch between to pictures in stickman game

From Dev

Fixing a bug in a For loop

Related Related

  1. 1

    Need help fixing a game I made in Python

  2. 2

    Whack-A-Mole game with huge bug! Can I get some help fixing it?

  3. 3

    Simple console labirinth game: Need help to figure out a bug

  4. 4

    Need help in identifying and fixing SSLPeerUnverifiedException

  5. 5

    Need help fixing the syntax for this LEFT OUTER JOIN

  6. 6

    I need help fixing this hangman function

  7. 7

    Need help fixing strange cin behavior

  8. 8

    Need help fixing setInterval issue in JavaScript

  9. 9

    Need CSS/HTML help fixing navigation in IE

  10. 10

    I need help fixing this hangman function

  11. 11

    Need help fixing a segmentation fault (core dumped)

  12. 12

    Need help fixing my nginx server

  13. 13

    Snake game: fast response vs. collision bug

  14. 14

    need help fixing 500 error python/flask app

  15. 15

    Need help fixing my error on Notice: Array to string conversion in

  16. 16

    Need help fixing my implementation of RK4

  17. 17

    Need help fixing a vba Error '1004' after editing a section of code

  18. 18

    Need help fixing my error on Notice: Array to string conversion in

  19. 19

    Need help fixing CSS for a GTK theme for Gnome Shell

  20. 20

    Need help fixing top bar inside another div with scroll

  21. 21

    Need help fixing my isotope.js [jquery and html code]

  22. 22

    Need help fixing broken nautilus directory links on launcher

  23. 23

    I need help fixing my code - very fragile

  24. 24

    Need help fixing a random sentence generator that uses for loops and list's

  25. 25

    Need some help in fixing the Spark streaming dependency (Scala sbt)

  26. 26

    Need Help fixing incorrect output to console window.

  27. 27

    Need help fixing the output of this Higher Order Function.

  28. 28

    Need help to switch between to pictures in stickman game

  29. 29

    Fixing a bug in a For loop

HotTag

Archive