Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.99 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.         private Vector2f SnakePos = new Vector2f(0, 0);
  12.  
  13.         private Vector2 ApplePos = new Vector2();
  14.  
  15.         private Vector2 SnakeLetterPos = new Vector2(670, 200);
  16.  
  17.  
  18.         private int SnakeCounter;
  19.  
  20.         private int SnakeDirection = 0;
  21.  
  22.         private int Score = 0;
  23.  
  24.  
  25.         private Random Random = new Random();
  26.  
  27.  
  28.         private Rectanglef Apple;
  29.  
  30.  
  31.         private bool SnakeCollission;
  32.  
  33.         private bool Collission;
  34.  
  35.         private bool Menu = true;
  36.  
  37.         private bool ScoreCollission = false;
  38.  
  39.         private bool ShowButton = true;
  40.  
  41.  
  42.         private Font Score_Font = new Font("Planet Benson 2", 70);
  43.  
  44.         private Font Button_Font = new Font("Planet Benson 2", 40);
  45.  
  46.  
  47.         private List<Vector2f> Snake = new List<Vector2f>();
  48.  
  49.  
  50.         private Bitmap[] SnakeMenuLetters = new Bitmap[5];
  51.  
  52.         private Bitmap SnakeHead;
  53.  
  54.         private Bitmap PlayButton;
  55.  
  56.  
  57.         private Audio BackgroundMusic = new Audio("SnakeMusic.wav");
  58.  
  59.         private Audio AppleBite = new Audio("AppleBite.wav");
  60.  
  61.  
  62.         private Button Start = null;
  63.  
  64.         public override void GameStart()
  65.         {
  66.             BackgroundMusic.SetLooping(true);
  67.             GAME_ENGINE.PlayAudio(BackgroundMusic);
  68.  
  69.             GetRandomNumberApple();
  70.             Apple = new Rectanglef(ApplePos.X, ApplePos.Y, 32, 32);
  71.  
  72.             Snake.Add(SnakePos);
  73.  
  74.             PlayButton = new Bitmap("PlayButton3.png");
  75.  
  76.             SnakeHead = new Bitmap("SnakeGraphics.png");
  77.  
  78.             if (ShowButton == true)
  79.             {
  80.                 Start = new Button(OnButtonClick_StartGame, "", 800, 600, 300, 100);
  81.                 Start.SetBitmap(PlayButton);
  82.                 Start.SetActive(true);
  83.             }
  84.  
  85.             SnakeMenuLetters[0] = new Bitmap("SnakeLetters.png");
  86.             SnakeMenuLetters[1] = new Bitmap("SnakeLetters.png");
  87.             SnakeMenuLetters[2] = new Bitmap("SnakeLetters.png");
  88.             SnakeMenuLetters[3] = new Bitmap("SnakeLetters.png");
  89.             SnakeMenuLetters[4] = new Bitmap("SnakeLetters.png");
  90.  
  91.  
  92.         }
  93.  
  94.         public override void GameEnd()
  95.         {
  96.  
  97.         }
  98.  
  99.         public override void Update()
  100.         {
  101.  
  102.             SnakeCounter++;
  103.  
  104.             //These if-statements set the SnakeDirection
  105.             if (GAME_ENGINE.GetKeyDown(Key.W))
  106.             {
  107.                 if (SnakeDirection != 2)
  108.                 {
  109.                     SnakeDirection = 1;
  110.                 }
  111.  
  112.             }
  113.  
  114.             if (GAME_ENGINE.GetKeyDown(Key.S))
  115.             {
  116.                 if (SnakeDirection != 1)
  117.                 {
  118.                     SnakeDirection = 2;
  119.                 }
  120.  
  121.             }
  122.  
  123.             if (GAME_ENGINE.GetKeyDown(Key.A))
  124.             {
  125.                 if (SnakeDirection != 4)
  126.                 {
  127.                     SnakeDirection = 3;
  128.                 }
  129.  
  130.             }
  131.  
  132.             if (GAME_ENGINE.GetKeyDown(Key.D))
  133.             {
  134.                 if (SnakeDirection != 3)
  135.                 {
  136.                     SnakeDirection = 4;
  137.                 }
  138.  
  139.             }
  140.  
  141.             //This is the function that makes the snake parts follow the head
  142.             if (SnakeCounter >= 10)
  143.             {
  144.                 for (int i = Snake.Count - 1; i > 0; i--)
  145.                 {
  146.                     Snake[i] = new Vector2f(Snake[i - 1].X, Snake[i - 1].Y);
  147.                 }
  148.             }
  149.  
  150.             //This is the function that makes the snake move based on the SnakeDirection
  151.             if ((SnakeDirection == 1) && (SnakeCounter >= 10))
  152.             {
  153.                 SnakeCounter = 0;
  154.                 SnakePos.Y -= 64;
  155.                 Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
  156.             }
  157.  
  158.             if ((SnakeDirection == 2) && (SnakeCounter >= 10))
  159.             {
  160.                 SnakeCounter = 0;
  161.                 SnakePos.Y += 64;
  162.                 Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
  163.             }
  164.  
  165.             if ((SnakeDirection == 4) && (SnakeCounter >= 10))
  166.             {
  167.                 SnakeCounter = 0;
  168.                 SnakePos.X += 64;
  169.                 Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
  170.             }
  171.  
  172.             if ((SnakeDirection == 3) && (SnakeCounter >= 10))
  173.             {
  174.                 SnakeCounter = 0;
  175.                 SnakePos.X -= 64;
  176.                 Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
  177.             }
  178.  
  179.             //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
  180.             if (Snake[0].X > GAME_ENGINE.GetScreenWidth())
  181.             {
  182.                 SnakePos.X = 0;
  183.             }
  184.  
  185.             if (Snake[0].X < 0)
  186.             {
  187.                 SnakePos.X = 1920;
  188.             }
  189.  
  190.             if (Snake[0].Y > GAME_ENGINE.GetScreenHeight())
  191.             {
  192.                 SnakePos.Y = 0;
  193.             }
  194.  
  195.             if (Snake[0].Y < 0)
  196.             {
  197.                 SnakePos.Y = 1080;
  198.             }
  199.  
  200.             //This is the function that checks the collission between the apple and the snake
  201.             if (Snake[0].X > (Apple.X + 64) || (Snake[0].X + 64) < Apple.X || Snake[0].Y > (Apple.Y + 64) || (Snake[0].Y + 64) < Apple.Y)
  202.             {
  203.                 Collission = false;
  204.             }
  205.             else
  206.             {
  207.                 Collission = true;
  208.             }
  209.  
  210.             //This is the function that checks if the snake is colliding with itself
  211.             for (int i = 1; i < Snake.Count; i++)
  212.             {
  213.                 if (Snake[0].X > Snake[i].X + (i * 64) || (Snake[0].X + (i * 64)) < Snake[i].X || Snake[0].Y > (Snake[i].Y + (i * 64)) || (Snake[0].Y + (i * 64)) < Snake[i].Y)
  214.                 {
  215.                     SnakeCollission = false;
  216.                 }
  217.                 else
  218.                 {
  219.                     SnakeCollission = true;
  220.                 }
  221.             }
  222.  
  223.             //This function makes the timer score work
  224.             if (ScoreCollission)
  225.             {
  226.                 Score += 2;
  227.  
  228.                 for (int i = 0; i < 1000; i++)
  229.                 {
  230.                     if (Score == (100 * i))
  231.                     {
  232.                         ScoreCollission = false;
  233.                     }
  234.                 }
  235.  
  236.             }
  237.  
  238.         }
  239.  
  240.         private void OnButtonClick_StartGame()
  241.         {
  242.             Menu = false;
  243.             Start.SetActive(false);
  244.         }
  245.  
  246.         private void SnakeMenu()
  247.         {
  248.  
  249.             //Drawing the snake letters
  250.             for (int i = 0; i < 5; i++)
  251.             {
  252.                 GAME_ENGINE.DrawBitmap(SnakeMenuLetters[i], SnakeLetterPos.X + (118 * i), SnakeLetterPos.Y, 0 + (118 * i), 0, 130, 192);
  253.             }
  254.  
  255.  
  256.  
  257.         }
  258.  
  259.         //This function draws the apple each time it's called
  260.         private void DrawRandomApple()
  261.         {
  262.             GetRandomNumberApple();
  263.             Apple = new Rectanglef(ApplePos.X, ApplePos.Y, 32, 32);
  264.         }
  265.  
  266.         //This function generates two random numbers for the apple each time it's called
  267.         private void GetRandomNumberApple()
  268.         {
  269.             ApplePos.X = Random.Next(100, 1820);
  270.             ApplePos.Y = Random.Next(100, 980);
  271.         }
  272.  
  273.         public override void Paint()
  274.         {
  275.             if (!Menu)
  276.             {
  277.                 ShowButton = false;
  278.  
  279.                 if (ShowButton == false)
  280.                 {
  281.                     Start.SetSize(0, 0);
  282.                     Start.SetText("");
  283.                 }
  284.  
  285.                 //This is the function that draws the apple at the start of the game
  286.                 GAME_ENGINE.SetColor(Color.Green);
  287.                 GAME_ENGINE.FillEllipse(Apple);
  288.  
  289.                 if (Collission)
  290.                 {
  291.                     //Generate new apple
  292.                     DrawRandomApple();
  293.                     GAME_ENGINE.SetColor(Color.Green);
  294.                     GAME_ENGINE.FillEllipse(Apple);
  295.                     GAME_ENGINE.PlayAudio(AppleBite);
  296.  
  297.                     Snake.Add(SnakePos);
  298.                    
  299.                     ScoreCollission = true;
  300.                 }
  301.  
  302.                 //This is where we draw the snake
  303.                 for (int i = 0; i < Snake.Count; i++)
  304.                 {
  305.                     GAME_ENGINE.SetColor(Color.Red);
  306.                     GAME_ENGINE.FillRectangle(Snake[i].X, Snake[i].Y, 64, 64);                    
  307.                 }
  308.  
  309.                 //This is where we draw the score
  310.                 GAME_ENGINE.DrawString(Score_Font, Score.ToString(), (GAME_ENGINE.GetScreenWidth() / 2) - 50, GAME_ENGINE.GetScreenHeight() / 2, 1000, 1000);
  311.  
  312.             }
  313.  
  314.             if (GAME_ENGINE.GetKeyDown(Key.Escape))
  315.             {
  316.                 Menu = true;
  317.                 Start = new Button(OnButtonClick_StartGame, "", 800, 600, 300, 100);
  318.                 Start.SetBitmap(PlayButton);
  319.                 Start.SetActive(true);
  320.                 Snake.Clear();
  321.                 SnakePos.X = 0;
  322.                 SnakePos.Y = 0;
  323.                 Snake.Add(SnakePos);
  324.                 Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
  325.                 SnakeDirection = 0;
  326.                 Score = 0;
  327.             }
  328.  
  329.             if (SnakeCollission == true)
  330.             {
  331.                 Menu = true;
  332.                 Start = new Button(OnButtonClick_StartGame, "", 800, 600, 300, 100);
  333.                 Start.SetBitmap(PlayButton);
  334.                 Start.SetActive(true);
  335.                 Snake.Clear();
  336.                 SnakePos.X = 0;
  337.                 SnakePos.Y = 0;
  338.                 Snake.Add(SnakePos);
  339.                 Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
  340.                 SnakeDirection = 0;
  341.                 Score = 0;
  342.                 SnakeCollission = false;
  343.             }
  344.  
  345.             if (Menu)
  346.             {
  347.                 SnakeMenu();
  348.             }
  349.  
  350.         }
  351.     }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement