Guest User

Untitled

a guest
Nov 1st, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace GameEngine
  8. {
  9.     public class Gameplay_Opdracht_5 : AbstractGame
  10.     {
  11.         //This vector defines the SnakeSize
  12.         private Vector2 SnakeSize = new Vector2( 64, 64);
  13.  
  14.         //This bool checks if an apple is already drawn on the screen
  15.         private bool AppleDrawn = false;
  16.  
  17.         //This vector defines the x and y position of the apple
  18.         private Vector2 ApplePos = new Vector2();
  19.  
  20.         //This vector defines the SnakePosition
  21.         private Vector2 SnakePos = new Vector2(0, 64);
  22.  
  23.         //This int serves as a counter to make sure the snake moves at the right pace
  24.         private int SnakeCounter;
  25.  
  26.         //This int keeps track of which direction the snake is heading
  27.         private int SnakeDirection = 0;
  28.  
  29.         //This is the random number generator
  30.         private Random Random = new Random();
  31.  
  32.         //This is the Rectanglef for the snakehead
  33.         private Rectanglef SnakeHead;
  34.  
  35.         //This is the Rectanglef for the apple
  36.         private Rectanglef Apple;
  37.  
  38.         //This is the apple counter
  39.         private int AppleCounter = 0;
  40.  
  41.         //GridSizes
  42.         private int SizeX = 30;
  43.         private int SizeY = 16;
  44.  
  45.         public override void GameStart()
  46.         {
  47.  
  48.             GetRandomNumberApple();
  49.             Apple = new Rectanglef(ApplePos.X, ApplePos.Y, 64, 64);
  50.             SnakeHead = new Rectanglef(SnakePos.X, SnakePos.Y, SnakeSize.X, SnakeSize.Y);
  51.  
  52.         }
  53.  
  54.         public override void GameEnd()
  55.         {
  56.  
  57.         }
  58.  
  59.         public override void Update()
  60.         {
  61.             SnakeCounter++;
  62.  
  63. //These if-statements set the SnakeDirection
  64.  
  65.             if (GAME_ENGINE.GetKeyDown(Key.W))
  66.             {
  67.                 if (SnakeDirection != 2)
  68.                 {
  69.                     SnakeDirection = 1;
  70.                 }
  71.  
  72.             }
  73.  
  74.             if (GAME_ENGINE.GetKeyDown(Key.S))
  75.             {
  76.                 if (SnakeDirection != 1)
  77.                 {
  78.                     SnakeDirection = 2;
  79.                 }
  80.  
  81.             }
  82.  
  83.             if (GAME_ENGINE.GetKeyDown(Key.A))
  84.             {
  85.                 if (SnakeDirection != 4)
  86.                 {
  87.                     SnakeDirection = 3;
  88.                 }
  89.  
  90.             }
  91.  
  92.             if (GAME_ENGINE.GetKeyDown(Key.D))
  93.             {
  94.                 if (SnakeDirection != 3)
  95.                 {
  96.                     SnakeDirection = 4;
  97.                 }
  98.  
  99.             }
  100.  
  101.             if (AppleDrawn == true)
  102.             {
  103.                 AppleCounter++;
  104.             }
  105.  
  106.  
  107.  
  108.         }
  109.  
  110. //This function draws the apple each time it's called
  111.         private void DrawRandomApple()
  112.         {
  113.                 GetRandomNumberApple();
  114.                 GAME_ENGINE.FillEllipse(ApplePos.X, ApplePos.Y, 50, 50);
  115.         }
  116.  
  117. //This function generates two random numbers for the apple each time it's called
  118.         private void GetRandomNumberApple()
  119.         {
  120.             ApplePos.X = Random.Next(100, 1820);
  121.             ApplePos.Y = Random.Next(100, 980);
  122.         }
  123.  
  124.         public override void Paint()
  125.         {
  126.             GAME_ENGINE.SetColor(Color.Red);
  127.  
  128.             //This is the function that makes the snake move based on the SnakeDirection
  129.  
  130.             if ((SnakeDirection == 1) && (SnakeCounter >= 10))
  131.             {
  132.                 SnakeCounter = 0;
  133.                 SnakeHead.Y -= 64;
  134.             }
  135.  
  136.             if ((SnakeDirection == 2) && (SnakeCounter >= 10))
  137.             {
  138.                 SnakeCounter = 0;
  139.                 SnakeHead.Y += 64;
  140.             }
  141.  
  142.             if ((SnakeDirection == 4) && (SnakeCounter >= 10))
  143.             {
  144.                 SnakeCounter = 0;
  145.                 SnakeHead.X += 64;
  146.             }
  147.  
  148.             if ((SnakeDirection == 3) && (SnakeCounter >= 10))
  149.             {
  150.                 SnakeCounter = 0;
  151.                 SnakeHead.X -= 64;
  152.             }
  153.  
  154.             //This the function that makes sure that the snake re-appears on the left side of the screen when it goes off the right side
  155.  
  156.             if (SnakeHead.X > GAME_ENGINE.GetScreenWidth())
  157.             {
  158.                 SnakeHead.X = 0;
  159.             }
  160.  
  161.             if (SnakeHead.X < 0)
  162.             {
  163.                 SnakeHead.X = 1920;
  164.             }
  165.  
  166.             if (SnakeHead.Y > GAME_ENGINE.GetScreenHeight())
  167.             {
  168.                 SnakeHead.Y = 0;
  169.             }
  170.  
  171.             if (SnakeHead.Y < 0)
  172.             {
  173.                 SnakeHead.Y = 1024;
  174.             }
  175.  
  176.             //This is the function that draws the apple at random positions
  177.             if (AppleDrawn == false)
  178.             {
  179.                 GAME_ENGINE.SetColor(Color.Green);
  180.                 GAME_ENGINE.FillRectangle(Apple);
  181.             }
  182.  
  183. //This is the function that checks the collission between the apple and the snake
  184.             if ((SnakeHead.X > Apple.X) && (SnakeHead.X < (Apple.X + 64)) && (SnakeHead.Y > Apple.Y) && (SnakeHead.Y < (Apple.Y + 64)))
  185.             {
  186.                 AppleDrawn = true;
  187.             }
  188.  
  189.             if ((AppleDrawn == true) && (AppleCounter == 10))
  190.             {
  191.                 AppleCounter = 0;
  192.                 AppleDrawn = false;
  193.             }
  194.  
  195.  
  196.                 //This function draws the grid
  197.                 for (int x = 0; x < SizeX; x++)
  198.             {
  199.                 for (int y = 0; y < SizeY; y++)
  200.                 {
  201.                     GAME_ENGINE.SetColor(Color.Black);
  202.                     GAME_ENGINE.DrawRectangle(0 + (x * 64), 0 + (y * 64), 64, 64);
  203.                 }
  204.             }
  205.  
  206.  
  207.  
  208. //This is where we draw the actual snake
  209.             GAME_ENGINE.SetColor(Color.Red);
  210.             GAME_ENGINE.FillRectangle(SnakeHead);
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment