Advertisement
mvaganov

CmdLineGame

Mar 26th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. namespace CmdLineGame {
  3.     class MainClass {
  4.         class Entity {
  5.             public int x = 2, y = 3;            // the position of my game character
  6.             public char playerIcon = '@';
  7.             public Entity(int x, int y, char c) {
  8.                 this.x = x;
  9.                 this.y = y;
  10.                 this.playerIcon = c;
  11.             }
  12.             public bool IsHere(int y, int x) {
  13.                 return this.y == y && this.x == x;
  14.             }
  15.             public void Draw() { Console.Write (playerIcon); }
  16.         }
  17.         Entity[] players = new Entity[]{
  18.             new Entity (2, 3, '@'),
  19.             new Entity (9, 4, '$')
  20.         };
  21.         bool gameIsRunning = true;  // state of the game
  22.         int input;                  // the input state from the user
  23.         int width = 10, height = 5;
  24.         public void DoGame() {
  25.             while (gameIsRunning) { // <-- game loop
  26.                 // draw/render the game
  27.                 Console.SetCursorPosition (0, 0);
  28.                 for (int row = 0; row < height; row++) {
  29.                     for (int col = 0; col < width; col++) {
  30.                         bool foundPlayerHere = false;
  31.                         for (int i = 0; i < players.Length; i++) {
  32.                             if (players [i].IsHere (row, col)) {
  33.                                 players [i].Draw ();
  34.                                 foundPlayerHere = true;
  35.                                 break;
  36.                             }
  37.                         }
  38.                         if(!foundPlayerHere) {
  39.                             Console.Write ('.');
  40.                         }
  41.                     }
  42.                     Console.Write ('\n');
  43.                 }
  44.                 // get input from the user
  45.                 input = Console.Read ();
  46.                 Update();
  47.             }
  48.         }
  49.         public void Update() {
  50.             if (input == 'w') { players[0].y--; }
  51.             if (input == 's') { players[0].y++; }
  52.             if (input == 'a') { players[0].x--; }
  53.             if (input == 'd') { players[0].x++; }
  54.             if (input == 27) { gameIsRunning = false; }
  55.             if (players [0].x == players [1].x && players [0].y == players [1].y) {
  56.                 Console.WriteLine ("\nYou Win!");
  57.                 gameIsRunning = false;
  58.             }
  59.         }
  60.         // Main is 'static' so it can be accessed before anything is instantiated
  61.         public static void Main (string[] args) {
  62.             MainClass mc = new MainClass ();
  63.             mc.DoGame ();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement