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;
- //Uppgift1: Skriv en ny klass Enemy, som ärver Gameobject och som vandrar runt slumpmässigt på spelplanen
- //Uppgift2: Skriv en ny klass Player, som ärver Gameobject och som kan styras med tangenterna WASD
- namespace GridGame
- {
- class Program
- {
- static void Main(string[] args)
- {
- Meny startMeny = new Meny();
- startMeny.Main();
- Game myGame = new Game(40, 25);
- myGame.DrawBoard();
- while (true)
- {
- myGame.UpdateBoard();
- myGame.DrawBoard();
- }
- }
- }
- class Meny
- {
- static int index = 0;
- public void Main()
- {
- List<string> menuItems = new List<string>() { "Start", "High Score", "Exit" };
- Console.CursorVisible = false;
- bool itemSelected = false;
- while (itemSelected == false)
- {
- string selectedMenuItem = drawMenu(menuItems);
- if (selectedMenuItem == "Start")
- {
- Console.Clear();
- itemSelected = true;
- }
- else if (selectedMenuItem == "Exit")
- {
- Environment.Exit(0);
- }
- }
- }
- private static String drawMenu(List<string> items)
- {
- for (int i = 0; i < items.Count; i++)
- {
- if (i == index)
- {
- Console.BackgroundColor = ConsoleColor.Gray;
- Console.ForegroundColor = ConsoleColor.Black;
- Console.WriteLine(items[i]);
- }
- else
- {
- Console.WriteLine(items[i]);
- }
- Console.ResetColor();
- }
- ConsoleKeyInfo cKey = Console.ReadKey();
- if (cKey.Key == ConsoleKey.DownArrow)
- {
- if (index == items.Count - 1)
- {
- index = 0;
- }
- else { index++; }
- }
- else if (cKey.Key == ConsoleKey.UpArrow)
- {
- if (index <= 0)
- {
- index = items.Count - 1;
- }
- else { index--; }
- }
- else if (cKey.Key == ConsoleKey.Enter)
- {
- return items[index];
- }
- else
- {
- Console.Clear();
- return "";
- }
- Console.Clear();
- return "";
- }
- }
- class Game
- {
- static List<GameObject> GameObjects = new List<GameObject>();
- public Game(int xSize, int ySize)
- {
- for (int i = 0; i < ySize + 2; i++)
- {
- for (int j = 0; j < xSize + 2; j++)
- {
- if (j == 0 || i == 0 || i == ySize + 1 || j == xSize + 1)
- {
- GameObjects.Add(new Wall(j, i));
- }
- }
- }
- Random RNG = new Random();
- GameObjects.Add(new Player(7, 3));
- GameObjects.Add(new Enemy(20, 15, RNG));
- GameObjects.Add(new Enemy(18, 19, RNG));
- GameObjects.Add(new Enemy(20, 20, RNG));
- GameObjects.Add(new Enemy(32, 5, RNG));
- GameObjects.Add(new Enemy(15, 22, RNG));
- GameObjects.Add(new Enemy(20, 25, RNG));
- GameObjects.Add(new Enemy(30, 25, RNG));
- }
- public void DrawBoard()
- {
- foreach (GameObject gameObject in GameObjects)
- {
- gameObject.Draw(2, 1);
- }
- }
- public static void DrawTurn(int xBoxSize, int yBoxSize, int turn)
- {
- Console.SetCursorPosition(xBoxSize, yBoxSize);
- Console.Write("Turns: " + turn);
- }
- public void UpdateBoard()
- {
- foreach (GameObject gameObject in GameObjects)
- {
- gameObject.Update(GameObjects);
- }
- int i = 0;
- while (i < GameObjects.Count)
- {
- if (GameObjects[i] is Enemy && !GameObjects[i].IsAlive)
- {
- GameObjects.RemoveAt(i);
- i--;
- }
- i++;
- }
- }
- public static void RemoveEnemy(Enemy enemy)
- {
- GameObjects.Remove(enemy);
- }
- }
- abstract class GameObject
- {
- public bool IsAlive = true;
- public int XPosition;
- public int YPosition;
- public abstract void Draw(int xBoxSize, int yBoxSize);
- public abstract void Update(List<GameObject> GameObjects);
- }
- class Player : GameObject
- {
- int numTurns;
- public Player(int xPosition, int yPosition)
- {
- XPosition = xPosition;
- YPosition = yPosition;
- }
- public override void Draw(int xBoxSize, int yBoxSize)
- {
- int startX = XPosition * xBoxSize;
- int startY = YPosition * yBoxSize;
- Console.SetCursorPosition(startX, startY);
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.Write("[]");
- Console.ResetColor();
- }
- private void Erase(int xBoxSize, int yBoxSize)
- {
- int startX = XPosition * xBoxSize;
- int startY = YPosition * yBoxSize;
- Console.SetCursorPosition(startX, startY);
- Console.Write(" ");
- }
- public override void Update(List<GameObject> GameObjects)
- {
- ConsoleKeyInfo KeyPress = Console.ReadKey(true);
- int tempXPosition = XPosition;
- int tempYPosition = YPosition;
- bool occupied = false;
- Erase(2, 1);
- if (KeyPress.Key == ConsoleKey.W)
- {
- tempYPosition--;
- numTurns++;
- }
- if (KeyPress.Key == ConsoleKey.A)
- {
- tempXPosition--;
- numTurns++;
- }
- if (KeyPress.Key == ConsoleKey.S)
- {
- tempYPosition++;
- numTurns++;
- }
- if (KeyPress.Key == ConsoleKey.D)
- {
- tempXPosition++;
- numTurns++;
- }
- if(KeyPress.Key == ConsoleKey.Escape)
- {
- Environment.Exit(0);
- }
- Game.DrawTurn(95, 13, numTurns);
- foreach (var item in GameObjects)
- {
- if (tempXPosition == item.XPosition && tempYPosition == item.YPosition)
- {
- if (item is Enemy)
- {
- occupied = true;
- }
- if (item is Wall)
- {
- occupied = true;
- }
- }
- }
- if (!occupied)
- {
- XPosition = tempXPosition;
- YPosition = tempYPosition;
- }
- }
- }
- class Enemy : GameObject
- {
- Random RNG;
- public Enemy(int xPosition, int yPosition, Random rNG)
- {
- XPosition = xPosition;
- YPosition = yPosition;
- RNG = rNG;
- }
- public override void Draw(int xBoxSize, int yBoxSize)
- {
- int startX = XPosition * xBoxSize;
- int startY = YPosition * yBoxSize;
- Console.SetCursorPosition(startX, startY);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("()");
- Console.ResetColor();
- }
- private void Erase(int xBoxSize, int yBoxSize)
- {
- int startX = XPosition * xBoxSize;
- int startY = YPosition * yBoxSize;
- Console.SetCursorPosition(startX, startY);
- Console.Write(" ");
- }
- public override void Update(List<GameObject> GameObjects)
- {
- int slumptal = RNG.Next(1,5);
- int tempXPosition = XPosition;
- int tempYPosition = YPosition;
- bool occupied = false;
- if (IsAlive)
- {
- Erase(2, 1);
- if (slumptal == 1)
- {
- tempXPosition--;
- }
- if (slumptal == 2)
- {
- tempXPosition++;
- }
- if (slumptal == 3)
- {
- tempYPosition--;
- }
- if (slumptal == 4)
- {
- tempYPosition++;
- }
- foreach (var item in GameObjects)
- {
- if (tempXPosition == item.XPosition && tempYPosition == item.YPosition)
- {
- if(item is Wall)
- occupied = true;
- if (item is Player)
- {
- occupied = false;
- IsAlive = false;
- }
- }
- }
- if (!occupied)
- {
- XPosition = tempXPosition;
- YPosition = tempYPosition;
- }
- }
- else
- {
- Game.RemoveEnemy(this);
- }
- }
- }
- class Wall : GameObject
- {
- public Wall(int xPosition, int yPosition)
- {
- XPosition = xPosition;
- YPosition = yPosition;
- }
- public override void Draw(int xBoxSize, int yBoxSize)
- {
- int startX = XPosition * xBoxSize;
- int startY = YPosition * yBoxSize;
- Console.SetCursorPosition(startX, startY);
- Console.Write("##");
- }
- public override void Update(List<GameObject> GameObjects)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement