-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbird.cpp
More file actions
103 lines (86 loc) · 2.41 KB
/
Copy pathbird.cpp
File metadata and controls
103 lines (86 loc) · 2.41 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
93
94
95
96
97
98
99
100
101
102
103
#include "bird.h"
void Bird::init()
{
point = Mix_LoadWAV("sound/point.wav");
currentRenderingTexture = mid;
animationFrames = 0;
score = 0;
velocity = 0.0f;
acceleration = 0.0f;
pos.x = WIDTH / 3 - 32;
pos.y = HEIGHT / 2;
pos.w = BIRD_WIDTH;
pos.h = BIRD_HEIGHT;
}
Bird::Bird(SDL_Texture *up, SDL_Texture *mid, SDL_Texture *down, SDL_Renderer *renderer) : up(up), mid(mid), down(down), renderer(renderer)
{
init();
}
void Bird::update(bool jump, float elapsedTime)
{
elapsedTime *= 5;
if(jump)
{
acceleration = 10.0f;
velocity = -0.7 * GRAVITY;
}
else
{
acceleration += 0.5 * GRAVITY * elapsedTime;
}
if(acceleration >= GRAVITY)
acceleration = GRAVITY;
velocity += acceleration * elapsedTime;
pos.y += velocity * elapsedTime;
}
bool Bird::collisionDetector(Pipe* pipe) {
bool collided = false;
if (pos.x + pos.w > pipe->top_dst.x && pos.x < pipe->top_dst.x + PIPE_WIDTH) {
if (pos.y < pipe->top_dst.y + pipe->top_dst.h || pos.y + pos.h > pipe->bottom_dst.y) {
if (coll) {
coll--;
if(coll==14){
Mix_PlayChannel(-1, point, 0);
}
}
else {
collided = true;
}
}
}
if (!pipe->passed && pos.x >= pipe->top_dst.x) {
pipe->passed = true;
// score ++;
}
if (pos.y + pos.h > HEIGHT - GROUND_HEIGHT) {
pos.y = 0;
} else if (pos.y < 0) {
pos.y = HEIGHT - GROUND_HEIGHT - pos.h;
}
return collided;
}
void Bird::render()
{
animation();
if(velocity == 0)
SDL_RenderCopy(renderer, mid, NULL, &pos);
else if(velocity < 50)
SDL_RenderCopyEx(renderer, currentRenderingTexture, NULL, &pos, -30.0, NULL, SDL_FLIP_NONE);
else if(velocity >= 50 && velocity < 200)
SDL_RenderCopyEx(renderer, currentRenderingTexture, NULL, &pos, 30.0, NULL, SDL_FLIP_NONE);
else if(velocity >= 200)
SDL_RenderCopyEx(renderer, mid, NULL, &pos, 90.0, NULL, SDL_FLIP_NONE);
}
void Bird::animation()
{
animationFrames++;
if(animationFrames == 5)
currentRenderingTexture = down;
else if(animationFrames == 15)
currentRenderingTexture = mid;
else if(animationFrames == 20)
{
animationFrames = 0;
currentRenderingTexture = up;
}
}