Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.44 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.  
  7. namespace ConsoleApp8
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = false;
  14.             bool isPlaying = true;
  15.             int HeroX, HeroY;
  16.             int HeroDX = 0, HeroDY = 1;
  17.             int collectBerries = 0;
  18.             int ollBerries = 0;
  19.  
  20.             char[,] Map = {
  21.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
  22.                 {'#','@','.','.','.','.','.','.','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','.','.','.','.','.','.','.','#'},
  23.                 {'#','.','#','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','#','.','#'},
  24.                 {'#','.','#','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','#','.','#'},
  25.                 {'#','.','#','#','#','#','#','#','#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#','#','#','#','#','#','#','.','#'},
  26.                 {'#','.','.','.','.','.','.','.','#','.','#','#','#','#','#','#','.','#','.','#','#','#','#','#','#','.','#','.','.','.','.','.','.','.','#'},
  27.                 {'#','.','.','.','.','.','.','.','#','.','#','.','.','.','.','.','.','#','.','.','.','.','.','.','#','.','#','.','.','.','.','.','.','.','#'},
  28.                 {'#','.','.','.','.','.','.','.','#','.','#','.','.','.','.','.','.','#','.','.','.','.','.','.','#','.','#','.','.','.','.','.','.','.','#'},
  29.                 {'#','.','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#','.','#'},
  30.                 {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
  31.                 {'#','.','#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#','.','#'},
  32.                 {'#','.','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','.','#'},
  33.                 {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
  34.                 {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'} };
  35.  
  36.             DrawMap(Map, out HeroX, out HeroY, ref ollBerries);
  37.  
  38.             while (isPlaying)
  39.             {
  40.                 Console.SetCursorPosition(0, 15);
  41.                 Console.WriteLine($"Собрано: {collectBerries}/{ollBerries}");
  42.  
  43.                 if (Console.KeyAvailable)
  44.                 {
  45.                     ConsoleKeyInfo key = Console.ReadKey(true);
  46.                     changeDirection(key, ref HeroDX, ref HeroDY);
  47.                 }
  48.                 if (Map[HeroX + HeroDX, HeroY + HeroDY] != '#')
  49.                 {
  50.                     Move(ref HeroX, ref HeroY, HeroDX, HeroDY);
  51.                     if (Map[HeroX, HeroY] == '.')
  52.                     {
  53.                         collectBerries++;
  54.                         Map[HeroX, HeroY] = ' ';
  55.                     }
  56.                 }
  57.                 if (collectBerries == ollBerries)
  58.                 {
  59.                     Console.SetCursorPosition(0, 16);
  60.                     Console.ForegroundColor = ConsoleColor.Green;
  61.                     Console.WriteLine("ВЫ ПОБЕДИЛИ!!!");
  62.                     isPlaying = false;
  63.                 }
  64.                 System.Threading.Thread.Sleep(250);
  65.             }
  66.         }
  67.         static void DrawMap(char[,] map, out int HeroX, out int HeroY, ref int ollBerries)
  68.         {
  69.             HeroX = 0;
  70.             HeroY = 0;
  71.             for (int i = 0; i < map.GetLength(0); i++)
  72.             {
  73.                 for (int j = 0; j < map.GetLength(1); j++)
  74.                 {
  75.                     Console.Write(map[i, j]);
  76.                     if (map[i, j] == '@')
  77.                     {
  78.                         HeroX = i;
  79.                         HeroY = j;
  80.                     }
  81.                     else if (map[i,j] == '.')
  82.                     {
  83.                         ollBerries++;
  84.                     }
  85.                 }
  86.                 Console.WriteLine();
  87.             }
  88.         }
  89.         static void Move (ref int x, ref int y, int DX, int DY)
  90.         {
  91.             Console.SetCursorPosition(y, x);
  92.             Console.Write(' ');
  93.             x += DX;
  94.             y += DY;
  95.             Console.SetCursorPosition(y, x);
  96.             Console.Write('@');
  97.         }
  98.         static void changeDirection (ConsoleKeyInfo key, ref int DX, ref int DY)
  99.         {
  100.             switch (key.Key)
  101.             {
  102.                 case ConsoleKey.UpArrow:
  103.                     DX = -1; DY = 0;
  104.                     break;
  105.                 case ConsoleKey.LeftArrow:
  106.                     DX = 0; DY = -1;
  107.                     break;
  108.                 case ConsoleKey.DownArrow:
  109.                     DX = 1; DY = 0;
  110.                     break;
  111.                 case ConsoleKey.RightArrow:
  112.                     DX = 0; DY = 1;
  113.                     break;
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement