Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace CmdLineGame {
- class MainClass {
- class Entity {
- public int x = 2, y = 3; // the position of my game character
- public char playerIcon = '@';
- public Entity(int x, int y, char c) {
- this.x = x;
- this.y = y;
- this.playerIcon = c;
- }
- public bool IsHere(int y, int x) {
- return this.y == y && this.x == x;
- }
- public void Draw() { Console.Write (playerIcon); }
- }
- Entity[] players = new Entity[]{
- new Entity (2, 3, '@'),
- new Entity (9, 4, '$')
- };
- bool gameIsRunning = true; // state of the game
- int input; // the input state from the user
- int width = 10, height = 5;
- public void DoGame() {
- while (gameIsRunning) { // <-- game loop
- // draw/render the game
- Console.SetCursorPosition (0, 0);
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- bool foundPlayerHere = false;
- for (int i = 0; i < players.Length; i++) {
- if (players [i].IsHere (row, col)) {
- players [i].Draw ();
- foundPlayerHere = true;
- break;
- }
- }
- if(!foundPlayerHere) {
- Console.Write ('.');
- }
- }
- Console.Write ('\n');
- }
- // get input from the user
- input = Console.Read ();
- Update();
- }
- }
- public void Update() {
- if (input == 'w') { players[0].y--; }
- if (input == 's') { players[0].y++; }
- if (input == 'a') { players[0].x--; }
- if (input == 'd') { players[0].x++; }
- if (input == 27) { gameIsRunning = false; }
- if (players [0].x == players [1].x && players [0].y == players [1].y) {
- Console.WriteLine ("\nYou Win!");
- gameIsRunning = false;
- }
- }
- // Main is 'static' so it can be accessed before anything is instantiated
- public static void Main (string[] args) {
- MainClass mc = new MainClass ();
- mc.DoGame ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement