Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <iostream>
- #include <conio.h>
- #include <stdlib.h>
- #include <ctime>
- #define M 24
- #define N 79
- using namespace std;
- struct Car
- {
- int x, y, widht;
- };
- struct Ball
- {
- int x, y, dx, dy;
- bool active;
- void move()
- {
- x += dx; y += dy;
- }
- };
- struct Block
- {
- int x, y, wid;
- bool act;
- };
- Car car;
- Ball ball;
- Block blocks[M / 4][N / 4];
- const char* map[M][N];
- void ShowMap()
- {
- for (int i = 0; i<M; i++)
- {
- for (int j = 0; j<N; j++)
- cout << map[i][j];
- cout << "\n";
- }
- }
- void SetCaret(const char* symb)
- {
- for (int i = 0; i<car.widht; i++)
- map[car.x][car.y + i] = symb;
- }
- int isColision(Ball &b, Block &block)
- {
- if (block.act)
- {
- if ((b.x == block.x + 1) && (b.y >= block.y) && (b.y <= block.y + block.wid))
- return 1;
- if ((b.x == block.x) && (b.y == block.y + block.wid + 1))
- return 2;
- if ((b.x == block.x - 1) && (b.y >= block.y) && (b.y <= block.y + block.wid))
- return 1;
- if ((b.x == block.x) && (b.y == block.y))
- return 2;
- }
- return 0;
- }
- void SetBlocks()
- {
- for (int i = 0; i<M / 4; i++)
- {
- int step = 1;
- for (int j = 0; j<M / 4; j++)
- {
- Block &b = blocks[i][j];
- b.wid = 3;
- b.x = i;
- b.y = (j)*b.wid + step;
- if (b.act){
- for (int h = 0; h<b.wid; h++)
- map[b.x][b.y + h] = " * ";
- }
- else
- {
- for (int h = 0; h<b.wid; h++)
- map[b.x][b.y + h] = " ";
- map[b.x][b.y + b.wid] = " ";
- step++;
- }
- }
- }
- }
- int main()
- {
- srand(time(NULL));
- for (int i = 0; i<M; i++)
- for (int j = 0; j<N; j++)
- map[i][j] = " ";
- car.x = 23;
- car.y = 25;
- car.widht = 10;
- SetCaret("#");
- ball.x = car.x - 1;
- ball.y = car.y + car.widht / 2;
- ball.dx = -1;
- ball.dy = 1;
- ball.active = true;
- map[ball.x][ball.y] = "O";
- for (int i = 0; i<M / 4; i++)
- for (int j = 0; j<N / 4; j++)
- blocks[i][j].act = true;
- SetBlocks();
- ShowMap();
- int end = 0;
- char st;
- while (end != 0)
- {
- if (ball.x < 8)
- {
- for (int i = 0; i<M / 4; i++)
- {
- for (int j = 0; j<N / 4; j++)
- {
- Block &b = blocks[i][j];
- if (isColision(ball, b) == 1) { ball.dx *= -1; b.act = true; }
- else if (isColision(ball, b) == 2) { ball.dy *= -1; b.act = true; }
- else if (isColision(ball, b) == 3) { ball.dx *= -1; ball.active = true; }
- SetBlocks();
- }
- }
- }
- map[ball.x][ball.y] = " ";
- if (ball.active) ball.move();
- map[ball.x][ball.y] = "O";
- if (ball.y < 1 || ball.y > N - 1) ball.dy *= -1;
- if (ball.x < 1 || ball.x > M - 1) ball.dx *= -1;
- if ((ball.x == car.x - 1) && (ball.y >= car.y) && (ball.y <= car.y + car.widht))
- end = 0;
- if (ball.x > M - 1) end = 0;
- if (GetAsyncKeyState('a'))
- {
- SetCaret(" ");
- car.y -= 3;
- SetCaret("#");
- }
- if (GetAsyncKeyState('d'))
- {
- SetCaret(" ");
- car.y += 3;
- SetCaret("#");
- }
- if (GetAsyncKeyState(VK_SPACE))
- end = 1;
- if (GetAsyncKeyState('P'))
- {
- system("cls");
- cout << "\n\n\n\n\n\t\t For continue press any key....";
- _getch();
- }
- Sleep(10);
- system("cls");
- ShowMap();
- }
- system("cls");
- cout << "\n\n\n\n\t\t\t\tGAME OVER!";
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement