Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace CmdLineGame2 {
- class MainClass {
- public static void Main (string[] args) {
- MainClass mc = new MainClass ();
- mc.DoGame ();
- }
- class Entity {
- public MainClass game;
- public int x = 2, y = 3;
- public char face = '@';
- public Entity(MainClass g, int x, int y, char c) {
- this.game = g; this.x = x; this.y = y; this.face = c;
- }
- public void Draw() { Console.Write (face); }
- public bool IsAt(int x, int y) { return this.x == x && this.y == y; }
- public virtual void Update() {}
- }
- class Player : Entity {
- public Player(MainClass g, int x, int y, char c):base(g,x,y,c){}
- public override void Update() {
- if (game.input == 'w') { y --; }
- if (game.input == 's') { y ++; }
- if (game.input == 'a') { x = x - 1; }
- if (game.input == 'd') { x += 1; }
- }
- }
- class RandomEnemy : Entity {
- public RandomEnemy(MainClass g, int x, int y, char c)
- :base(g,x,y,c) { }
- public override void Update() {
- Random r = new Random ();
- string possibleMoves = "wasd ";
- int randomNumber = r.Next ();
- int cappedBetween0And5 = randomNumber % 5;
- char whichMove = possibleMoves[ cappedBetween0And5 ];
- if (whichMove == 'w') { y --; }
- if (whichMove == 's') { y ++; }
- if (whichMove == 'a') { x = x - 1; }
- if (whichMove == 'd') { x += 1; }
- }
- }
- Entity[] players;
- public MainClass() {
- players = new Entity[]{
- new Player(this, 2, 3, '@'), // 0 player
- new RandomEnemy(this, 4, 4, 'o') // 1
- };
- }
- int width = 10, height = 8, input;
- bool gameRunning = true;
- int GetPlayerAt(int col, int row) {
- for (int p = 0; p < players.Length; p++) {
- if (players [p].IsAt(col, row)) {
- return p;
- }
- }
- return -1;
- }
- void Draw() {
- Console.SetCursorPosition (0, 0);
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- int playerHere = GetPlayerAt(col, row);
- if (playerHere == -1) {
- Console.Write ('.');
- } else {
- players [playerHere].Draw ();
- }
- }
- Console.Write ('\n');
- }
- }
- void Input() {
- if (Console.KeyAvailable) {
- do {
- input = Console.Read ();
- } while(input == '\r' || input == '\n'); // FIXME handle '\n' and '\r' line endings without a loop
- } else {
- DateTime start = DateTime.Now;
- // throttle code - pausing to keep framerate more consistent
- while ((DateTime.Now - start).TotalMilliseconds < 100 && !Console.KeyAvailable) {
- System.Threading.Thread.Sleep (1);
- }
- input = -1;
- }
- }
- void Update() {
- for (int i = 0; i < players.Length; i++) {
- players [i].Update ();
- }
- if (input == 27) { gameRunning = false; }
- if(players[0].x < 0) { Console.WriteLine("YOU LOSE!"); gameRunning = false; }
- if(players[0].x >= width) { Console.WriteLine("YOU WIN!!"); gameRunning = false; }
- if (players[0].x == players[1].x && players[0].y == players[1].y) { // if the player reaches the coin
- Console.Write ("\a"); // play a sound
- }
- }
- public void DoGame() {
- while (gameRunning) {
- Draw (); // draws, or "renders" the game
- Input (); // do nothing but get input from user as quickly as possible
- Update (); // handle all changes to state, esp. based on user input and AI
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement