-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRacket.cpp
56 lines (48 loc) · 1.15 KB
/
Racket.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
#include "Racket.hpp"
Racket::Racket(GameWindow* gw, const side& racket_side, const SDL_Scancode& up, const SDL_Scancode& down)
: game_window(gw),
key_up(up), key_down(down),
x(rect.x),
y(rect.y),
w(rect.w),
h(rect.h),
step(gw->height/240),
racket_side(racket_side)
{
reset();
}
void Racket::up()
{
y -= step;
if (y < 0)
y = 0;
}
void Racket::down()
{
y += step;
if (y > game_window->height - h)
y = game_window->height - h;
}
void Racket::render()
{
SDL_SetRenderDrawColor(game_window->renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderFillRect(game_window->renderer, &rect);
}
void Racket::control(const Uint8* key_state)
{
if (!key_state[key_up] != !key_state[key_down])
{
if (key_state[key_up])
up();
else if (key_state[key_down])
down();
}
}
void Racket::reset()
{
int racket_w = game_window->width / 25;
int racket_h = game_window->height / 4;
int racket_x = (racket_side == LEFT) ? game_window->margin - 1 : game_window->width - game_window->margin + 1 - racket_w;
int racket_y = game_window->height / 2 - racket_h / 2;
rect = {std::move(racket_x), std::move(racket_y), std::move(racket_w), std::move(racket_h)};
}