Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <cstdlib>
- #include <iostream>
- #include <map>
- #include <conio.h>
- #include <time.h>
- #include <windows.h>
- #include <math.h>
- #include <string.h>
- #include <stdio.h>
- #define SPACEBAR 0x20
- #define UP_ARROW 0x26
- #define DOWN_ARROW 0x28
- #define LEFT_ARROW 0x25
- #define RIGHT_ARROW 0x27
- #define ESCAPE 0x1B
- #define DASH_CHAR ((char)-51)
- #define SHIP_CHAR ((char)-53)
- #define PLAYER_CHAR ((char)-54)
- #define BULLET_CHAR ((char)-70)
- #define CLEAR_COMMAND "cls"
- #define GOLD (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY)
- #define GREEN (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
- #define BROWN (FOREGROUND_RED | FOREGROUND_GREEN)
- #define RED (FOREGROUND_RED | FOREGROUND_INTENSITY)
- #define DEFAULT_COLOR (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
- using namespace std;
- enum Direction {
- UP, DOWN, LEFT, RIGHT
- };
- struct Bullet {
- int x, y;
- Direction direction;
- };
- struct Ship {
- int x, y;
- int width = 2;
- int height = 0;
- };
- struct Player {
- int x, y;
- int width = 2;
- int height = 0;
- };
- int offsetX;
- int offsetY;
- int sizeX;
- int sizeY;
- float speed;
- map<int, Ship> ships;
- map<int, Bullet> bullets;
- Direction direction = RIGHT;
- Player player;
- int score = 0;
- bool isActive = true;
- int endAnimation = 0;
- bool shoot = false;
- int index = 0;
- int movement = 0;
- int movementCooldown = 0;
- bool pressed = false;
- void configuration() {
- offsetX = 3;
- offsetY = 1;
- sizeX = 50;
- sizeY = 20;
- speed = 0.01;
- struct Player pl;
- pl.x = 25;
- pl.y = 17;
- player = pl;
- }
- void doGameOver() {
- system(CLEAR_COMMAND);
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hConsole, RED);
- cout << "\t\t _ __ _ " << endl;
- cout << "\t\t| |/ / (_) " << endl;
- cout << "\t\t| ' / ___ _ __ _ ___ ___ __ _ _ __ _ _ " << endl;
- cout << "\t\t| < / _ \\| '_ \\| |/ _ \\/ __| / _` | '__| | | |" << endl;
- cout << "\t\t| . \\ (_) | | | | | __/ (__ | (_| | | | |_| |" << endl;
- cout << "\t\t|_|\\_\\___/|_| |_|_|\\___|\\___| \\__, |_| \\__, |" << endl;
- cout << "\t\t __/ | __/ |" << endl;
- cout << "\t\t |___/ |___/ " << endl;
- SetConsoleTextAttribute(hConsole, GOLD);
- cout << "\t\t\t\tZdobyte punkty: " << score << endl;
- SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
- }
- bool isItMyBullet(int x, int y) {
- for (map<int, Bullet>::iterator i = bullets.begin(); i != bullets.end(); i++) {
- if (i->second.x == x && i->second.y == y && i->second.direction == UP) return true;
- }
- return false;
- }
- bool isItBullet(int x, int y) {
- for (map<int, Bullet>::iterator i = bullets.begin(); i != bullets.end(); i++) {
- if (i->second.x == x && i->second.y == y && i->second.direction == DOWN) return true;
- }
- return false;
- }
- bool isItShip(int x, int y) {
- for (map<int, Ship>::iterator i = ships.begin(); i != ships.end(); i++) {
- 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;
- }
- return false;
- }
- bool isItPlayer(int x, int y) {
- return x >= player.x && x <= player.x + player.width && y >= player.y && y <= player.y + player.height;
- }
- int getShip(int x, int y) {
- for (map<int, Ship>::iterator i = ships.begin(); i != ships.end(); i++) {
- 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;
- }
- return -1;
- }
- char getShipChar(int x, int y) {
- Ship ship = ships[getShip(x, y)];
- if (ship.x + (ship.width / 2) + movement == x) return SHIP_CHAR;
- else return DASH_CHAR;
- }
- char getPlayerChar(int x, int y) {
- if (player.x + (player.width / 2) == x) return PLAYER_CHAR;
- else return DASH_CHAR;
- }
- void addMyBullet() {
- struct Bullet b;
- b.x = player.x + (player.width/2);
- b.y = player.y - 1;
- b.direction = UP;
- bullets[index] = b;
- index++;
- }
- void addBullet(Ship ship) {
- struct Bullet b;
- b.x = ship.x + (ship.width/2) + movement;
- b.y = ship.y + ship.height;
- b.direction = DOWN;
- bullets[index] = b;
- index++;
- }
- void addBullets() {
- for (map<int, Ship>::iterator i = ships.begin(); i != ships.end(); i++) {
- if (rand() % 3 == 0)
- addBullet(i->second);
- }
- }
- void init() {
- srand(NULL);
- for (int i = 0; i < 7; i++) {
- struct Ship ship;
- ship.x = i * 7 + 2;
- ship.y = 4;
- ships[i] = ship;
- }
- }
- void reset() {
- doGameOver();
- cin.get();
- configuration();
- shoot = false;
- movement = 0;
- movementCooldown = 0;
- pressed = false;
- index = 0;
- direction = RIGHT;
- score = 0;
- endAnimation = 0;
- isActive = true;
- bullets.clear();
- ships.clear();
- system(CLEAR_COMMAND);
- init();
- }
- bool wait(float seconds)
- {
- clock_t endwait;
- endwait = clock() + seconds * CLOCKS_PER_SEC;
- while (clock() < endwait) {
- if (GetAsyncKeyState(ESCAPE)) {
- doGameOver();
- return false;
- }
- }
- if ((GetAsyncKeyState(UP_ARROW) || GetAsyncKeyState(SPACEBAR)) && !pressed)
- {
- shoot = true;
- pressed = true;
- }
- else if (!(GetAsyncKeyState(UP_ARROW) || GetAsyncKeyState(SPACEBAR)) && pressed) pressed = false;
- if (GetAsyncKeyState(LEFT_ARROW))
- {
- player.x--;
- if (player.x < 0) player.x = 0;
- }
- if (GetAsyncKeyState(RIGHT_ARROW))
- {
- player.x++;
- if (player.x + player.width >= sizeX) player.x = sizeX - 1 - player.width;
- }
- return true;
- }
- void printInColor(char str, WORD color) {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hConsole, color);
- cout << str;
- SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
- }
- void printInColorArr(char str[], WORD color) {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hConsole, color);
- cout << str;
- SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
- }
- void printInColorInt(int str, WORD color) {
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hConsole, color);
- cout << str;
- SetConsoleTextAttribute(hConsole, DEFAULT_COLOR);
- }
- void buildMap() {
- for (int q = 0; q<offsetY; q++) {
- cout << endl;
- }
- for (int q = 0; q<offsetX; q++) {
- cout << " ";
- }
- for (int i = 0; i < sizeX + 2; i++) {
- printInColor((char)-36, BROWN);
- }
- cout << endl;
- for (int j = 0; j < sizeY; j++) {
- for (int q = 0; q<offsetX; q++) {
- cout << " ";
- }
- printInColor((char)-37, BROWN);
- for (int i = 0; i < sizeX; i++) {
- if (isItPlayer(i, j)) printInColor(getPlayerChar(i, j), GREEN);
- else if (isItMyBullet(i, j)) printInColor(BULLET_CHAR, GREEN);
- else if (isItBullet(i, j)) printInColor(BULLET_CHAR, GOLD);
- else if (isItShip(i, j)) printInColor(getShipChar(i, j), RED);
- else cout << " ";
- }
- if (j == 0) {
- printInColor((char)-37, BROWN);
- printInColorArr("Punkty: ", GOLD);
- printInColorInt(score, GOLD);
- cout << endl;
- }
- else {
- printInColor((char)-37, BROWN);
- cout << endl;
- }
- }
- for (int q = 0; q<offsetX; q++) {
- cout << " ";
- }
- printInColor((char)-37, BROWN);
- for (int i = 0; i < sizeX; i++) {
- printInColor((char)-36, BROWN);
- }
- printInColor((char)-37, BROWN);
- }
- int translateY(int y, int dir) {
- if (dir == DOWN) y = y + 1;
- else if (dir == UP) y = y - 1;
- if (y < 0 || y >= sizeY) return -1;
- return y;
- }
- void logicStuff() {
- movementCooldown++;
- if (movementCooldown >= 5) {
- movementCooldown = 0;
- if (direction == RIGHT) {
- movement++;
- if (movement >= 3) {
- movement = movement - 2;
- direction = LEFT;
- }
- }
- else if (direction == LEFT) {
- movement--;
- if (movement <= -2) {
- movement = movement + 2;
- direction = RIGHT;
- }
- }
- addBullets();
- }
- if (shoot) {
- addMyBullet();
- shoot = false;
- }
- if (endAnimation == 25) reset();
- if (!isActive) {
- endAnimation++;
- return;
- }
- {
- int toErase[255];
- int ind = 0;
- for (map<int, Bullet>::iterator i = bullets.begin(); i != bullets.end(); i++) {
- int tempY = translateY(i->second.y, i->second.direction);
- if (tempY == -1) {
- toErase[ind] = i->first;
- ind++;
- }
- else {
- i->second.y = tempY;
- int ship = getShip(i->second.x, i->second.y);
- if (ship != -1) {
- ships.erase(ship);
- toErase[ind] = i->first;
- ind++;
- score++;
- if (ships.size() == 0) isActive = false;
- }
- if (isItPlayer(i->second.x, i->second.y)) {
- isActive = false;
- }
- }
- }
- for (int i = 0; i < ind; i++) {
- bullets.erase(toErase[i]);
- }
- }
- if (!isActive) reset();
- }
- int main()
- {
- configuration();
- init();
- while (true) {
- COORD cur = { 0, 0 };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
- logicStuff();
- buildMap();
- if (!wait(speed)) break;
- }
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement