-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.cpp
68 lines (60 loc) · 1.65 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
65
66
67
68
#include<bits/stdc++.h>
#include "player.hpp"
Player::Player(int xg, int yg, int h, int w){
destR.h=h; destR.w=w;
destR.x=xg; destR.y=yg;
x=xg; y=yg;
}
void Player::display(SDL_Renderer* renderer){
//Show player on screen
SDL_Surface * playersurf = IMG_Load("images/player.png");
playertex = SDL_CreateTextureFromSurface(renderer, playersurf);
SDL_FreeSurface(playersurf);
if(x<=375)
SDL_RenderCopyEx(renderer, playertex,NULL, &destR,-20,NULL,SDL_FLIP_NONE);
else
SDL_RenderCopyEx(renderer, playertex,NULL, &destR,20,NULL,SDL_FLIP_HORIZONTAL);
}
void Player::move(int dir){
//Move player right or left
if(destR.x + dir*10 >=0 && destR.x + dir*10 <=750){
destR.x+=dir*10;
x+=dir*10;
}
}
void Player::shoot(int x, int y, SDL_Renderer* renderer){
//Animation to display shooting
SDL_Surface * shootsurf = IMG_Load("images/shoot.png");
shoottex = SDL_CreateTextureFromSurface(renderer, shootsurf);
SDL_FreeSurface(shootsurf);
SDL_Rect destRsh;
destRsh.h=60; destRsh.w=60;
destRsh.x=x-30; destRsh.y=y-30;
SDL_RenderCopy(renderer, shoottex,NULL, &destRsh);
}
bool Player::shooted(int xb, int yb, SDL_Renderer* renderer){
//decrease health if shooted
int cx=x+64, cy=y+125;
if(xb>cx-60 && xb<cx+60 && yb>cy-60 && yb<cy+60){
health-=1;
if(health<=0){
return false;
}
}
return true;
}
void Player::setScore(int s){
score = s;
}
int Player::getScore(){
return score;
}
int Player::getX(){
return x;
}
int Player::getY(){
return y;
}
int Player::getHealth(){
return health;
}