SI2020/Main.cpp

243 lines
5.1 KiB
C++
Raw Normal View History

2020-03-20 20:18:54 +01:00
#include<iostream>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<string>
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<><64>-(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;
}