Advertisement
MrVeiran

snake

Mar 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. 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)
  2.         {
  3.             SnakeLengthLabel.Text = snakeLength.ToString();
  4.  
  5.             switch (changeToDirection) //Checks if the snake can move in the direction of the arrow key pressed
  6.             {
  7.                 case "left":
  8.                     if (direction != "right")
  9.                     {
  10.                         direction = "left";
  11.                     }
  12.                     break;
  13.                 case "right":
  14.                     if (direction != "left")
  15.                     {
  16.                         direction = "right";
  17.                     }
  18.                     break;
  19.                 case "up":
  20.                     if (direction != "down")
  21.                     {
  22.                         direction = "up";
  23.                     }
  24.                     break;
  25.                 case "down":
  26.                     if (direction != "up")
  27.                     {
  28.                         direction = "down";
  29.                     }
  30.                     break;
  31.             }
  32.             switch (direction) //Changes snakes direction
  33.             {
  34.                 case "left":
  35.                     snakeXPos -= 1;
  36.                     checkIFOffBoard();
  37.                     Box[snakeXPos, snakeYPos].BackColor = Color.Red;
  38.                     break;
  39.                 case "right":
  40.                     snakeXPos += 1;
  41.                     checkIFOffBoard();
  42.                     Box[snakeXPos, snakeYPos].BackColor = Color.Red;
  43.                     break;
  44.                 case "up":
  45.                     snakeYPos -= 1;
  46.                     checkIFOffBoard();
  47.                     Box[snakeXPos, snakeYPos].BackColor = Color.Red;
  48.                     break;
  49.                 case "down":
  50.                     snakeYPos += 1;
  51.                     checkIFOffBoard();
  52.                     Box[snakeXPos, snakeYPos].BackColor = Color.Red;
  53.                     break;
  54.             }
  55.  
  56.             for (int i = 0; i <= 9; i++)    //Checks if the snake has eaten a food
  57.             {
  58.                 if (snakeXPos == foodXPos[i] && snakeYPos == foodYPos[i])
  59.                 {
  60.                     snakeLength += 1;
  61.                     SnakeLengthLabel.Text = snakeLength.ToString();
  62.                     newFood();                    
  63.                 }
  64.             }          
  65.  
  66.             for (int i = 1; i < 100; i++) //Checks if the snake has collided with itself
  67.             {
  68.                 if (snakeXPos == snakeXPositions[i] && snakeYPos == snakeYPositions[i])
  69.                 {
  70.                     EndGameLabel.Text = "You Lost!" + Environment.NewLine + Environment.NewLine + "The snake collided with itself" + Environment.NewLine + Environment.NewLine + "Click to play again";
  71.                     endGame();
  72.                 }
  73.             }
  74.  
  75.             if (snakeLength < 20) //Increases the snakes movement speed depending on its length (shorter timer interval = faster speed)
  76.             {
  77.                 timer1.Interval = 500 - snakeLength * 25;
  78.             }
  79.            
  80.             for (int i = 99; i >= 1; i--) // v Makes the snakes body follow the path the head of the snake took
  81.             {
  82.                 if (i < snakeLength)
  83.                 {
  84.                     snakeXPositions[i + 1] = snakeXPositions[i];
  85.                     snakeYPositions[i + 1] = snakeYPositions[i];
  86.                 }
  87.                 else if (i > snakeLength)
  88.                 {
  89.                     snakeXPositions[i] = -1;
  90.                     snakeYPositions[i] = 0;
  91.                 }
  92.             }                           // ^
  93.  
  94.             snakeXPositions[1] = snakeXPos;            
  95.             snakeYPositions[1] = snakeYPos;
  96.  
  97.             for (int y = 0; y < 30; y++) // v Updates snakes position
  98.             {
  99.                 for (int x = 0; x < 30; x++)
  100.                 {
  101.                     if (Box[x, y].BackColor != Color.Blue)
  102.                     {
  103.                         Box[x, y].BackColor = Color.Black;
  104.                     }
  105.  
  106.  
  107.                     if (BoxBorders.Checked == true)
  108.                     {
  109.                         Box[x, y].BorderStyle = BorderStyle.FixedSingle;
  110.                     }
  111.                     else
  112.                     {
  113.                         Box[x, y].BorderStyle = BorderStyle.None;
  114.                     }
  115.                        
  116.                 }
  117.             }
  118.  
  119.             for (int i = 1; i < 100; i++)
  120.             {
  121.                 if (snakeXPositions[i] != -1)
  122.                 {
  123.                     Box[snakeXPositions[i], snakeYPositions[i]].BackColor = Color.Red;
  124.                 }
  125.  
  126.             }                           // ^
  127.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement