-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPlayer.cpp
More file actions
53 lines (45 loc) · 1.05 KB
/
Player.cpp
File metadata and controls
53 lines (45 loc) · 1.05 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
#include "Player.h"
Player::Player(SDL_Surface *screen)
{
this->screen = screen;
this->images[0] = IMG_Load( "player/1.png" );
this->images[1] = IMG_Load( "player/2.png" );
this->images[2] = IMG_Load( "player/3.png" );
this->images[3] = IMG_Load( "player/4.png" );
this->x = 200;
this->y = 0;
this->acceleration=2;
this->velocity=0;
this->current_frame=0;
}
Player::~Player()
{
SDL_FreeSurface( images[0] );
SDL_FreeSurface( images[1] );
SDL_FreeSurface( images[2] );
SDL_FreeSurface( images[3] );
}
void Player::logic()
{
y+=velocity;
velocity+=acceleration;
if(y>=500-images[current_frame]->w/2)
{
y=500-images[current_frame]->w/2;
velocity=0;
}
}
void Player::jump()
{
velocity=-30;
}
void Player::render()
{
SDL_Rect offset;
offset.x = x - images[current_frame]->w/2;
offset.y = y - images[current_frame]->h/2;
SDL_BlitSurface( images[current_frame], NULL, screen, &offset );
current_frame++;
if(current_frame>3)
current_frame=0;
}