// Creates the head of the snake public snakeBody createHead(Point point) { snakeBody body = new snakeBody(); body.Image = Image.FromFile(Application.StartupPath + @"\Images\body.jpg"); body.Location = point; body.Name = "Head"; body.Size = new Size(20, 20); body.SizeMode = PictureBoxSizeMode.AutoSize; gamePanel.Controls.Add(body); return(body); }
// Creates one new body segment of the snake private snakeBody createBodyPart(Point point, snakeBody prevBody) { snakeBody body = new snakeBody(); body.Image = Image.FromFile(Application.StartupPath + @"\Images\body.jpg"); body.Location = point; body.Name = "Body"; body.Size = new Size(20, 20); body.SizeMode = PictureBoxSizeMode.AutoSize; body.prev = prevBody; gamePanel.Controls.Add(body); return(body); }
// The actual start playing function private void startGameButton_Click(object sender, EventArgs e) { // Cleanup from any previous rounds by making everything invisible for (int i = 1; i < snakeWholeBody.Count(); i++) { snakeWholeBody[i].Visible = false; } // make sure snake is alive dead = false; // Clear previous forbidden locations forbiddenLocations.Clear(); // reset intentions to defaults intentionsList = new List <string> { "up", "up", "up", "up", "up", "up", "up" }; startGameButton.Visible = false; // reset snake body and previous inputs to default snakeWholeBody = new List <snakeBody> { }; prevIntention = "up"; prevMove = "up"; intention = "up"; // Clean up the tick event handelers from previous rounds // if the first time nothing happens. On subsequent loops // this prevents the speed from doubleing each time timer.Tick -= new EventHandler(TimerEventProcessor); // reset score score = 0; scoreLabel.Text = "Score: " + score; // Create initial Snake Point startPoint = new Point(400, 200); snakeBody head = createHead(startPoint); snakeWholeBody.Add(head); startPoint.Y += 20; snakeBody body1 = createBodyPart(startPoint, head); snakeWholeBody.Add(body1); startPoint.Y += 20; snakeBody body2 = createBodyPart(startPoint, body1); snakeWholeBody.Add(body2); startPoint.Y += 20; snakeBody body3 = createBodyPart(startPoint, body2); snakeWholeBody.Add(body3); startPoint.Y += 20; snakeBody body4 = createBodyPart(startPoint, body3); snakeWholeBody.Add(body4); startPoint.Y += 20; snakeBody body5 = createBodyPart(startPoint, body4); snakeWholeBody.Add(body5); startPoint.Y += 20; snakeBody body6 = createBodyPart(startPoint, body5); snakeWholeBody.Add(body6); prevTail = body6; //Initialize first powerup prevPowerup = generatePowerup(); // Sets the timer interval to 0.08 seconds. timer.Tick += new EventHandler(TimerEventProcessor); timer.Interval = 80; timer.Enabled = true; timer.Start(); }
// This function uses the most recent intention from the user to move the sanke private void move() { // temporary variable to mark the snakes next location as "forbidden" on the // next loop. This is to cuase a game over whe the head touches the tail List <Point> tempLocations = new List <Point>(); // Iterate though each part of the snake and move it based on the appropriate // intention ftom the Intention List for (int i = 0; i < snakeWholeBody.Count(); i++) { snakeBody piece = snakeWholeBody[i]; string direction = intentionsList[i]; // Switch case used to increment direction. 20 is used to make // sure the snake stay on the grid switch (direction) { case "left": piece.Left -= 20; break; case "right": piece.Left += 20; break; case "up": piece.Top -= 20; break; case "down": piece.Top += 20; break; default: piece.Top -= 20; break; } // Update temporary Locations tempLocations.Add(piece.Location); } // Checks to see if the snake is out of bounds or if the head has // touched its body (i.e. forbidden locations) if ((snakeWholeBody[0].Location.Y <= 0) || (snakeWholeBody[0].Location.Y >= 460) || (snakeWholeBody[0].Location.X <= 0) || (snakeWholeBody[0].Location.X >= 780) || forbiddenLocations.Contains(snakeWholeBody[0].Location)) { // Remove the head from teh screen for future rounds snakeWholeBody[0].Visible = false; // Reset forbidden locations and intentions to the defaults forbiddenLocations = new List <Point>(); intentionsList = new List <string> { "up", "up", "up", "up", "up", "up", "up" }; // Update to allow Game Over to trigger dead = true; } // Checks for Powerups if (snakeWholeBody[0].Location == powerupLocation) { // updates forbidden locations for next move forbiddenLocations = tempLocations; // Generates new Powerup prevPowerup = generatePowerup(); //Iterates score score += 10; scoreLabel.Text = "Score: " + score.ToString(); // Creates new tail piece, adds it to the snake and extends the intentions list prevTail = createBodyPart(prevTailPrevLocation, prevTail); snakeWholeBody.Add(prevTail); intentionsList.Add(intentionsList[intentionsList.Count() - 1]); } else { // updates forbidden locations for next move forbiddenLocations = tempLocations; } // Saves the current move and Tal location for next move prevMove = intentionsList[0]; prevTailPrevLocation = prevTail.Location; // Updates intention List intentionsList.RemoveAt(intentionsList.Count() - 1); intentionsList.Insert(0, intention); }