none
Snake Game collision checking help RRS feed

  • General discussion

  • I'm nearly done with a snake game. I found the 5 part tutorial here: http://www.youtube.com/user/TutorialHouseNz#p/search/0/mLIB60wG_AI

    Unfortunately, I cannot get the collision checking to work; the snake can intersect itself without dying. Also, I'd like the snake to pass through walls. For example, if it began to pass through the right side of hte screen, it will begin to appear on the left side of the screen. Instead, it goes off into infinity. 

    The tutorial author included collision checking for the edges of the screen, but that code doesn't work, either. I'd like to share the code with classmates next semester, so if you could please help me with both, that would be great!

    Any help would be awesome! I will send you my whole project; it was created in MS Visual Studio 2010.

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Snake
    {
    	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)
    		{
    		}
    
    		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);
    				}
    			}
    
    			this.Invalidate();
    		}
    
    		public void Collisoin()
    		{
    			for (int i = 1; i < snake.MySnake.Length; i++)
    			{
    				if (snake.MySnake[0].IntersectsWith(snake.MySnake[i])) //Snake collides with itself
    				{
    					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[1].X < 0 || snake.MySnake[1].X > 480)
    			{
    				//Use if you want the snake to pass through one side of the screen and come out the opposite side
    				snake.MySnake[1].X = snake.MySnake[1].X + 480;
    				//Use if you want the snake to die when it hits the edge of the screen
    				//MessageBox.Show("Haha! You're not a worm;\nyou can't chew through walls! Your total score is " + Score);
    				//Restart();
    			}
    			if (snake.MySnake[1].Y < 0 || snake.MySnake[1].Y > 480)
    			{
    				//Use if you want the snake to pass through one side of the screen and come out the opposite side
    				snake.MySnake[1].Y = snake.MySnake[1].Y + 480;
    				//Use if you want the snake to die when it hits the edge of the screen
    				//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();
    		}
    	}
    }
    
    


     

    • Moved by CoolDadTxModerator Wednesday, August 10, 2011 1:24 PM Not IDE related (From:Visual C# IDE)
    Sunday, July 31, 2011 3:56 AM

All replies

  • It looks like you are not calling your Collision() method inside of the timer1_Tick. Insert it in the following way:

     

    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.Collisoin();
    
    			//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);
    				}
    			}
    
    			this.Invalidate();
    		}


    Thursday, August 11, 2011 3:37 PM