-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
64 lines (59 loc) · 1.3 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
57
58
59
60
61
62
63
64
#include "Player.h"
Player::Player(double x,double y, double velocity,SDL_Renderer* renderer)
{
this->x=x;
this->y=y;
this->renderer=renderer;
this->velocity = velocity;
int w,h;
texture = IMG_LoadTexture(renderer, "personaje.png");
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
rect.x = 0;
rect.y = 100;
rect.w = w;
rect.h = h;
}
void Player::logic()
{
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
if( currentKeyStates[ SDL_SCANCODE_UP ] )
{
rect.y-=velocity;
}
if( currentKeyStates[ SDL_SCANCODE_DOWN ] )
{
rect.y+=velocity;
}
if( currentKeyStates[ SDL_SCANCODE_RIGHT ] )
{
rect.x+=velocity;
}
if( currentKeyStates[ SDL_SCANCODE_LEFT ] )
{
rect.x-=velocity;
}
}
void Player::draw()
{
SDL_RenderCopy(renderer, texture, NULL, &rect);
}
void Player::hitObstacle()
{
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
if( currentKeyStates[ SDL_SCANCODE_UP ] )
{
rect.y+=velocity;
}
if( currentKeyStates[ SDL_SCANCODE_DOWN ] )
{
rect.y-=velocity;
}
if( currentKeyStates[ SDL_SCANCODE_RIGHT ] )
{
rect.x-=velocity;
}
if( currentKeyStates[ SDL_SCANCODE_LEFT ] )
{
rect.x+=velocity;
}
}