Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7.  
  8. namespace TankGame
  9. {
  10.  
  11.     class Level
  12.     {
  13.         Tank[] roster;
  14.         int rosterCount = 0;
  15.        protected Entity[,] gameField;
  16.         int width;
  17.         int height;
  18.         public Level(int height, int width, int[,] codeField)
  19.         {
  20.             roster = new Tank[4];
  21.             gameField = new Entity[height, width];
  22.             this.width = width;
  23.             this.height = height;
  24.             for (int i = 0; i < height; i++)
  25.             {
  26.  
  27.                 for (int j = 0; j < width; j++)
  28.                 {
  29.  
  30.                     if (codeField[i, j] == 1)
  31.                     {
  32.                         gameField[i, j] = new Brick(i, j);
  33.                     }
  34.                     else if (codeField[i, j] == 2)
  35.                     {
  36.                         gameField[i, j] = new Plank(i, j);
  37.                     }
  38.                     else if (codeField[i, j] == 3)
  39.                     {
  40.                         gameField[i, j] = new Water(i, j);
  41.                     }
  42.                     else if (codeField[i, j] == 4)
  43.                     {  
  44.                         gameField[i, j] = new Tank(i, j, false, this);
  45.                         roster[rosterCount] = (Tank)gameField[i, j];
  46.                         rosterCount++;
  47.  
  48.                        
  49.                     }
  50.                     else if (codeField[i, j] == 5)
  51.                     {
  52.                         gameField[i, j] = new Tank(i, j, true, this);
  53.                         roster[rosterCount] = (Tank)gameField[i, j];
  54.                         rosterCount++;
  55.                     }
  56.  
  57.  
  58.                 }
  59.             }
  60.         }
  61.         public void DrawAll()
  62.         {
  63.             for (int i = 0; i < height; i++)
  64.             {
  65.  
  66.                 for (int j = 0; j < width; j++)
  67.                 {
  68.                     if (gameField[i, j] == null)
  69.                     {
  70.                         Console.Write(' ');
  71.                     }else
  72.                     {
  73.                         gameField[i, j].Draw();
  74.                     }
  75.  
  76.                 }
  77.                 Console.WriteLine();
  78.             }
  79.         }
  80.     }
  81.     abstract class Entity
  82.     {
  83.         protected char symbol;
  84.         protected ConsoleColor color;
  85.         protected int code;
  86.         protected int X;
  87.         protected int Y;
  88.         public void Draw()
  89.         {
  90.             Console.ForegroundColor = color;
  91.             Console.Write(this.symbol);
  92.             Console.ResetColor();
  93.         }
  94.  
  95.     }
  96.  
  97.     abstract class Block : Entity
  98.     {
  99.  
  100.     }
  101.  
  102.     class Brick : Block
  103.     {
  104.         public Brick(int spawnX, int spawnY)
  105.         {
  106.             symbol = '#';
  107.             code = 1;
  108.             color = ConsoleColor.Red;
  109.             this.X = spawnX;
  110.             this.Y = spawnY;
  111.         }
  112.     }
  113.  
  114.     class Plank : Block
  115.     {
  116.         public Plank(int spawnX, int spawnY)
  117.         {
  118.             symbol = '=';
  119.             code = 2;
  120.             color = ConsoleColor.DarkYellow;
  121.             this.X = spawnX;
  122.             this.Y = spawnY;
  123.         }
  124.     }
  125.  
  126.     class Water : Block
  127.     {
  128.         public Water(int spawnX, int spawnY)
  129.         {
  130.             symbol = '~';
  131.             code = 3;
  132.             color = ConsoleColor.Cyan;
  133.             this.X = spawnX;
  134.             this.Y = spawnY;
  135.         }
  136.     }
  137.  
  138.     abstract class Mob : Entity
  139.     {
  140.        protected Level level;
  141.         protected int direction; //1 for up, 2 for right, 3 for down, 4 for left
  142.  
  143.         void Move()
  144.         {
  145.            switch (direction)
  146.             {
  147.             /*    case 1:
  148.                     if(level.gameField) // doesn't work
  149.                     break;
  150.                 case 2:
  151.                     break;
  152.                 case 3:
  153.                     break;
  154.                 case 4:
  155.                     break;*/
  156.             }
  157.         }
  158.     }
  159.     class Bullet : Mob
  160.     {
  161.         bool friendly;
  162.         public Bullet(int spawnX, int spawnY, int spawnDirection, bool friendly)
  163.         {
  164.             symbol = '*';
  165.             color = ConsoleColor.DarkGray;
  166.             this.X = spawnX;
  167.             this.Y = spawnY;
  168.             direction = spawnDirection;
  169.             this.friendly = friendly;
  170.            
  171.         }
  172.     }
  173.     class Tank : Mob
  174.     {
  175.         bool isHero;
  176.         Bullet shot;
  177.        
  178.         public Tank(int spawnX, int spawnY, bool isPlayer, Level sourceLevel)
  179.         {
  180.             symbol = '^';
  181.             this.X = spawnX;
  182.             this.Y = spawnY;
  183.             direction = 1;
  184.             if (!isPlayer)
  185.             {
  186.                 color = ConsoleColor.DarkGreen;
  187.                 code = 4;
  188.                 isHero = false;
  189.             }
  190.             else
  191.             {
  192.                 color = ConsoleColor.Magenta;
  193.                 code = 5;
  194.                 isHero = true;
  195.             }
  196.             level = sourceLevel;
  197.  
  198.  
  199.         }
  200.  
  201.     }
  202.  
  203.  
  204.  
  205.    
  206.  
  207.     class Program
  208.     {
  209.      
  210.         private const int fieldHeight = 11;
  211.         private const int fieldWidth = 20;
  212.         private static bool buttonPressed;
  213.         static void Main(string[] args)
  214.         {
  215.             ConsoleKeyInfo cki = new ConsoleKeyInfo();
  216.             int[,] codeField = new int[fieldHeight, fieldWidth]
  217.           {
  218.                 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //0 for empty
  219.                 {1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1}, //1 for brick
  220.                 {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1}, //2 for plank
  221.                 {1,0,2,0,0,2,3,3,3,3,3,3,3,3,2,0,0,2,0,1}, //3 for water
  222.                 {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1}, //4 for enemy spawn
  223.                 {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1}, //5 for player spawn
  224.                 {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1},
  225.                 {1,0,2,0,0,2,3,3,3,3,3,3,3,3,2,0,0,2,0,1},
  226.                 {1,0,2,0,0,2,0,0,0,0,0,0,0,0,2,0,0,2,0,1},
  227.                 {1,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,1},
  228.                 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
  229.           };
  230.             Level testLevel = new Level(fieldHeight, fieldWidth, codeField);
  231.             testLevel.DrawAll();
  232.          /*   int i = 0;
  233.             while (true)
  234.             {
  235.  
  236.                 buttonPressed = false;
  237.                 while (Console.KeyAvailable == false)
  238.                 {
  239.                    
  240.                     Console.Clear();
  241.                     testLevel.DrawAll();
  242.                     System.Threading.Thread.Sleep(500);
  243.                    
  244.                 }
  245.                 buttonPressed = true; //test
  246.                 if (buttonPressed)
  247.                 {
  248.                     cki = Console.ReadKey(true);
  249.                     buttonPressed = false;
  250.                 }
  251.                
  252.                 System.Threading.Thread.Sleep(100);
  253.                 Console.WriteLine("You pressed the '{0}' key.", cki.Key);
  254.                
  255.             }*/
  256.         }
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement