-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cc
138 lines (126 loc) · 3.8 KB
/
game.cc
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
#include "game.h"
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include "win_manager.h"
// Space between adjective two pipes
const int kPipeSpace = 30;
// Speed along the x axis
const double kSpeedX = 12.0;
// Range of length of break
const int kLengthL = 8, kLengthR = 10;
// Speed along the y axis when user press the key
const double kUpSpeedY = -15.0;
// Griviational Acceleration
const double kGrivAcc = 35.0;
// Abscissa of the bird
const double kBirdX = 7.0;
namespace {
static inline int toInt(double x) {
return static_cast<int> (x + 0.5);
}
void DrawBird(WinManager &manager, double bird_y) {
int x = toInt(kBirdX), y = toInt(bird_y);
manager.WriteCharacter(x, y - 1, 'v');
manager.WriteLine(x - 1, x + 1, y, '>');
manager.WriteCharacter(x, y + 1, '^');
}
void DrawPipe(WinManager &manager, const Pipe &pipe) {
int x = toInt(pipe.x);
manager.WriteColumn(x, 0, pipe.H - 1, '|');
manager.WriteColumn(x + 1, 0, pipe.H - 1, '|');
manager.WriteColumn(x, pipe.D + 1, -1, '|');
manager.WriteColumn(x + 1, pipe.D + 1, -1, '|');
manager.WriteLine(x, x + 1, pipe.H, '-');
manager.WriteLine(x, x + 1, pipe.D, '-');
}
};
Game::Game(int height, int width)
: height_(height), width_(width) {
high_score_ = 0;
pipes_ = new Pipe[width / kPipeSpace + 5];
}
Game::~Game() {
delete pipes_;
}
void Game::Init(double start_time) {
srand(time(NULL));
paused_ = false;
last10_time_ = start_time_ = last_time_ = start_time;
fps_ = speed_y_ = .0;
frame_count_ = score_ = pipe_count_ = 0;
bird_y_ = height_ * 0.15;
}
void Game::NextFrame(double current_time, int key, WinManager &manager) {
using std::max;
using std::min;
double time_break = current_time - last_time_;
last_time_ = current_time;
++frame_count_;
if (frame_count_ == 10) {
fps_ = 10.0 / (current_time - last10_time_);
last10_time_ = current_time;
frame_count_ = 0;
}
if (key == 'p' || key == 'P')
paused_ ^= 1;
if (paused_) {
manager.WriteString(-1, height_ / 2 - 1, "Pausing...");
manager.WriteString(-1, height_ / 2, "(Press P to Continue)");
return;
}
manager.Clear();
double last_speed_y = speed_y_;
if (key == ' ')
last_speed_y = speed_y_ = kUpSpeedY;
else
speed_y_ += kGrivAcc * time_break;
bird_y_ += 0.5 * (last_speed_y + speed_y_) * time_break;
bird_y_ = max(bird_y_, 1.0);
bird_y_ = min(bird_y_, height_ - 0.7);
int new_pipe_count = 0;
for (int i = 0; i < pipe_count_; ++i) {
pipes_[i].x -= kSpeedX * time_break;
if (pipes_[i].x > 0) {
DrawPipe(manager, pipes_[i]);
pipes_[new_pipe_count++] = pipes_[i];
} else {
++score_;
high_score_ = max(high_score_, score_);
}
}
pipe_count_ = new_pipe_count;
if (width_ - pipes_[pipe_count_ - 1].x > kPipeSpace) {
NewPipe();
DrawPipe(manager, pipes_[pipe_count_ - 1]);
}
DrawBird(manager, bird_y_);
}
int Game::get_score() const { return score_; }
int Game::get_high_score() const { return high_score_; }
double Game::get_time() const { return last_time_ - start_time_; }
double Game::get_fps() const { return fps_; }
bool Game::CheckIfFail() const {
if (bird_y_ + 1.0 >= height_) return true;
for (int i = 0; i < pipe_count_; ++i)
if (HitPipe(pipes_[i])) return true;
return false;
}
void Game::NewPipe() {
Pipe &new_pipe = pipes_[pipe_count_++];
new_pipe.x = width_ - 0.5;
int h = rand() % (kLengthR - kLengthL + 1) + kLengthL;
new_pipe.H = rand() % (height_ - h + 1 - 6) + 3 + 1;
new_pipe.D = new_pipe.H + h;
}
bool Game::HitPipe(const Pipe &p) const {
int dx = toInt(p.x) - toInt(kBirdX);
if (dx > 2 || dx < -2)
return false;
else if (dx == 2)
return bird_y_ <= p.H - 0.5 || bird_y_ >= p.D + 0.5;
else if (dx == 1 || dx == -2)
return bird_y_ <= p.H + 0.5 || bird_y_ >= p.D - 0.5;
else
return bird_y_ <= p.H + 1.5 || bird_y_ >= p.D - 1.5;
}