Advertisement
stirante

Untitled

Nov 30th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.84 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <map>
  8. #include <conio.h>
  9. #include <time.h>
  10. #include <windows.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14.  
  15. #define SPACEBAR 0x20
  16. #define UP_ARROW 0x26
  17. #define DOWN_ARROW 0x28
  18. #define LEFT_ARROW 0x25
  19. #define RIGHT_ARROW 0x27
  20. #define ESCAPE 0x1B
  21.  
  22. #define DASH_CHAR ((char)-51)
  23. #define SHIP_CHAR ((char)-53)
  24. #define PLAYER_CHAR ((char)-54)
  25. #define BULLET_CHAR ((char)-70)
  26.  
  27. #define CLEAR_COMMAND "cls"
  28.  
  29. #define GOLD (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  30. #define GREEN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
  31. #define BROWN (FOREGROUND_RED | FOREGROUND_GREEN)
  32. #define RED (FOREGROUND_RED | FOREGROUND_INTENSITY)
  33. #define DEFAULT_COLOR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
  34.  
  35. using namespace std;
  36.  
  37. enum Direction {
  38.     UP, DOWN, LEFT, RIGHT
  39. };
  40.  
  41. struct Bullet {
  42.     int x, y;
  43.     Direction direction;
  44. };
  45.  
  46. struct Ship {
  47.     int x, y;
  48.     int width = 2;
  49.     int height = 0;
  50. };
  51.  
  52. struct Player {
  53.     int x, y;
  54.     int width = 2;
  55.     int height = 0;
  56. };
  57.  
  58. int offsetX;
  59. int offsetY;
  60. int sizeX;
  61. int sizeY;
  62. float speed;
  63.  
  64. map<int, Ship> ships;
  65. map<int, Bullet> bullets;
  66. Direction direction = RIGHT;
  67. Player player;
  68. int score = 0;
  69. bool isActive = true;
  70. int endAnimation = 0;
  71. bool shoot = false;
  72. int index = 0;
  73. int movement = 0;
  74. int movementCooldown = 0;
  75. bool pressed = false;
  76.  
  77. void configuration() {
  78.     offsetX = 3;
  79.     offsetY = 1;
  80.     sizeX = 50;
  81.     sizeY = 20;
  82.     speed = 0.01;
  83.     struct Player pl;
  84.     pl.x = 25;
  85.     pl.y = 17;
  86.     player = pl;
  87. }
  88.  
  89. void doGameOver() {
  90.     system(CLEAR_COMMAND);
  91.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  92.     SetConsoleTextAttribute(hConsole, RED);
  93.     cout << "\t\t _  __           _                              " << endl;
  94.     cout << "\t\t| |/ /          (_)                             " << endl;
  95.     cout << "\t\t| ' / ___  _ __  _  ___  ___    __ _ _ __ _   _ " << endl;
  96.     cout << "\t\t|  < / _ \\| '_ \\| |/ _ \\/ __|  / _` | '__| | | |" << endl;
  97.     cout << "\t\t| . \\ (_) | | | | |  __/ (__  | (_| | |  | |_| |" << endl;
  98.     cout << "\t\t|_|\\_\\___/|_| |_|_|\\___|\\___|  \\__, |_|   \\__, |" << endl;
  99.     cout << "\t\t                                __/ |      __/ |" << endl;
  100.     cout << "\t\t                               |___/      |___/ " << endl;
  101.     SetConsoleTextAttribute(hConsole, GOLD);
  102.     cout << "\t\t\t\tZdobyte punkty: " << score << endl;
  103.     SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
  104. }
  105.  
  106. bool isItMyBullet(int x, int y) {
  107.     for (map<int, Bullet>::iterator i = bullets.begin(); i != bullets.end(); i++) {
  108.         if (i->second.x == x && i->second.y == y && i->second.direction == UP) return true;
  109.     }
  110.     return false;
  111. }
  112. bool isItBullet(int x, int y) {
  113.     for (map<int, Bullet>::iterator i = bullets.begin(); i != bullets.end(); i++) {
  114.         if (i->second.x == x && i->second.y == y && i->second.direction == DOWN) return true;
  115.     }
  116.     return false;
  117. }
  118. bool isItShip(int x, int y) {
  119.     for (map<int, Ship>::iterator i = ships.begin(); i != ships.end(); i++) {
  120.         if (x >= i->second.x + movement && x <= i->second.x + movement + i->second.width && y >= i->second.y && y <= i->second.y + i->second.height) return true;
  121.     }
  122.     return false;
  123. }
  124.  
  125. bool isItPlayer(int x, int y) {
  126.     return x >= player.x && x <= player.x + player.width && y >= player.y && y <= player.y + player.height;
  127. }
  128. int getShip(int x, int y) {
  129.     for (map<int, Ship>::iterator i = ships.begin(); i != ships.end(); i++) {
  130.         if (x >= i->second.x + movement && x <= i->second.x + movement + i->second.width && y >= i->second.y && y <= i->second.y + i->second.height) return i->first;
  131.     }
  132.     return -1;
  133. }
  134.  
  135. char getShipChar(int x, int y) {
  136.     Ship ship = ships[getShip(x, y)];
  137.     if (ship.x + (ship.width / 2) + movement == x) return SHIP_CHAR;
  138.     else return DASH_CHAR;
  139. }
  140.  
  141. char getPlayerChar(int x, int y) {
  142.     if (player.x + (player.width / 2) == x) return PLAYER_CHAR;
  143.     else return DASH_CHAR;
  144. }
  145.  
  146. void addMyBullet() {
  147.     struct Bullet b;
  148.     b.x = player.x + (player.width/2);
  149.     b.y = player.y - 1;
  150.     b.direction = UP;
  151.     bullets[index] = b;
  152.     index++;
  153. }
  154. void addBullet(Ship ship) {
  155.     struct Bullet b;
  156.     b.x = ship.x + (ship.width/2) + movement;
  157.     b.y = ship.y + ship.height;
  158.     b.direction = DOWN;
  159.     bullets[index] = b;
  160.     index++;
  161. }
  162. void addBullets() {
  163.     for (map<int, Ship>::iterator i = ships.begin(); i != ships.end(); i++) {
  164.         if (rand() % 3 == 0)
  165.             addBullet(i->second);
  166.     }
  167. }
  168.  
  169. void init() {
  170.     srand(NULL);
  171.     for (int i = 0; i < 7; i++) {
  172.         struct Ship ship;
  173.         ship.x = i * 7 + 2;
  174.         ship.y = 4;
  175.         ships[i] = ship;
  176.     }
  177. }
  178.  
  179. void reset() {
  180.     doGameOver();
  181.     cin.get();
  182.     configuration();
  183.     shoot = false;
  184.     movement = 0;
  185.     movementCooldown = 0;
  186.     pressed = false;
  187.     index = 0;
  188.     direction = RIGHT;
  189.     score = 0;
  190.     endAnimation = 0;
  191.     isActive = true;
  192.     bullets.clear();
  193.     ships.clear();
  194.     system(CLEAR_COMMAND);
  195.     init();
  196. }
  197.  
  198. bool wait(float seconds)
  199. {
  200.     clock_t endwait;
  201.     endwait = clock() + seconds * CLOCKS_PER_SEC;
  202.     while (clock() < endwait) {
  203.         if (GetAsyncKeyState(ESCAPE)) {
  204.             doGameOver();
  205.             return false;
  206.         }
  207.     }
  208.     if ((GetAsyncKeyState(UP_ARROW) || GetAsyncKeyState(SPACEBAR)) && !pressed)
  209.     {
  210.         shoot = true;
  211.         pressed = true;
  212.     }
  213.     else if (!(GetAsyncKeyState(UP_ARROW) || GetAsyncKeyState(SPACEBAR)) && pressed) pressed = false;
  214.     if (GetAsyncKeyState(LEFT_ARROW))
  215.     {
  216.         player.x--;
  217.         if (player.x < 0) player.x = 0;
  218.     }
  219.     if (GetAsyncKeyState(RIGHT_ARROW))
  220.     {
  221.         player.x++;
  222.         if (player.x + player.width >= sizeX) player.x = sizeX - 1 - player.width;
  223.     }
  224.     return true;
  225. }
  226.  
  227. void printInColor(char str, WORD color) {
  228.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  229.     SetConsoleTextAttribute(hConsole, color);
  230.     cout << str;
  231.     SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
  232. }
  233. void printInColorArr(char str[], WORD color) {
  234.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  235.     SetConsoleTextAttribute(hConsole, color);
  236.     cout << str;
  237.     SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
  238. }
  239. void printInColorInt(int str, WORD color) {
  240.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  241.     SetConsoleTextAttribute(hConsole, color);
  242.     cout << str;
  243.     SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
  244. }
  245.  
  246. void buildMap() {
  247.     for (int q = 0; q<offsetY; q++) {
  248.         cout << endl;
  249.     }
  250.     for (int q = 0; q<offsetX; q++) {
  251.         cout << " ";
  252.     }
  253.     for (int i = 0; i < sizeX + 2; i++) {
  254.         printInColor((char)-36, BROWN);
  255.     }
  256.     cout << endl;
  257.     for (int j = 0; j < sizeY; j++) {
  258.         for (int q = 0; q<offsetX; q++) {
  259.             cout << " ";
  260.         }
  261.         printInColor((char)-37, BROWN);
  262.         for (int i = 0; i < sizeX; i++) {
  263.             if (isItPlayer(i, j)) printInColor(getPlayerChar(i, j), GREEN);
  264.             else if (isItMyBullet(i, j)) printInColor(BULLET_CHAR, GREEN);
  265.             else if (isItBullet(i, j)) printInColor(BULLET_CHAR, GOLD);
  266.             else if (isItShip(i, j)) printInColor(getShipChar(i, j), RED);
  267.             else cout << " ";
  268.         }
  269.         if (j == 0) {
  270.             printInColor((char)-37, BROWN);
  271.             printInColorArr("Punkty: ", GOLD);
  272.             printInColorInt(score, GOLD);
  273.             cout << endl;
  274.         }
  275.         else {
  276.             printInColor((char)-37, BROWN);
  277.             cout << endl;
  278.         }
  279.     }
  280.     for (int q = 0; q<offsetX; q++) {
  281.         cout << " ";
  282.     }
  283.     printInColor((char)-37, BROWN);
  284.     for (int i = 0; i < sizeX; i++) {
  285.         printInColor((char)-36, BROWN);
  286.     }
  287.     printInColor((char)-37, BROWN);
  288. }
  289.  
  290. int translateY(int y, int dir) {
  291.     if (dir == DOWN) y = y + 1;
  292.     else if (dir == UP) y = y - 1;
  293.     if (y < 0 || y >= sizeY) return -1;
  294.     return y;
  295. }
  296.  
  297. void logicStuff() {
  298.     movementCooldown++;
  299.     if (movementCooldown >= 5) {
  300.         movementCooldown = 0;
  301.         if (direction == RIGHT) {
  302.             movement++;
  303.             if (movement >= 3) {
  304.                 movement = movement - 2;
  305.                 direction = LEFT;
  306.             }
  307.         }
  308.         else if (direction == LEFT) {
  309.             movement--;
  310.             if (movement <= -2) {
  311.                 movement = movement + 2;
  312.                 direction = RIGHT;
  313.             }
  314.         }
  315.         addBullets();
  316.     }
  317.     if (shoot) {
  318.         addMyBullet();
  319.         shoot = false;
  320.     }
  321.     if (endAnimation == 25) reset();
  322.     if (!isActive) {
  323.         endAnimation++;
  324.         return;
  325.     }
  326.     {
  327.         int toErase[255];
  328.         int ind = 0;
  329.         for (map<int, Bullet>::iterator i = bullets.begin(); i != bullets.end(); i++) {
  330.             int tempY = translateY(i->second.y, i->second.direction);
  331.             if (tempY == -1) {
  332.                 toErase[ind] = i->first;
  333.                 ind++;
  334.             }
  335.             else {
  336.                 i->second.y = tempY;
  337.                 int ship = getShip(i->second.x, i->second.y);
  338.                 if (ship != -1) {
  339.                     ships.erase(ship);
  340.                     toErase[ind] = i->first;
  341.                     ind++;
  342.                     score++;
  343.                     if (ships.size() == 0) isActive = false;
  344.                 }
  345.                 if (isItPlayer(i->second.x, i->second.y)) {
  346.                     isActive = false;
  347.                 }
  348.             }
  349.         }
  350.         for (int i = 0; i < ind; i++) {
  351.             bullets.erase(toErase[i]);
  352.         }
  353.     }
  354.     if (!isActive) reset();
  355. }
  356.  
  357. int main()
  358. {
  359.     configuration();
  360.     init();
  361.     while (true) {
  362.         COORD cur = { 0, 0 };
  363.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
  364.         logicStuff();
  365.         buildMap();
  366.         if (!wait(speed)) break;
  367.     }
  368.     cin.get();
  369.     return 0;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement