-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
90 lines (82 loc) ยท 2.17 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "game.h"
#include <iostream>
#include <cstdlib>
#include <conio.h>
Game::Game(int w, int h) {
numberOfLanes = h;
width = w;
quit = false;
score = 0;
level = 1;
for (int i = 0; i < numberOfLanes; i++)
map.push_back(new Lane(width));
player = new Player(width);
}
Game::~Game() {
delete player;
for (int i = 0; i < map.size(); i++) {
Lane* current = map.back();
map.pop_back();
delete current;
}
}
void Game::Draw() {
system("cls");
for (int i = 0; i < numberOfLanes; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 && (j == 0 || j == width - 1)) std::cout << "_";
else if (i == numberOfLanes - 1 && (j == 0 || j == width - 1)) std::cout << "___";
else if (map[i]->CheckPos(j) && i != 0 && i != numberOfLanes - 1) std::cout << "#";
else if (player->x == j && player->y == i) std::cout << "V";
else std::cout << " ";
}
std::cout << std::endl;
}
std::cout << "Level: " << level << " Score: " << score << std::endl;
}
void Game::Input() {
if (_kbhit()) {
char current = _getch();
switch (current) {
case 'a':
player->MoveLeft();
break;
case 'd':
player->MoveRight();
break;
case 'w':
player->MoveUp();
break;
case 's':
player->MoveDown();
break;
case 'q':
quit = true;
break;
}
}
}
void Game::Logic() {
for (int i = 1; i < numberOfLanes - 1; i++) {
if (rand() % 10 == 1)
map[i]->Move();
if (map[i]->CheckPos(player->x) && player->y == i)
quit = true;
}
if (player->y == numberOfLanes - 1) {
score += level * 10;
player->y = 0;
std::cout << "\x07";
map[rand() % numberOfLanes]->ChangeDirection();
if (score % 50 == 0) {
level++;
}
}
}
void Game::Run() {
while (!quit) {
Input();
Draw();
Logic();
}
}