Advertisement
mvaganov

CmdLineGame2.cs

Mar 26th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. namespace CmdLineGame2 {
  3.     class MainClass {
  4.         public static void Main (string[] args) {
  5.             MainClass mc = new MainClass ();
  6.             mc.DoGame ();
  7.         }
  8.         class Entity {
  9.             public MainClass game;
  10.             public int x = 2, y = 3;
  11.             public char face = '@';
  12.             public Entity(MainClass g, int x, int y, char c) {
  13.                 this.game = g; this.x = x; this.y = y; this.face = c;
  14.             }
  15.             public void Draw() { Console.Write (face); }
  16.             public bool IsAt(int x, int y) { return this.x == x && this.y == y; }
  17.             public virtual void Update() {}
  18.         }
  19.         class Player : Entity {
  20.             public Player(MainClass g, int x, int y, char c):base(g,x,y,c){}
  21.             public override void Update() {
  22.                 if (game.input == 'w') { y --; }
  23.                 if (game.input == 's') { y ++; }
  24.                 if (game.input == 'a') { x = x - 1; }
  25.                 if (game.input == 'd') { x += 1; }
  26.             }
  27.         }
  28.         class RandomEnemy : Entity {
  29.             public RandomEnemy(MainClass g, int x, int y, char c)
  30.                 :base(g,x,y,c) { }
  31.             public override void Update() {
  32.                 Random r = new Random ();
  33.                 string possibleMoves = "wasd ";
  34.                 int randomNumber = r.Next ();
  35.                 int cappedBetween0And5 = randomNumber % 5;
  36.                 char whichMove = possibleMoves[ cappedBetween0And5 ];
  37.                 if (whichMove == 'w') { y --; }
  38.                 if (whichMove == 's') { y ++; }
  39.                 if (whichMove == 'a') { x = x - 1; }
  40.                 if (whichMove == 'd') { x += 1; }
  41.             }
  42.         }
  43.         Entity[] players;
  44.         public MainClass() {
  45.             players = new Entity[]{
  46.                 new Player(this, 2, 3, '@'), // 0 player
  47.                 new RandomEnemy(this, 4, 4, 'o')  // 1
  48.             };
  49.         }
  50.         int width = 10, height = 8, input;
  51.         bool gameRunning = true;
  52.         int GetPlayerAt(int col, int row) {
  53.             for (int p = 0; p < players.Length; p++) {
  54.                 if (players [p].IsAt(col, row)) {
  55.                     return p;
  56.                 }
  57.             }
  58.             return -1;
  59.         }
  60.         void Draw() {
  61.             Console.SetCursorPosition (0, 0);
  62.             for (int row = 0; row < height; row++) {
  63.                 for (int col = 0; col < width; col++) {
  64.                     int playerHere = GetPlayerAt(col, row);
  65.                     if (playerHere == -1) {
  66.                         Console.Write ('.');
  67.                     } else {
  68.                         players [playerHere].Draw ();
  69.                     }
  70.                 }
  71.                 Console.Write ('\n');
  72.             }
  73.         }
  74.         void Input() {
  75.             if (Console.KeyAvailable) {
  76.                 do {
  77.                     input = Console.Read ();
  78.                 } while(input == '\r' || input == '\n'); // FIXME handle '\n' and '\r' line endings without a loop
  79.             } else {
  80.                 DateTime start = DateTime.Now;
  81.                 // throttle code - pausing to keep framerate more consistent
  82.                 while ((DateTime.Now - start).TotalMilliseconds < 100 && !Console.KeyAvailable) {
  83.                     System.Threading.Thread.Sleep (1);
  84.                 }
  85.                 input = -1;
  86.             }
  87.         }
  88.         void Update() {
  89.             for (int i = 0; i < players.Length; i++) {
  90.                 players [i].Update ();
  91.             }
  92.             if (input == 27) { gameRunning = false; }
  93.             if(players[0].x < 0) { Console.WriteLine("YOU LOSE!"); gameRunning = false; }
  94.             if(players[0].x >= width) { Console.WriteLine("YOU WIN!!"); gameRunning = false; }
  95.             if (players[0].x == players[1].x && players[0].y == players[1].y) { // if the player reaches the coin
  96.                 Console.Write ("\a");                       // play a sound
  97.             }
  98.         }
  99.         public void DoGame() {
  100.             while (gameRunning) {
  101.                 Draw ();   // draws, or "renders" the game
  102.                 Input ();  // do nothing but get input from user as quickly as possible
  103.                 Update (); // handle all changes to state, esp. based on user input and AI
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement