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