Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void timer1_Tick(object sender, EventArgs e) //Called every so many milliseconds depending on the interval which is set determined by the length of the snake (to increase difficulty of game, longer snake = faster update)
- {
- SnakeLengthLabel.Text = snakeLength.ToString();
- switch (changeToDirection) //Checks if the snake can move in the direction of the arrow key pressed
- {
- case "left":
- if (direction != "right")
- {
- direction = "left";
- }
- break;
- case "right":
- if (direction != "left")
- {
- direction = "right";
- }
- break;
- case "up":
- if (direction != "down")
- {
- direction = "up";
- }
- break;
- case "down":
- if (direction != "up")
- {
- direction = "down";
- }
- break;
- }
- switch (direction) //Changes snakes direction
- {
- case "left":
- snakeXPos -= 1;
- checkIFOffBoard();
- Box[snakeXPos, snakeYPos].BackColor = Color.Red;
- break;
- case "right":
- snakeXPos += 1;
- checkIFOffBoard();
- Box[snakeXPos, snakeYPos].BackColor = Color.Red;
- break;
- case "up":
- snakeYPos -= 1;
- checkIFOffBoard();
- Box[snakeXPos, snakeYPos].BackColor = Color.Red;
- break;
- case "down":
- snakeYPos += 1;
- checkIFOffBoard();
- Box[snakeXPos, snakeYPos].BackColor = Color.Red;
- break;
- }
- for (int i = 0; i <= 9; i++) //Checks if the snake has eaten a food
- {
- if (snakeXPos == foodXPos[i] && snakeYPos == foodYPos[i])
- {
- snakeLength += 1;
- SnakeLengthLabel.Text = snakeLength.ToString();
- newFood();
- }
- }
- for (int i = 1; i < 100; i++) //Checks if the snake has collided with itself
- {
- if (snakeXPos == snakeXPositions[i] && snakeYPos == snakeYPositions[i])
- {
- EndGameLabel.Text = "You Lost!" + Environment.NewLine + Environment.NewLine + "The snake collided with itself" + Environment.NewLine + Environment.NewLine + "Click to play again";
- endGame();
- }
- }
- if (snakeLength < 20) //Increases the snakes movement speed depending on its length (shorter timer interval = faster speed)
- {
- timer1.Interval = 500 - snakeLength * 25;
- }
- for (int i = 99; i >= 1; i--) // v Makes the snakes body follow the path the head of the snake took
- {
- if (i < snakeLength)
- {
- snakeXPositions[i + 1] = snakeXPositions[i];
- snakeYPositions[i + 1] = snakeYPositions[i];
- }
- else if (i > snakeLength)
- {
- snakeXPositions[i] = -1;
- snakeYPositions[i] = 0;
- }
- } // ^
- snakeXPositions[1] = snakeXPos;
- snakeYPositions[1] = snakeYPos;
- for (int y = 0; y < 30; y++) // v Updates snakes position
- {
- for (int x = 0; x < 30; x++)
- {
- if (Box[x, y].BackColor != Color.Blue)
- {
- Box[x, y].BackColor = Color.Black;
- }
- if (BoxBorders.Checked == true)
- {
- Box[x, y].BorderStyle = BorderStyle.FixedSingle;
- }
- else
- {
- Box[x, y].BorderStyle = BorderStyle.None;
- }
- }
- }
- for (int i = 1; i < 100; i++)
- {
- if (snakeXPositions[i] != -1)
- {
- Box[snakeXPositions[i], snakeYPositions[i]].BackColor = Color.Red;
- }
- } // ^
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement