-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamer.h
113 lines (86 loc) · 1.72 KB
/
gamer.h
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
#pragma once
#include <windows.h>
class Gamer
{
public:
Gamer(COORD coord) : direction(RIGHT), coord{0}
{
this->coord.X = coord.X;
this->coord.Y = coord.Y;
map[coord.Y][coord.X] = 'O';
SetConsoleCursorPosition(hd, coord);
printf("O");
}
COORD getCoord()
{
return this->coord;
}
direct_t getDirection()
{
return direction;
}
void setCoord(COORD coord)
{
if (this->coord.X != coord.X || this->coord.Y != coord.Y)
{
if (map[coord.Y][coord.X] != ' ') return;
map[this->coord.Y][this->coord.X] = ' ';
SetConsoleCursorPosition(hd, this->coord);
printf(" ");
this->coord.X = coord.X;
this->coord.Y = coord.Y;
map[this->coord.Y][this->coord.X] = 'O';
SetConsoleCursorPosition(hd, coord);
printf("O");
}
}
void action()
{
if (GetKeyState('D') < 0)
{
COORD coord = this->coord;
coord.X++;
setCoord(coord);
direction = RIGHT;
}
if (GetKeyState('A') < 0)
{
COORD coord = this->getCoord();
coord.X--;
setCoord(coord);
direction = LEFT;
}
if (GetKeyState('W') < 0)
{
COORD coord = this->getCoord();
coord.Y--;
setCoord(coord);
direction = UP;
}
if (GetKeyState('S') < 0)
{
COORD coord = this->getCoord();
coord.Y++;
setCoord(coord);
direction = DOWN;
}
if (GetKeyState(VK_SPACE) < 0)
{
int index = 0;
for (index = 0; index < 10; index++)
if (!bullets[index]) break;
if (!bullets[index])
if (direction == RIGHT || direction == LEFT)
{
bullets[index] = new Bullet(coord, direction);
previous_direction = direction;
}
else
bullets[index] = new Bullet(coord, previous_direction);
}
}
private:
COORD coord;
direct_t direction;
direct_t previous_direction;
};