Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Explorer
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- //Color console
- Console.ForegroundColor = ConsoleColor.Black;
- Console.BackgroundColor = ConsoleColor.White;
- Console.Clear();
- //Generate & display the map
- Map map = new Map(20, 60);
- map.display();
- Map.Character hero = new Map.Character(10, 1, 0);
- //Random objective position
- int r_x = map.randomnumber_x();
- int r_y = map.randomnumber_y();
- //Play
- while (map.is_over(r_x,r_y) == 2)
- {
- Console.SetCursorPosition(r_x, r_y);
- Console.Write("O");
- map.update();
- }
- if (map.is_over(r_x, r_y) == 0)
- {
- Console.Clear();
- string win = "YOU WIN, YOU REACH THE OBJECTIVE";
- Console.SetCursorPosition((Console.WindowWidth - win.Length) / 2, (Console.WindowHeight - 1) / 2);
- Console.WriteLine(win);
- Console.Read();
- }
- if (map.is_over(r_x, r_y) == 1)
- {
- Console.Clear();
- string lose = "YOU DIE :(";
- Console.SetCursorPosition((Console.WindowWidth - lose.Length) / 2, (Console.WindowHeight - 1) / 2);
- Console.WriteLine(lose);
- Console.Read();
- }
- }
- public class Map
- {
- char[,] map;
- int width;
- int height;
- //int obj_x;
- //int obj_y;
- public Character hero = new Character(10,1,0);
- // random objective
- public int randomnumber_x()
- {
- Random rnum = new Random();
- int r_x = rnum.Next(1, height);
- return (r_x);
- }
- public int randomnumber_y()
- {
- Random rnum = new Random();
- int r_y = rnum.Next(1, width);
- return (r_y);
- }
- //build the map
- public Map(int width,int height)
- {
- this.width = width;
- this.height = height;
- hero.x = 1;
- hero.y = 0;
- map = new char[width, height];
- for (int i = 0; i < map.GetLength(0);i++)
- {
- for (int j = 0; j < map.GetLength(1);j++)
- {
- map[i, j] = ' ';
- }
- }
- int c = 0;
- Random rand = new Random();
- while (c<=15)
- {
- int xpos = rand.Next(0, width);
- int ypos = rand.Next(0, height);
- map[xpos, ypos] = 'X';
- c += 1;
- }
- }
- public void display()
- {
- for (int i = 0; i < map.GetLength(0);i++)
- {
- Console.Write("#");
- for (int j = 0; j < map.GetLength(1);j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine("#");
- }
- Console.WriteLine("Life: " + hero.life + "HP");
- Console.WriteLine("RULES : \n O is the objective to reach. \n x are the ennemies \n + is your character");
- Console.SetCursorPosition(hero.x, hero.y);
- Console.Write("+");
- }
- public void update()
- {
- //wait for User Enter
- var UserEnter = Console.ReadKey();
- if (UserEnter.Key == ConsoleKey.UpArrow)
- {
- hero.move(0, -1);
- }
- if (UserEnter.Key == ConsoleKey.DownArrow)
- {
- hero.move(0, 1);
- }
- if (UserEnter.Key == ConsoleKey.LeftArrow)
- {
- hero.move(-1, 0);
- }
- if (UserEnter.Key == ConsoleKey.RightArrow)
- {
- hero.move(1, 0);
- }
- /*if (map[hero.x+1,hero.y]=='X' || map[hero.x,hero.y+1]=='X' || map[hero.x-1,hero.y]=='X'||map[hero.x,hero.y-1]=='X' && UserEnter.Key !=ConsoleKey.Enter)
- {
- hero.life -= 1;
- }*/
- /*if (map[hero.x+1,hero.y]=='X' || map[hero.x,hero.y+1]=='X' || map[hero.x-1,hero.y]=='X'||map[hero.x,hero.y-1]=='X' && UserEnter.Key ==ConsoleKey.Enter)
- {
- map[hero.x + 1, hero.y] = map[hero.x, hero.y + 1] = map[hero.x - 1, hero.y] = map[hero.x,hero.y-1]=' ';
- }*/
- //Draw map
- Console.Clear();
- for (int i = 0; i < map.GetLength(0);i++)
- {
- Console.Write("#");
- for (int j = 0; j < map.GetLength(1);j++)
- {
- Console.Write(map[i, j]);
- }
- Console.WriteLine("#");
- }
- // Stay in the grid
- if (hero.x <= 0)
- {
- hero.x += 1;
- }
- if (hero.x > height)
- {
- hero.x -= 1;
- }
- if (hero.y < 0)
- {
- hero.y += 1;
- }
- if (hero.y >= width)
- {
- hero.y -= 1;
- }
- //write info
- Console.WriteLine("Life: " + hero.life +"HP");
- Console.WriteLine("RULES : \n O is the objective to reach. \n x are the ennemies \n + is your character");
- Console.WriteLine("x= " + hero.x + "y= " + hero.y);
- //move the character
- Console.SetCursorPosition(hero.x , hero.y);
- Console.Write("+");
- }
- public int is_over(int r_x, int r_y)
- {
- if (hero.x == r_x && hero.y == r_y )
- {
- return (0);
- }
- if (hero.life == 0)
- {
- return (1);
- }
- else
- {
- return (2);
- }
- }
- public class Character
- {
- public int life { get; set; }
- public int x{ get; set;}
- public int y { get; set;}
- public Character(int life, int x, int y)
- {
- this.x = x;
- this.y = y;
- this.life = life;
- }
- public void move(int dx, int dy)
- {
- x += dx;
- y += dy;
- }
- }
- public class Ennemy : Character
- {
- public Ennemy(int life, int x, int y): base(life,x,y)
- {
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement