Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace SnakeNN
  12. {
  13.     public partial class MainForm : Form
  14.     {
  15.         private List<Circle> Snake = new List<Circle>();
  16.         private Circle food = new Circle();
  17.         public MainForm()
  18.         {
  19.             InitializeComponent();
  20.  
  21.             new SnakeSettings();
  22.  
  23.             gameTimer.Interval = 1000 / SnakeSettings.Speed;
  24.             gameTimer.Tick += UpdateScreen; //wtf is this?
  25.             gameTimer.Start();
  26.  
  27.             StartGame();
  28.         }
  29.  
  30.         private void StartGame()
  31.         {
  32.             new SnakeSettings();
  33.             Snake.Clear(); // clear the List
  34.             Circle head = new Circle();
  35.             head.X = 10;
  36.             head.Y = 5;
  37.             Snake.Add(head);
  38.  
  39.             lblScore.Text = SnakeSettings.Score.ToString();
  40.             lblGameOver.Visible = false;
  41.             GenerateFood();
  42.         }
  43.  
  44.         // Generate random food
  45.         private void GenerateFood()
  46.         {
  47.             int maxXPos = pbCanvas.Size.Width / SnakeSettings.Width;
  48.             int maxYPos = pbCanvas.Size.Height / SnakeSettings.Height;
  49.  
  50.             Random random = new Random();
  51.             food = new Circle();
  52.             food.X = random.Next(0, maxXPos);
  53.             food.Y = random.Next(0, maxYPos);
  54.         }
  55.  
  56.         private void UpdateScreen(object sender, EventArgs e)
  57.         {
  58.             //Check for Game Over
  59.             if (SnakeSettings.GameOver)
  60.             {
  61.                 //Check if Enter is pressed
  62.                 if (SnakeInput.KeyPressed(Keys.Enter))
  63.                 {
  64.                     StartGame();
  65.                 }
  66.             }
  67.             else
  68.             {
  69.                 if (SnakeInput.KeyPressed(Keys.Right) && SnakeSettings.direction != Direction.Left)
  70.                     SnakeSettings.direction = Direction.Right;
  71.                 else if (SnakeInput.KeyPressed(Keys.Left) && SnakeSettings.direction != Direction.Right)
  72.                     SnakeSettings.direction = Direction.Left;
  73.                 else if (SnakeInput.KeyPressed(Keys.Up) && SnakeSettings.direction != Direction.Down)
  74.                     SnakeSettings.direction = Direction.Up;
  75.                 else if (SnakeInput.KeyPressed(Keys.Down) && SnakeSettings.direction != Direction.Up)
  76.                     SnakeSettings.direction = Direction.Down;
  77.  
  78.                 MovePlayer();
  79.             }
  80.  
  81.             pbCanvas.Invalidate();
  82.  
  83.         }
  84.  
  85.         private void MovePlayer()
  86.         {
  87.             for (int i = Snake.Count - 1; i >= 0; i--)
  88.             {
  89.                 //Move head
  90.                 if (i == 0)
  91.                 {
  92.                     switch (SnakeSettings.direction)
  93.                     {
  94.                         case Direction.Right:
  95.                             Snake[i].X++;
  96.                             break;
  97.                         case Direction.Left:
  98.                             Snake[i].X--;
  99.                             break;
  100.                         case Direction.Up:
  101.                             Snake[i].Y--;
  102.                             break;
  103.                         case Direction.Down:
  104.                             Snake[i].Y++;
  105.                             break;
  106.                     }
  107.  
  108.  
  109.                     //Get maximum X and Y Pos
  110.                     int maxXPos = pbCanvas.Size.Width / SnakeSettings.Width;
  111.                     int maxYPos = pbCanvas.Size.Height / SnakeSettings.Height;
  112.  
  113.                     //Detect collission with game borders.
  114.                     if (Snake[i].X < 0 || Snake[i].Y < 0
  115.                         || Snake[i].X >= maxXPos || Snake[i].Y >= maxYPos)
  116.                     {
  117.                         Die();
  118.                     }
  119.  
  120.  
  121.                     //Detect collission with body
  122.                     for (int j = 1; j < Snake.Count; j++)
  123.                     {
  124.                         if (Snake[i].X == Snake[j].X &&
  125.                            Snake[i].Y == Snake[j].Y)
  126.                         {
  127.                             Die();
  128.                         }
  129.                     }
  130.  
  131.                     //Detect collision with food piece
  132.                     if (Snake[0].X == food.X && Snake[0].Y == food.Y)
  133.                     {
  134.                         Eat();
  135.                     }
  136.  
  137.                 }
  138.                 else
  139.                 {
  140.                     //Move body
  141.                     Snake[i].X = Snake[i - 1].X;
  142.                     Snake[i].Y = Snake[i - 1].Y;
  143.                 }
  144.             }
  145.         }
  146.  
  147.         private void MainForm_KeyDown(object sender, KeyEventArgs e)
  148.         {
  149.             SnakeInput.ChangeState(e.KeyCode, true);
  150.         }
  151.  
  152.         private void MainForm_KeyUp(object sender, KeyEventArgs e)
  153.         {
  154.             SnakeInput.ChangeState(e.KeyCode, false);
  155.         }
  156.  
  157.         private void Eat()
  158.         {
  159.             //Add circle to body
  160.             Circle circle = new Circle
  161.             {
  162.                 X = Snake[Snake.Count - 1].X,
  163.                 Y = Snake[Snake.Count - 1].Y
  164.             };
  165.             Snake.Add(circle);
  166.  
  167.             //Update Score
  168.             SnakeSettings.Score += SnakeSettings.Points;
  169.             lblScore.Text = SnakeSettings.Score.ToString();
  170.  
  171.             GenerateFood();
  172.         }
  173.  
  174.         private void Die()
  175.         {
  176.             SnakeSettings.GameOver = true;
  177.         }
  178.  
  179.         private void pbCanvas_Paint(object sender, PaintEventArgs e)
  180.         {
  181.             Graphics canvas = e.Graphics;
  182.  
  183.             if (!SnakeSettings.GameOver)
  184.             {
  185.                 //Set colour of snake
  186.  
  187.                 //Draw snake
  188.                 for (int i = 0; i < Snake.Count; i++)
  189.                 {
  190.                     Brush snakeColour;
  191.                     if (i == 0)
  192.                         snakeColour = Brushes.Black;     //Draw head
  193.                     else
  194.                         snakeColour = Brushes.Green;    //Rest of body
  195.  
  196.                     //Draw snake
  197.                     canvas.FillEllipse(snakeColour,
  198.                         new Rectangle(Snake[i].X * SnakeSettings.Width,
  199.                                       Snake[i].Y * SnakeSettings.Height,
  200.                                       SnakeSettings.Width, SnakeSettings.Height));
  201.                 }
  202.                 //Draw Food
  203.                 canvas.FillEllipse(Brushes.Red,
  204.                     new Rectangle(food.X * SnakeSettings.Width,
  205.                          food.Y * SnakeSettings.Height, SnakeSettings.Width, SnakeSettings.Height));
  206.             }
  207.             else
  208.             {
  209.                 string gameOver = "Game over \nYour final score is: " + SnakeSettings.Score + "\nPress Enter to try again";
  210.                 lblGameOver.Text = gameOver;
  211.                 //lblGameOver.Visible = true;
  212.                 Console.Write("snek ded :(");
  213.             }
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement