From 4aaf4d9cb8dfddc8436600071e18216e465f31d3 Mon Sep 17 00:00:00 2001 From: Tomasz Dzierzbicki Date: Fri, 20 Mar 2020 19:18:54 +0000 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.cpp | 242 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 Main.cpp diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..ebe698a --- /dev/null +++ b/Main.cpp @@ -0,0 +1,242 @@ +#include +#include +#include +#include +#include + +using namespace std; + +char pole[27][27][2]; +int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + +void color(string foregroundColor,string backgroundColor) +{ + HANDLE hOut; + hOut = GetStdHandle(STD_OUTPUT_HANDLE); + int foregroundCode = 15; + if (foregroundColor == "black") + foregroundCode = 0; + if (foregroundColor == "dark_blue") + foregroundCode = 1; + if (foregroundColor == "green") + foregroundCode = 2; + if (foregroundColor == "cyan") + foregroundCode = 3; + if (foregroundColor == "dark_red") + foregroundCode = 4; + if (foregroundColor == "purple") + foregroundCode = 5; + if (foregroundColor == "dark_yellow") + foregroundCode = 6; + if (foregroundColor == "light_gray") + foregroundCode = 7; + if (foregroundColor == "gray") + foregroundCode = 8; + if (foregroundColor == "blue") + foregroundCode = 9; + if (foregroundColor == "lime") + foregroundCode = 10; + if (foregroundColor == "light_blue") + foregroundCode = 11; + if (foregroundColor == "red") + foregroundCode = 12; + if (foregroundColor == "magenta") + foregroundCode = 13; + if (foregroundColor == "yellow") + foregroundCode = 14; + if (foregroundColor == "white") + foregroundCode = 15; + + int backgroundCode = 0; + if (backgroundColor == "black") + backgroundCode = 0; + if (backgroundColor == "dark_blue") + backgroundCode = 1; + if (backgroundColor == "green") + backgroundCode = 2; + if (backgroundColor == "cyan") + backgroundCode = 3; + if (backgroundColor == "dark_red") + backgroundCode = 4; + if (backgroundColor == "purple") + backgroundCode = 5; + if (backgroundColor == "dark_yellow") + backgroundCode = 6; + if (backgroundColor == "light_gray") + backgroundCode = 7; + if (backgroundColor == "gray") + backgroundCode = 8; + if (backgroundColor == "blue") + backgroundCode = 9; + if (backgroundColor == "lime") + backgroundCode = 10; + if (backgroundColor == "light_blue") + backgroundCode = 11; + if (backgroundColor == "red") + backgroundCode = 12; + if (backgroundColor == "magenta") + backgroundCode = 13; + if (backgroundColor == "yellow") + backgroundCode = 14; + if (backgroundColor == "white") + backgroundCode = 15; + SetConsoleTextAttribute(hOut, foregroundCode + backgroundCode*16); +} + +void SetWindow(int Width, int Height) +{ + _COORD coord; + coord.X = Width; + coord.Y = Height; + + _SMALL_RECT Rect; + Rect.Top = 0; + Rect.Left = 0; + Rect.Bottom = Height - 1; + Rect.Right = Width - 1; + + HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle + SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size + SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size +} + +void updatePola() +{ + system("cls"); + for (int i = 0; i < 27; i++) + { + for (int j = 0; j < 27; j++) + { + char item = pole[i][j][0]; + switch (item) + { + case 'B': + { + color("purple", "dark_yellow"); + }break; + case 'T': + { + color("red", "dark_yellow"); + }break; + case '.': + { + color("yellow", "dark_yellow"); + }break; + case '#': + { + color("light_gray", "gray"); + }break; + } + cout << pole[i][j][0]; + + } + cout << endl; + color("white", "black"); + } +} + +void Move(char kierunek) +{ + switch (kierunek) + { + //góra-(w) + case 'w': + { + if (pole[pozycjaTraktoraX - 1][pozycjaTraktoraY][0] != '#') + { + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; + pozycjaTraktoraX--; + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; + + } + updatePola(); + }break; + //dół-(s) + case 's': + { + if (pole[pozycjaTraktoraX + 1][pozycjaTraktoraY][0] != '#') + { + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; + pozycjaTraktoraX++; + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; + + } + updatePola(); + }break; + //lewo-(a) + case 'a': + { + if (pole[pozycjaTraktoraX][pozycjaTraktoraY - 1][0] != '#') + { + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; + pozycjaTraktoraY--; + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; + + } + updatePola(); + }break; + //prawo-(d) + case 'd': + { + if (pole[pozycjaTraktoraX][pozycjaTraktoraY + 1][0] != '#') + { + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; + pozycjaTraktoraY++; + pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; + + } + updatePola(); + }break; + } +} + +int main() +{ + SetWindow(50, 30); + + //create pola// + for (int i = 0; i < 27; i++) + { + pole[i][0][0] = '#'; + pole[0][i][0] = '#'; + pole[26][i][0] = '#'; + pole[i][26][0] = '#'; + } + for (int i = 1; i < 26; i++) + { + for (int j = 1; j < 26; j++) + { + pole[i][j][0] = '.'; + } + } + + pole[1][1][0] = 'T'; + + for (int i = 0; i < 25; i++) + { + pole[i+1][i+1][0] = 'B'; + } + updatePola(); + + + //---------start---------// + bool traktorDziala = true; + + char akcja; + + do + { + akcja = _getch(); + if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd') + { + Move(akcja); + } + if (akcja == '0') + { + traktorDziala = false; + } + } while (traktorDziala); + //---------end---------// + + return 0; +}