Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace TankGame
- {
- class Level
- {
- Tank[] roster;
- int rosterCount = 0;
- protected Entity[,] gameField;
- int width;
- int height;
- public Level(int height, int width, int[,] codeField)
- {
- roster = new Tank[4];
- gameField = new Entity[height, width];
- this.width = width;
- this.height = height;
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- if (codeField[i, j] == 1)
- {
- gameField[i, j] = new Brick(i, j);
- }
- else if (codeField[i, j] == 2)
- {
- gameField[i, j] = new Plank(i, j);
- }
- else if (codeField[i, j] == 3)
- {
- gameField[i, j] = new Water(i, j);
- }
- else if (codeField[i, j] == 4)
- {
- gameField[i, j] = new Tank(i, j, false, this);
- roster[rosterCount] = (Tank)gameField[i, j];
- rosterCount++;
- }
- else if (codeField[i, j] == 5)
- {
- gameField[i, j] = new Tank(i, j, true, this);
- roster[rosterCount] = (Tank)gameField[i, j];
- rosterCount++;
- }
- }
- }
- }
- public void DrawAll()
- {
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- if (gameField[i, j] == null)
- {
- Console.Write(' ');
- }else
- {
- gameField[i, j].Draw();
- }
- }
- Console.WriteLine();
- }
- }
- }
- abstract class Entity
- {
- protected char symbol;
- protected ConsoleColor color;
- protected int code;
- protected int X;
- protected int Y;
- public void Draw()
- {
- Console.ForegroundColor = color;
- Console.Write(this.symbol);
- Console.ResetColor();
- }
- }
- abstract class Block : Entity
- {
- }
- class Brick : Block
- {
- public Brick(int spawnX, int spawnY)
- {
- symbol = '#';
- code = 1;
- color = ConsoleColor.Red;
- this.X = spawnX;
- this.Y = spawnY;
- }
- }
- class Plank : Block
- {
- public Plank(int spawnX, int spawnY)
- {
- symbol = '=';
- code = 2;
- color = ConsoleColor.DarkYellow;
- this.X = spawnX;
- this.Y = spawnY;
- }
- }
- class Water : Block
- {
- public Water(int spawnX, int spawnY)
- {
- symbol = '~';
- code = 3;
- color = ConsoleColor.Cyan;
- this.X = spawnX;
- this.Y = spawnY;
- }
- }
- abstract class Mob : Entity
- {
- protected Level level;
- protected int direction; //1 for up, 2 for right, 3 for down, 4 for left
- void Move()
- {
- switch (direction)
- {
- /* case 1:
- if(level.gameField) // doesn't work
- break;
- case 2:
- break;
- case 3:
- break;
- case 4:
- break;*/
- }
- }
- }
- class Bullet : Mob
- {
- bool friendly;
- public Bullet(int spawnX, int spawnY, int spawnDirection, bool friendly)
- {
- symbol = '*';
- color = ConsoleColor.DarkGray;
- this.X = spawnX;
- this.Y = spawnY;
- direction = spawnDirection;
- this.friendly = friendly;
- }
- }
- class Tank : Mob
- {
- bool isHero;
- Bullet shot;
- public Tank(int spawnX, int spawnY, bool isPlayer, Level sourceLevel)
- {
- symbol = '^';
- this.X = spawnX;
- this.Y = spawnY;
- direction = 1;
- if (!isPlayer)
- {
- color = ConsoleColor.DarkGreen;
- code = 4;
- isHero = false;
- }
- else
- {
- color = ConsoleColor.Magenta;
- code = 5;
- isHero = true;
- }
- level = sourceLevel;
- }
- }
- class Program
- {
- private const int fieldHeight = 11;
- private const int fieldWidth = 20;
- private static bool buttonPressed;
- static void Main(string[] args)
- {
- ConsoleKeyInfo cki = new ConsoleKeyInfo();
- int[,] codeField = new int[fieldHeight, fieldWidth]
- {
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //0 for empty
- {1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1}, //1 for brick
- {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1}, //2 for plank
- {1,0,2,0,0,2,3,3,3,3,3,3,3,3,2,0,0,2,0,1}, //3 for water
- {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1}, //4 for enemy spawn
- {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1}, //5 for player spawn
- {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1},
- {1,0,2,0,0,2,3,3,3,3,3,3,3,3,2,0,0,2,0,1},
- {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1},
- {1,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
- };
- Level testLevel = new Level(fieldHeight, fieldWidth, codeField);
- testLevel.DrawAll();
- /* int i = 0;
- while (true)
- {
- buttonPressed = false;
- while (Console.KeyAvailable == false)
- {
- Console.Clear();
- testLevel.DrawAll();
- System.Threading.Thread.Sleep(500);
- }
- buttonPressed = true; //test
- if (buttonPressed)
- {
- cki = Console.ReadKey(true);
- buttonPressed = false;
- }
- System.Threading.Thread.Sleep(100);
- Console.WriteLine("You pressed the '{0}' key.", cki.Key);
- }*/
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement