Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <vector>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define UP 1
- #define RIGHT 2
- #define DOWN 3
- #define LEFT 4
- using namespace std;
- char Map[500][500], EmptySpaceChar = ' ', WallChar = '#', PlayerChar = 'O', AIChar = 'X', BulletChar = (char)249;
- int MapWidth, MapHeight;
- int PlayerX, PlayerY, PlayerX_prev, PlayerY_prev, PlayerSpeed, PlayerDirection, PlayerHealth;
- bool needDrawMap = true;
- class Wall
- {
- public:
- int X, Y;
- };
- class Bullet
- {
- public:
- int X, Y, Direction, X_prev, Y_prev, Speed = 1;
- void MakeMove()
- {
- X_prev = X;
- Y_prev = Y;
- if ( Direction == UP ) {
- for( int i=0 ; i < Speed ; i++)
- if( Y-1 > 0 )
- Y--;
- }
- else if ( Direction == DOWN ) {
- for( int i=0 ; i < Speed ; i++)
- if( Y+1 < MapHeight )
- Y++;
- }
- else if ( Direction == LEFT ) {
- for( int i= 0 ; i < Speed ; i++ )
- if( X-1 > 0 )
- X--;
- }
- else if ( Direction == RIGHT ) {
- for( int i=0 ; i < Speed ; i++)
- if( X+1 < MapWidth )
- X++;
- }
- }
- bool CheckDelete()
- {
- /// daca map[][] e altceva
- }
- void Update()
- {
- Map[Y_prev][X_prev] = EmptySpaceChar;
- Map[Y][X] = BulletChar;
- if( Y_prev != Y || X_prev != X )
- needDrawMap = true;
- }
- };
- class AI
- {
- public:
- int X, Y, X_prev, Y_prev, Speed=1, Direction, Health = 100;
- void MakeMove()
- {
- Direction = rand() % 8 + 1;
- X_prev = X;
- Y_prev = Y;
- if ( Direction == UP ) {
- for( int i=0 ; i < Speed ; i++)
- if( Y-1 > 0 )
- if( Map[Y-1][X] == EmptySpaceChar)
- Y--;
- }
- else if ( Direction == DOWN ) {
- for( int i=0 ; i < Speed ; i++)
- if( Y+1 < MapHeight )
- if( Map[Y+1][X] == EmptySpaceChar)
- Y++;
- }
- else if ( Direction == LEFT ) {
- for( int i= 0 ; i < Speed ; i++ )
- if( X-1 > 0 )
- if( Map[Y][X-1] == EmptySpaceChar)
- X--;
- }
- else if ( Direction == RIGHT ) {
- for( int i=0 ; i < Speed ; i++)
- if( X+1 < MapWidth )
- if( Map[Y][X+1] == EmptySpaceChar)
- X++;
- }
- }
- void Update()
- {
- Map[Y_prev][X_prev] = EmptySpaceChar;
- Map[Y][X] = AIChar;
- if( Y_prev != Y || X_prev != X )
- needDrawMap = true;
- }
- };
- vector<Wall> WallList;
- vector<AI> AIList;
- vector<Bullet> BulletList;
- void PrepareForTheAwesomeness();
- void KeyInput();
- void UpdatePlayer();
- void DrawMapAroundPlayer();
- int main()
- {
- PrepareForTheAwesomeness();
- while(1)
- {
- if( needDrawMap )
- DrawMapAroundPlayer();
- KeyInput();
- UpdatePlayer();
- for( int i=0 ; i<AIList.size() ; i++)
- {
- AIList[i].MakeMove();
- AIList[i].Update();
- if( AIList[i].Health <= 0 )
- AIList.erase( AIList.begin() + i );
- }
- }
- return 0;
- }
- void PrepareForTheAwesomeness()
- {
- cin >> MapWidth >> MapHeight;
- PlayerX = 3;
- PlayerY = 3;
- PlayerSpeed = 1;
- PlayerDirection = UP;
- PlayerHealth = 100;
- srand(time(NULL));
- for( int y=0 ; y<=MapHeight ; y++ )
- for( int x=0 ; x<=MapWidth ; x++ )
- Map[y][x] = EmptySpaceChar;
- for( int i=0 ; i<=MapHeight ; i++ )
- {
- Wall wall;
- wall.X = 0;
- wall.Y = i;
- WallList.push_back(wall);
- Map[wall.Y][wall.X] = WallChar;
- wall.X = MapWidth;
- wall.Y = i;
- WallList.push_back(wall);
- Map[wall.Y][wall.X] = WallChar;
- }
- for( int i=0 ; i<=MapWidth ; i++ )
- {
- Wall wall;
- wall.X = i;
- wall.Y = 0;
- WallList.push_back(wall);
- Map[wall.Y][wall.X] = WallChar;
- wall.X = i;
- wall.Y = MapHeight;
- WallList.push_back(wall);
- Map[wall.Y][wall.X] = WallChar;
- }
- Wall wall;
- for( int y=5 ; y<=MapHeight ; y+=5 )
- for( int x=1 ; x<=MapWidth-8 ; x++ )
- if( y % 10 == 5 )
- {
- wall.X = MapWidth-x;
- wall.Y = y;
- WallList.push_back(wall);
- Map[wall.Y][wall.X] = WallChar;
- }
- else
- {
- wall.X = x;
- wall.Y = y;
- WallList.push_back(wall);
- Map[wall.Y][wall.X] = WallChar;
- }
- AI ai;
- ai.X = 17;
- ai.Y = 3;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- ai.X = 17;
- ai.Y = 8;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- ai.X = 17;
- ai.Y = 13;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- ai.X = 17;
- ai.Y = 18;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- ai.X = 3;
- ai.Y = 8;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- ai.X = 3;
- ai.Y = 13;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- ai.X = 3;
- ai.Y = 18;
- Map[ai.Y][ai.X] = AIChar;
- AIList.push_back(ai);
- Map[PlayerY][PlayerX] = PlayerChar;
- }
- void KeyInput()
- {
- PlayerX_prev = PlayerX;
- PlayerY_prev = PlayerY;
- if (GetAsyncKeyState(38) || GetAsyncKeyState(87)) { //UP
- for( int i=0 ; i < PlayerSpeed ; i++)
- if( PlayerY-1 > 0 )
- if( Map[PlayerY-1][PlayerX] == EmptySpaceChar)
- {
- PlayerDirection = UP;
- PlayerY--;
- }
- }
- else if (GetAsyncKeyState(40) || GetAsyncKeyState(83)) { //DOWN
- for( int i=0 ; i < PlayerSpeed ; i++)
- if( PlayerY+1 < MapHeight )
- if( Map[PlayerY+1][PlayerX] == EmptySpaceChar)
- {
- PlayerDirection = DOWN;
- PlayerY++;
- }
- }
- else if (GetAsyncKeyState(37) || GetAsyncKeyState(65)) { //LEFT
- for( int i= 0 ; i < PlayerSpeed ; i++ )
- if( PlayerX-1 > 0 )
- if( Map[PlayerY][PlayerX-1] == EmptySpaceChar)
- {
- PlayerDirection = LEFT;
- PlayerX--;
- }
- }
- else if (GetAsyncKeyState(39) || GetAsyncKeyState(68)) { //RIGHT
- for( int i=0 ; i < PlayerSpeed ; i++)
- if( PlayerX+1 < MapWidth )
- if( Map[PlayerY][PlayerX+1] == EmptySpaceChar)
- {
- PlayerDirection = RIGHT;
- PlayerX++;
- }
- }
- if( GetAsyncKeyState(16) ) //SHIFT RIGHT
- {
- Map[1][1] = BulletChar;
- }
- Sleep(70);
- }
- void UpdatePlayer()
- {
- Map[PlayerY_prev][PlayerX_prev] = EmptySpaceChar;
- Map[PlayerY][PlayerX] = PlayerChar;
- if( PlayerY_prev != PlayerY || PlayerX_prev != PlayerX )
- needDrawMap = true;
- }
- void DrawMapAroundPlayer()
- {
- system("cls");
- cout << endl << endl << endl;
- cout << " Game Play " << endl;
- cout << " ";
- for( int i=0 ; i<=14 ; i++ )
- cout << (char)219;
- cout << endl;
- for( int y=-6 ; y<=6 ; y++ )
- {
- cout << " " << (char)219;
- for( int x=-6 ; x<=6 ; x++ )
- {
- if( PlayerX+x < 0 || PlayerX+x > MapWidth || PlayerY+y > MapHeight || PlayerY+y < 0 )
- cout << WallChar;
- else
- cout << Map[PlayerY+y][PlayerX+x];
- }
- cout << (char)219 << endl;
- }
- cout << " ";
- for( int i=0 ; i<=14 ; i++ )
- cout << (char)219;
- cout << endl;
- cout << endl << " Player's Direction: ";
- if( PlayerDirection == UP )
- cout << "UP" << endl;
- else if( PlayerDirection == DOWN )
- cout << "DOWN" << endl;
- else if( PlayerDirection == LEFT )
- cout << "LEFT" << endl;
- else
- cout << "RIGHT" << endl;
- cout << " Player's Position: (" << PlayerX << "," << PlayerY << ")" << endl;
- cout << " Player's Health: " << PlayerHealth << endl;
- needDrawMap = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment