-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
56 lines (50 loc) · 1.17 KB
/
player.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 "player.h"
Player::Player(int xUser, int yUser)
{
player.x = xUser;
player.y = yUser;
player.w = PADDLE_HEIGHT;
player.h = PADDLE_WIDTH;
xVelocity = 0;
}
void Player::handle_input()//Moving via the arrow keys
{
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_LEFT)
{
xVelocity -= SPEED;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
xVelocity += SPEED;
}
}
else if(event.type == SDL_KEYUP)
{
if(event.key.keysym.sym == SDLK_LEFT)
{
xVelocity += SPEED;
}
else if(event.key.keysym.sym == SDLK_RIGHT)
{
xVelocity -= SPEED;
}
}
}
void Player::move(Uint32 deltaTicks)//Moving according to frame rate
{
player.x += xVelocity *(deltaTicks/1000.f);
if( player.x < 0 )
{
player.x = 0;
}
else if( player.x + PADDLE_HEIGHT > SCREEN_WIDTH)
{
player.x = SCREEN_WIDTH - PADDLE_HEIGHT;
}
}
void Player::show()//Display the paddle/player on the screen
{
apply_surface( (int)player.x, (int)player.y, paddleSprite, screen );
}