3 Replies - 3153 Views - Last Post: 15 August 2011 - 05:41 PM Rate Topic: -----

#1 xactionbobx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-August 11

Control issue with snake game in C#

Posted 14 August 2011 - 04:13 PM

Hello All! Long time reader, first time poster!

I have recently written a snake clone in c#. All is going well, and I'm having a great time working through it and learning. I should mention that although I have been programming for a while, this is my first attempt at a game that includes System.Drawing for the animation, and keyboard movement.

The problem I am having is that when the snake is moving to the right, if I press down the left quickly, to bring the snake down one line and move the opposite direction, my game detects that the snake has collided with itself, and calls the reset method (game over) I have tried adding different conditions to my if statements for key press detection, to no avail. Code is posted below, thanks in advance for any/all help!

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //Starts the game when the spacebar is pressed.
            if (e.KeyData == Keys.Space)
            {
                timer1.Enabled = true;
                lblSpace.Visible = false;
                up = false;
                down = false;
                right = true;
                left = false;
            }
            //Each of my directions is a boolean.  up, down, left, right.  Here I check to make sure that up
            //(the opposite direction) is false before changing the variable for down to true.  I believe this
            //is where my problem originates.
            if (e.KeyData == Keys.Down && up == false)
            {
                down = true;
                up = false;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Up && down == false)
            {
                down = false;
                up = true;
                right = false;
                left = false;
            }
            if (e.KeyData == Keys.Left && right == false)
            {
                down = false;
                up = false;
                right = false;
                left = true;
            }
            if (e.KeyData == Keys.Right && left == false)
            {
                down = false;
                up = false;
                right = true;
                left = false;
            }
        }




If you would like to install the game and try it to get a better understanding of my problem, please visit www.action-bob.com and click "snake" to download and install.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Control issue with snake game in C#

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Control issue with snake game in C#

Posted 14 August 2011 - 04:51 PM

Your problem is that the keypresses are being processed faster than your movement routine. An easy way to fix this is to create a boolean that indicates if you have processed a key press during this move cycle. In the keypress routine check that it is false and set it to true. In the movement routine set it to false. This way you will process (at most) one change of direction per movement.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Control issue with snake game in C#

Posted 14 August 2011 - 05:34 PM

Another obvious issue is the use of global variables for down, up, right, left instead of raising events for the movement.

You have events for keypresses and as you can see they are near instantaneous response.

But then you probably have some type of polling timer or loop for the movement. So as Momerath said, you are not handling movement anywhere near as fast as you are handling keypresses.

I would call this a case of writing a "process driven" application, instead of writing an "event driven" application; which is what most C# apps are meant to be.

Personally I would consider the right thing to do would be to fix the architecture and make it a proper event-driven C# application. Sure you can try to band-aide it with a bool. Without knowing what else is in your code its hard to say how well that will work. Or how much the program will become patches, on top of patches, all to try to overcome improper design.


Intermediate:
The tutorials below walk through making an application including inheritance, custom events and custom controls, object serialization and more.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Was This Post Helpful? 1
  • +
  • -

#4 xactionbobx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-August 11

Re: Control issue with snake game in C#

Posted 15 August 2011 - 05:41 PM

Thanks for the tips guys. Momerath, I went with your suggestion, and it works flawlessly, thank you. tlhIn'toq, your suggestions were also helpful in helping me to realize where the mistakes came from, and I am currently refactoring now. I appreciate your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1