Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GameEngine
- {
- public class Gameplay_Opdracht_5 : AbstractGame
- {
- //This vector defines the SnakeSize
- private Vector2 SnakeSize = new Vector2( 64, 64);
- //This bool checks if an apple is already drawn on the screen
- private bool AppleDrawn = false;
- //This vector defines the x and y position of the apple
- private Vector2 ApplePos = new Vector2();
- //This vector defines the SnakePosition
- private Vector2 SnakePos = new Vector2(0, 64);
- //This int serves as a counter to make sure the snake moves at the right pace
- private int SnakeCounter;
- //This int keeps track of which direction the snake is heading
- private int SnakeDirection = 0;
- //This is the random number generator
- private Random Random = new Random();
- //This is the Rectanglef for the snakehead
- private Rectanglef SnakeHead;
- //This is the Rectanglef for the apple
- private Rectanglef Apple;
- //This is the apple counter
- private int AppleCounter = 0;
- //GridSizes
- private int SizeX = 30;
- private int SizeY = 16;
- public override void GameStart()
- {
- GetRandomNumberApple();
- Apple = new Rectanglef(ApplePos.X, ApplePos.Y, 32, 32);
- SnakeHead = new Rectanglef(SnakePos.X, SnakePos.Y, SnakeSize.X, SnakeSize.Y);
- }
- public override void GameEnd()
- {
- }
- public override void Update()
- {
- SnakeCounter++;
- //These if-statements set the SnakeDirection
- if (GAME_ENGINE.GetKeyDown(Key.W))
- {
- if (SnakeDirection != 2)
- {
- SnakeDirection = 1;
- }
- }
- if (GAME_ENGINE.GetKeyDown(Key.S))
- {
- if (SnakeDirection != 1)
- {
- SnakeDirection = 2;
- }
- }
- if (GAME_ENGINE.GetKeyDown(Key.A))
- {
- if (SnakeDirection != 4)
- {
- SnakeDirection = 3;
- }
- }
- if (GAME_ENGINE.GetKeyDown(Key.D))
- {
- if (SnakeDirection != 3)
- {
- SnakeDirection = 4;
- }
- }
- if (AppleDrawn == true)
- {
- AppleCounter++;
- }
- }
- //This function draws the apple each time it's called
- private void DrawRandomApple()
- {
- GetRandomNumberApple();
- GAME_ENGINE.FillEllipse(ApplePos.X, ApplePos.Y, 50, 50);
- }
- //This function generates two random numbers for the apple each time it's called
- private void GetRandomNumberApple()
- {
- ApplePos.X = Random.Next(100, 1820);
- ApplePos.Y = Random.Next(100, 980);
- }
- public override void Paint()
- {
- GAME_ENGINE.SetColor(Color.Red);
- //This is the function that makes the snake move based on the SnakeDirection
- if ((SnakeDirection == 1) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakeHead.Y -= SnakeSize.Y;
- }
- if ((SnakeDirection == 2) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakeHead.Y += SnakeSize.Y;
- }
- if ((SnakeDirection == 4) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakeHead.X += SnakeSize.X;
- }
- if ((SnakeDirection == 3) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakeHead.X -= SnakeSize.X;
- }
- //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
- if (SnakeHead.X > GAME_ENGINE.GetScreenWidth())
- {
- SnakeHead.X = 0;
- }
- if (SnakeHead.X < 0)
- {
- SnakeHead.X = 1920;
- }
- if (SnakeHead.Y > GAME_ENGINE.GetScreenHeight())
- {
- SnakeHead.Y = 0;
- }
- if (SnakeHead.Y < 0)
- {
- SnakeHead.Y = 1024;
- }
- //This is the function that draws the apple at random positions
- if (AppleDrawn == false)
- {
- GAME_ENGINE.SetColor(Color.Green);
- GAME_ENGINE.FillEllipse(Apple);
- }
- //This is the function that checks the collission between the apple and the snake
- if ((SnakeHead.X >= Apple.X) && (SnakeHead.X <= (Apple.X + 32)) && (SnakeHead.Y >= Apple.Y) && (SnakeHead.Y <= (Apple.Y + 32)))
- {
- Console.WriteLine("Your collission works");
- AppleDrawn = true;
- }
- //if ((AppleDrawn == true) && (AppleCounter == 10))
- //{
- // AppleCounter = 0;
- // AppleDrawn = false;
- //}
- //This function draws the grid
- for (int x = 0; x < SizeX; x++)
- {
- for (int y = 0; y < SizeY; y++)
- {
- GAME_ENGINE.SetColor(Color.Black);
- GAME_ENGINE.DrawRectangle(0 + (x * 64), 0 + (y * 64), 64, 64);
- }
- }
- //This is where we draw the actual snake
- GAME_ENGINE.SetColor(Color.Red);
- GAME_ENGINE.FillRectangle(SnakeHead);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment