Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
I swear I followed this guy's tutorial to the letter; I have no errors in my code. When the window is opened, the player is instructed to press the spacebar in order to start the game, use the arrow keys to move the snake, and get the highest score. The intro label disappears and a piece of food appears in the window, but none of the arrow keys will move the snake.
There are 2 classes; Snake and Food, as well as the Windows Form. There are 3 event handlers in the form; Draw, KeyDown, and Load.
There is something you need to know about me before you read the code. My biggest obstacle in programming is my vision; I have 20/400 in my right eye, and light & hand motion in my left. In order to do anything on the computer, I need magnification of at least 300-450%. Please do not be surprised if the error you find is a simple one; imagine watching a YouTube video at 300+ magnification; it is very blurry! I am mostly going on what I can hear and what little I can see. I was able to decipher most of the text, but the chance that I missed something small yet important is very high.
Here is the code for the form:
	public partial class FRM_Snake : Form
	{
		Random foodPiece = new Random();
		Graphics g;
		Snake snake = new Snake();
		Food food;
		bool moveLeft = false;
		bool moveUp = false;
		bool moveRight = false;
		bool moveDown = false;
		int Score = 0;
		public FRM_Snake()
		{
			InitializeComponent();
			food = new Food(foodPiece);
		}
		private void FRM_Snake_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData == Keys.Space)
			{
				timer1.Enabled = true;
				LBL_Begin.Text = "";
				moveUp = false;
				moveDown = true;
				moveLeft = false;
				moveRight = false;
			}
			if (e.KeyData == Keys.Down && moveUp == false)
			{
				moveDown = true;
				moveUp = false;
				moveLeft = false;
				moveRight = false;
				snake.MoveDown();
			}
			if (e.KeyData == Keys.Up && moveDown == false)
			{
				moveDown = false;
				moveUp = true;
				moveLeft = false;
				moveRight = false;
				snake.MoveUp();
			}
			if (e.KeyData == Keys.Left && moveRight == false)
			{
				moveDown = false;
				moveUp = false;
				moveLeft = true;
				moveRight = false;
				snake.MoveLeft();
			}
			if (e.KeyData == Keys.Right && moveLeft == false)
			{
				moveDown = false;
				moveUp = false;
				moveLeft = false;
				moveRight = true;
				snake.MoveRight();
			}
		}
		private void FRM_Snake_Paint(object sender, PaintEventArgs e)
		{
			g = e.Graphics;
			food.drawFood(g);
			snake.DrawSnake(g);
		}
		private void FRM_Snake_Load(object sender, EventArgs e)
		{
			if (moveDown)
			{
				snake.MoveDown();
			}
			if (moveUp)
			{
				snake.MoveUp();
			}
			if (moveLeft)
			{
				snake.MoveLeft();
			}
			if (moveRight)
			{
				snake.MoveRight();
			}	this.Invalidate();
		}
		private void timer1_Tick(object sender, EventArgs e)
		{
			this.TSL_Score.Text = Convert.ToString(Score);
			if (moveDown)
				snake.MoveDown();
			if (moveUp)
				snake.MoveUp();
			if (moveLeft)
				snake.MoveLeft();
			if (moveRight)
				snake.MoveRight();
			//Collision Detection
			for (int i = 0; i < snake.MySnake.Length; i++)
			{
				if (snake.MySnake[i].IntersectsWith(food.foodPiece))
				{
					Score += 20;
					snake.growSnake();
					food.FoodLocation(foodPiece);
				}
			}
		}
		public void Collisoin()
		{
			for (int i = 1; i < snake.MySnake.Length; i++)
			{
				if (snake.MySnake[0].IntersectsWith(snake.MySnake[i]))
				{
					MessageBox.Show("Ouch! Quit biting yourself!\n Your total score is " + Score);
					Restart();
				}
			}
			//Use if you want game to be over when snake hits the edge of the screen
			if (snake.MySnake[0].X < 0 || snake.MySnake[0].X > 480)
			{
				MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
				Restart();
			}
			if (snake.MySnake[0].Y < 0 || snake.MySnake[0].Y > 480)
			{
				MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
				Restart();
			}
		}
		public void Restart()
		{
			Score = 0;
			this.TSL_Score.Text = "0";
			timer1.Enabled = false;
			this.LBL_Begin.Text = "Don't space out!\nPress Spacebar to begin";
			snake = new Snake();
		}
	}
</pre>
Here is the Snake class; I don't think the Food class has anything to do with my snake's lack of movement :)
<pre>	class Snake
	{
		private Rectangle[] mySnake;
		private SolidBrush myBrush;
		private int x;
		private int y;
		private int width;
		private int height;
		public Rectangle[] MySnake
		{
			get
			{
				return mySnake;
			}
		}
		//Constructor
		public Snake()
		{
			//Basic drawing components of the snake
			mySnake = new Rectangle[3];
			myBrush = new SolidBrush(Color.White);
			//The position of the snake on the game
			x = 16;
			y = 16;
			//Width and height of the snake
			width = 16;
			height = 16;
			//Give the snake a base so it can grow
			for (int i = 0; i &lt; mySnake.Length; i++)
			{
				mySnake[i] = new Rectangle(x, y, width, height);
				x -= 16;
			}
		}
		public void DrawSnake(Graphics g)
		{
			foreach (Rectangle rec in mySnake)
			{
				g.FillRectangle(myBrush, rec);
			}
		}
		public void DrawSnake()
		{
			for (int i = mySnake.Length - 1; i &gt; 0; i--)
			{
				mySnake[i] = mySnake[i - 1];
			}
		}
		public void MoveDown()
		{
			DrawSnake();
			mySnake[0].Y += 16;
		}
		public void MoveUp()
		{
			DrawSnake();
			mySnake[0].Y -= 16;
		}
		public void MoveLeft()
		{
			DrawSnake();
			mySnake[0].X -= 16;
		}
		public void MoveRight()
		{
			DrawSnake();
			mySnake[0].X += 16;
		}
		public void growSnake()
		{
			List&lt;Rectangle&gt; Rec = mySnake.ToList();
			Rec.Add(new Rectangle(mySnake[mySnake.Length - 1].X, mySnake[mySnake.Length - 1].Y, width, height));
			mySnake = Rec.ToArray();
		}
	}

http://www.youtube.com/user/TutorialHouseNz#p/a[^]
It doesn't help when the sample project you're squinting at cannot be found because the author no longer has a website, either :)
Posted
Updated 22-Jul-11 13:38pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Jul-11 17:49pm    
What tutorial do you mean?
--SA

1 solution

Hi, I the main bug I can find in your code is that in the timer tick routine you are not invalidating the form, which means the form will never be redrawn when the snake is moved.

To fix this just add the line 'this.Invalidate()' after you've moved the snake.

Here is what your timer_Tick function should look like (without the collision detection code).
private void timer1_Tick(object sender, EventArgs e)
{
    this.TSL_Score.Text = Convert.ToString(Score);
    if (moveDown)
        snake.MoveDown();
    if (moveUp)
        snake.MoveUp();
    if (moveLeft)
        snake.MoveLeft();
    if (moveRight)
        snake.MoveRight();

    this.Invalidate();
}


Also the 'FRM_Snake_Load' function is not needed, as this is only called once when the form is loaded and this was where you were invalidating the form to redraw the snake, but this is ineffective.

Hope this helps.
 
Share this answer
 

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