-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappy.cpp
More file actions
92 lines (80 loc) · 1.94 KB
/
flappy.cpp
File metadata and controls
92 lines (80 loc) · 1.94 KB
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
#include "flappy.h"
Obstacle::Obstacle(int rate, BackGround *p): BackGround(p->height-2, p->width-2, true, p) {
this->setPosition(p->pos.X+p->width-1-this->width, p->pos.Y+1);
for(int i=0; i<MAX_SCREEN; i++) {
for(int j=0; j<MAX_SCREEN; j++) {
this->data[i][j] = 0;
}
}
this->rate=rate;
this->count=0;
for(i=0; i<this->width*3/4; i++) {
this->move();
}
this->name="obstacle";
srand((unsigned)time(NULL));
}
void Obstacle::move() {
for(int i=0; i<this->height; i++) {
for(int j=0; j<this->width-1; j++) {
this->data[i][j] = this->data[i][j+1];
}
}
int l1=rand()%(this->height*1/2);
int l2=this->height*1/4;
int length=(this->count % this->rate ? 0 : (l1+l2));
int direct=rand() % 2;
for(i=0; i<this->height; i++) {
int value=i<length?1:0;
int line=direct?i:this->height-1-i;
this->data[line][this->width-1]=value;
}
this->count++;
}
int Obstacle::TimeEvent(int timestamp) {
if(timestamp-this->lastTimestamp < 500) {
return 0;
}
this->move();
this->lastTimestamp=timestamp;
return 1;
}
Bird::Bird(BackGround *p): Ctrl(p) {
this->setPosition(p->pos.X+6, p->pos.Y+p->height/2);
this->name="bird";
}
void Bird::toShow(int focus) {
this->setCursor(this->pos.X, this->pos.Y);
printf("+");
this->setCursor(this->pos.X+1, this->pos.Y);
printf(">");
}
int Bird::TimeEvent(int timestamp) {
if(timestamp-this->lastTimestamp < 200) {
return 0;
}
BackGround *bg = (BackGround*)this->parent;
if(this->pos.Y+1 >= bg->pos.Y+bg->height-1) {
return -1;
}
Obstacle *obs=(Obstacle*)this->parent->getChild("obstacle");
if(obs==NULL) {
return -1;
}
int obsX=this->pos.X-obs->pos.X;
int obsY=this->pos.Y-obs->pos.Y;
if(obs->data[obsY][obsX] || obs->data[obsY][obsX+1]) {
return -1;
}
this->lastTimestamp=timestamp;
this->pos.Y++;
return 1;
}
int Bird::Up() {
if(this->pos.Y-1 <= this->parent->pos.Y) {
return 0;
}
this->lastTimestamp=clock();
this->pos.Y--;
return 1;
}