-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
165 lines (139 loc) · 3.29 KB
/
Main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Main.cpp: îïðåäåëÿåò òî÷êó âõîäà äëÿ êîíñîëüíîãî ïðèëîæåíèÿ.
//
#include "stdafx.h"
#include "Main.h"
#include "Screen.h"
#include "vector"
using namespace std;
using namespace constSpace;
/*
bool gameOver;
int sizeOfField = 30;
int x, y, score;
eDirection direction;
void Run() {
gameOver = false;
direction = UP;
x = sizeOfField / 2;
y = sizeOfField - 1;
}
void Draw(Player& playerTank) {
system("cls");
int x = playerTank.getX();
int y = playerTank.getY();
eDirection dir = playerTank.getDirection();
for (int i = 0; i < sizeOfField; ++i)
{
for (int j = 0; j < sizeOfField; j++)
{
if (i == 0 || i == sizeOfField - 1 || j == 0 || j == sizeOfField - 1) cout << "#";
else if ((i == x && j == y)||(i == x - 1 && j == y)||(i == x + 1 && j == y)
||(i == x && j == y - 1)||(i == x && j == y + 1))
{
cout << char(219);
}
else if (((i == x + 1 && j == y - 1)
|| (i == x + 1 && j == y + 1))
&& dir == UP)
cout << char(219);
else if (((i == x - 1 && j == y - 1)
|| (i == x - 1 && j == y + 1))
&& dir == DOWN)
cout << char(219);
else if (((i == x - 1 && j == y + 1)
|| (i == x + 1 && j == y + 1))
&& dir == LEFT)
cout << char (219);
else if (((i == x - 1 && j == y - 1)
|| (i == x + 1 && j == y - 1))
&& dir == RIGHT)
cout << char(219);
else cout << ' ';
}
cout << endl;
}
}
void test(){
int ch;
while ((ch = _getch()) != 27) / * 27 = Esc key * /
{
printf("%d", ch);
if (ch == 0 || ch == 224)
printf(", %d", _getch());
printf("\n");
}
printf("ESC %d\n", ch);
}
*/
void Fire(vector<BaseScreenObject*>& objects, Tank& tank)
{
auto* shot = new Shot(&tank);
shot->SetPosDir(tank.X(), tank.Y(), tank.GetDirection());
objects.push_back(shot);
}
int main()
{
Screen screen;
screen.Prepare();
Tank tank1;
Tank tankE1(34, 65, DOWN);
Tank tankE2(22,7,UP);
Tank tankE3(6, 30, UP);
Tank tankE4(2, 43, DOWN);
tank1.SetPosDir(10, 10, eDirection::UP);
vector<BaseScreenObject*> objects;
objects.push_back(&tank1);
objects.push_back(&tankE1);
objects.push_back(&tankE2);
objects.push_back(&tankE3);
objects.push_back(&tankE4);
while (true) // main loop
{
screen.Prepare();
if (kbhit())
{
auto ch = getch();
if (ch != 0)
{
switch (ch)
{
case 119: tank1.Move(screen, eDirection::UP); break;
case 115: tank1.Move(screen, eDirection::DOWN); break;
case 97: tank1.Move(screen, eDirection::LEFT); break;
case 100: tank1.Move(screen, eDirection::RIGHT); break;
case ' ': Fire(objects, tank1); break;
}
}
}
int i = 0;
while (i < objects.size())
{
objects[i]->Update(screen);
Shot* shot = dynamic_cast<Shot*>(objects[i]);
if (shot && shot->IsObsolete())
{
objects.erase(objects.begin() + i);
continue;
}
i++;
}
for (int i = 0; i < objects.size(); i++)
{
objects[i]->Draw(screen, objects[i]);
}
screen.Draw();
Sleep(50);
}
screen.Draw();
getch();
/*
system("mode con cols=100 lines=40");
Run();
Player playerTank((sizeOfField - 1) / 2, (sizeOfField - 1) / 2, UP);
while (!gameOver) {
Draw(playerTank);
playerTank.Move(playerTank, sizeOfField);
}
*/
return 0;
}