Advertisement
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
- {
- private Vector2f SnakePos = new Vector2f(0, 0);
- private Vector2 ApplePos = new Vector2();
- private Vector2 SnakeLetterPos = new Vector2(670, 200);
- private int SnakeCounter;
- private int SnakeDirection = 0;
- private int Score = 0;
- private Random Random = new Random();
- private Rectanglef Apple;
- private bool SnakeCollission;
- private bool Collission;
- private bool Menu = true;
- private bool ScoreCollission = false;
- private bool ShowButton = true;
- private Font Score_Font = new Font("Planet Benson 2", 70);
- private Font Button_Font = new Font("Planet Benson 2", 40);
- private List<Vector2f> Snake = new List<Vector2f>();
- private Bitmap[] SnakeMenuLetters = new Bitmap[5];
- private Bitmap SnakeHead;
- private Bitmap PlayButton;
- private Audio BackgroundMusic = new Audio("SnakeMusic.wav");
- private Audio AppleBite = new Audio("AppleBite.wav");
- private Button Start = null;
- public override void GameStart()
- {
- BackgroundMusic.SetLooping(true);
- GAME_ENGINE.PlayAudio(BackgroundMusic);
- GetRandomNumberApple();
- Apple = new Rectanglef(ApplePos.X, ApplePos.Y, 32, 32);
- Snake.Add(SnakePos);
- PlayButton = new Bitmap("PlayButton3.png");
- SnakeHead = new Bitmap("SnakeGraphics.png");
- if (ShowButton == true)
- {
- Start = new Button(OnButtonClick_StartGame, "", 800, 600, 300, 100);
- Start.SetBitmap(PlayButton);
- Start.SetActive(true);
- }
- SnakeMenuLetters[0] = new Bitmap("SnakeLetters.png");
- SnakeMenuLetters[1] = new Bitmap("SnakeLetters.png");
- SnakeMenuLetters[2] = new Bitmap("SnakeLetters.png");
- SnakeMenuLetters[3] = new Bitmap("SnakeLetters.png");
- SnakeMenuLetters[4] = new Bitmap("SnakeLetters.png");
- }
- 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;
- }
- }
- //This is the function that makes the snake parts follow the head
- if (SnakeCounter >= 10)
- {
- for (int i = Snake.Count - 1; i > 0; i--)
- {
- Snake[i] = new Vector2f(Snake[i - 1].X, Snake[i - 1].Y);
- }
- }
- //This is the function that makes the snake move based on the SnakeDirection
- if ((SnakeDirection == 1) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakePos.Y -= 64;
- Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
- }
- if ((SnakeDirection == 2) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakePos.Y += 64;
- Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
- }
- if ((SnakeDirection == 4) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakePos.X += 64;
- Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
- }
- if ((SnakeDirection == 3) && (SnakeCounter >= 10))
- {
- SnakeCounter = 0;
- SnakePos.X -= 64;
- Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
- }
- //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 (Snake[0].X > GAME_ENGINE.GetScreenWidth())
- {
- SnakePos.X = 0;
- }
- if (Snake[0].X < 0)
- {
- SnakePos.X = 1920;
- }
- if (Snake[0].Y > GAME_ENGINE.GetScreenHeight())
- {
- SnakePos.Y = 0;
- }
- if (Snake[0].Y < 0)
- {
- SnakePos.Y = 1080;
- }
- //This is the function that checks the collission between the apple and the snake
- 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)
- {
- Collission = false;
- }
- else
- {
- Collission = true;
- }
- //This is the function that checks if the snake is colliding with itself
- for (int i = 1; i < Snake.Count; i++)
- {
- 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)
- {
- SnakeCollission = false;
- }
- else
- {
- SnakeCollission = true;
- }
- }
- //This function makes the timer score work
- if (ScoreCollission)
- {
- Score += 2;
- for (int i = 0; i < 1000; i++)
- {
- if (Score == (100 * i))
- {
- ScoreCollission = false;
- }
- }
- }
- }
- private void OnButtonClick_StartGame()
- {
- Menu = false;
- Start.SetActive(false);
- }
- private void SnakeMenu()
- {
- //Drawing the snake letters
- for (int i = 0; i < 5; i++)
- {
- GAME_ENGINE.DrawBitmap(SnakeMenuLetters[i], SnakeLetterPos.X + (118 * i), SnakeLetterPos.Y, 0 + (118 * i), 0, 130, 192);
- }
- }
- //This function draws the apple each time it's called
- private void DrawRandomApple()
- {
- GetRandomNumberApple();
- Apple = new Rectanglef(ApplePos.X, ApplePos.Y, 32, 32);
- }
- //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()
- {
- if (!Menu)
- {
- ShowButton = false;
- if (ShowButton == false)
- {
- Start.SetSize(0, 0);
- Start.SetText("");
- }
- //This is the function that draws the apple at the start of the game
- GAME_ENGINE.SetColor(Color.Green);
- GAME_ENGINE.FillEllipse(Apple);
- if (Collission)
- {
- //Generate new apple
- DrawRandomApple();
- GAME_ENGINE.SetColor(Color.Green);
- GAME_ENGINE.FillEllipse(Apple);
- GAME_ENGINE.PlayAudio(AppleBite);
- Snake.Add(SnakePos);
- ScoreCollission = true;
- }
- //This is where we draw the snake
- for (int i = 0; i < Snake.Count; i++)
- {
- GAME_ENGINE.SetColor(Color.Red);
- GAME_ENGINE.FillRectangle(Snake[i].X, Snake[i].Y, 64, 64);
- }
- //This is where we draw the score
- GAME_ENGINE.DrawString(Score_Font, Score.ToString(), (GAME_ENGINE.GetScreenWidth() / 2) - 50, GAME_ENGINE.GetScreenHeight() / 2, 1000, 1000);
- }
- if (GAME_ENGINE.GetKeyDown(Key.Escape))
- {
- Menu = true;
- Start = new Button(OnButtonClick_StartGame, "", 800, 600, 300, 100);
- Start.SetBitmap(PlayButton);
- Start.SetActive(true);
- Snake.Clear();
- SnakePos.X = 0;
- SnakePos.Y = 0;
- Snake.Add(SnakePos);
- Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
- SnakeDirection = 0;
- Score = 0;
- }
- if (SnakeCollission == true)
- {
- Menu = true;
- Start = new Button(OnButtonClick_StartGame, "", 800, 600, 300, 100);
- Start.SetBitmap(PlayButton);
- Start.SetActive(true);
- Snake.Clear();
- SnakePos.X = 0;
- SnakePos.Y = 0;
- Snake.Add(SnakePos);
- Snake[0] = new Vector2f(SnakePos.X, SnakePos.Y);
- SnakeDirection = 0;
- Score = 0;
- SnakeCollission = false;
- }
- if (Menu)
- {
- SnakeMenu();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement