Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<SFML/Graphics.hpp>
- #include<SFML/Audio.hpp>"
- #include"main.h"
- using namespace std;
- using namespace sf;
- const int Height = 900;
- const int Width = 900;
- const int Ground = 600;
- const string path_RightStand = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightStand/RightStand.txt";
- const string path_LeftStand = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftStand/LeftStand.txt";
- const string path_RightMove = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightMove/RightMove.txt";
- const string path_LeftMove = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftMove/LeftMove.txt";
- const string path_RightJump = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightJump/RightJump.txt";
- const string path_LeftJump = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftJump/LeftJump.txt";
- const string path_RightAttack1 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightAttack1/RightAttack1.txt";
- const string path_LeftAttack1 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftAttack1/LeftAttack1.txt";
- const string path_RightAttack2 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightAttack2/RightAttack2.txt";
- const string path_LeftAttack2 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftAttack2/LeftAttack2.txt";
- const string path_RightBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightBullet/RightBullet.txt";
- const string path_LeftBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftBullet/LeftBullet.txt";
- const string path_Rocket = "/home/hoaf13/workspace/SFMLtutorial/zero_images/ricardo/ricardo.txt";
- typedef pair<int ,int > II;
- RenderWindow window(VideoMode(Width , Height) , "Gaming" , Style::Close | Style::Resize);
- Clock clockz;
- class Animation{
- private:
- float speed , frame;
- Sprite sprite; // window.draw
- vector<IntRect> frames; // size of image
- vector<Texture> texture; // image
- public:
- Animation(){}
- void setAnimation(string path, float speed){
- this -> speed = speed;
- frame = 0;
- ifstream fin;
- fin.open(path);
- string path_image;
- Texture tmp_texture;
- while(!fin.eof()){
- getline(fin , path_image);
- if (path_image != ""){
- tmp_texture.loadFromFile(path_image);
- texture.push_back(tmp_texture);
- frames.push_back(IntRect(0,0,tmp_texture.getSize().x , tmp_texture.getSize().y));
- }
- }
- this -> sprite.setTexture(texture[0]);
- this -> sprite.setTextureRect(frames[0]);
- }
- void update(int x, int y){
- frame += speed;
- int n = frames.size();
- if (frame > n) frame -= float(n);
- sprite.setPosition(x,y);
- sprite.setTexture(texture[frame]);
- sprite.setTextureRect(frames[frame]);
- }
- void scaleAnimation(float x, float y){
- sprite.scale(x,y);
- }
- void rotateAnimation(float x){
- sprite.setRotation(x);
- }
- void originAnimation(float x, float y){
- sprite.setOrigin(x , y);
- }
- void draw(){
- window.draw(sprite);
- }
- };
- class Block{
- private:
- int x;
- int y;
- Texture texture;
- Sprite sprite;
- public:
- Block(){
- texture.loadFromFile("/home/hoaf13/Desktop/block.jpg");
- }
- void setBlockPos(int x, int y){
- this -> x = x;
- this -> y = y;
- sprite.setPosition(this->x,this->y);
- sprite.scale(0.1,0.1);
- }
- II getBlockPos(){
- return make_pair(sprite.getPosition().x,sprite.getPosition().y);
- }
- void draw(){
- sprite.setTexture(texture);
- window.draw(sprite);
- }
- };
- class Bullet{
- private:
- int x;
- int y;
- int owner; //0.Hero 1.Enemies
- bool alive; //0.Dead 1.Alive
- int direction; //Sang -> 0.Phai 1.Trai 2.Duoi 3.Len
- Animation animation[4];
- public:
- Bullet(int x, int y,int owner, int direction){
- this->owner = owner;
- this->x = x;
- this->y = y;
- this->direction = direction;
- alive = true;
- if (owner == 0){
- animation[0].setAnimation(path_RightBullet, 0.1f);
- animation[1].setAnimation(path_LeftBullet, 0.1f);
- }
- if (owner == 1){
- animation[0].setAnimation(path_Rocket, 0.1f);
- animation[1].setAnimation(path_Rocket, 0.1f);
- animation[2].setAnimation(path_Rocket, 0.1f);
- animation[3].setAnimation(path_Rocket, 0.1f);
- }
- }
- II getBulletPos(){
- return make_pair(x,y);
- }
- void setBulletPos(int x , int y){
- this->x = x;
- this->y = y;
- }
- int getBulletOwn(){
- return owner;
- }
- void setBulletOwn(int para){
- owner = para;
- }
- int getBulletDir(){
- return direction;
- }
- void setBulletDir(int para){
- direction = para;
- }
- void setScalation(float para1 , float para2){
- animation[0].scaleAnimation(para1,para2);
- animation[1].scaleAnimation(para1,para2);
- animation[2].scaleAnimation(para1,para2);
- animation[3].scaleAnimation(para1,para2);
- }
- void SetRotation(float para1){
- animation[0].rotateAnimation(para1);
- animation[1].rotateAnimation(para1);
- animation[2].rotateAnimation(para1);
- animation[3].rotateAnimation(para1);
- }
- void setOrigin(float para1 , float para2){
- animation[0].originAnimation(para1 , para2);
- animation[1].originAnimation(para1 , para2);
- animation[2].originAnimation(para1 , para2);
- animation[3].originAnimation(para1 , para2);
- }
- void draw(){
- animation[direction].update(x,y);
- animation[direction].draw();
- }
- };
- vector<Bullet> bullets;
- class Hero{
- private:
- int x;
- int y;
- int height;
- int direction;
- int life;
- bool alive;
- bool wanaJump;
- Time cooldownSkill1;
- Time cooldownSkill2;
- Time timeElapse1,timeElapse2;
- Animation animation[10]; // 0.RightStand 1.LeftStand 2.RightMove 3.LeftMove 4.RightJump 5.LeftJump
- public:
- Hero(){
- wanaJump = false;
- alive = true;
- life = 3;
- direction = 4;
- cooldownSkill1 = seconds(0.3f);
- cooldownSkill2 = seconds(0.1f);
- timeElapse2 = seconds(0.1f);
- animation[0].setAnimation(path_RightStand , 0.009f);
- animation[1].setAnimation(path_LeftStand, 0.009f);
- animation[2].setAnimation(path_RightMove,0.3f);
- animation[3].setAnimation(path_LeftMove,0.3f);
- animation[4].setAnimation(path_RightJump,0.04f);
- animation[5].setAnimation(path_LeftJump,0.04f);
- animation[6].setAnimation(path_RightAttack1 , 0.15f);
- animation[7].setAnimation(path_LeftAttack1 , 0.07f);
- animation[8].setAnimation(path_RightAttack2, 0.4f);
- animation[9].setAnimation(path_LeftAttack2 , 0.4f);
- }
- Time getTimeElapse2(){
- return timeElapse2;
- }
- void setTimeElapse2(Time para){
- timeElapse2 = para;
- }
- int getHeroDirection(){
- return direction;
- }
- void setHeroDirection(int para){
- direction = para;
- }
- int getHeroLife(){
- return life;
- }
- void setHeroLife(int para){
- life = para;
- }
- void setHeroPos(int x, int y){
- this -> x = x;
- this -> y = y;
- if (this->x < 0) this->x = 0;
- if (this->x > Width-40) this->x = Width-40;
- }
- II getHeroPos(){
- return make_pair(x,y);
- }
- void processDeath(){
- if (y > Height){
- setHeroPos(200,400);
- }
- }
- bool isReactBlock(){
- if (x > 140 && x < 700 && y >600 && y < 650) return true;
- return false;
- }
- bool checkWanaJump(){
- if (y > height){
- wanaJump = true;
- }
- else{
- wanaJump = false;
- height = Ground;
- }
- return wanaJump;
- }
- void gravityFall(){
- if (!(y == Ground && x > 155 && x < 700) && wanaJump == false){
- setHeroPos(getHeroPos().first , getHeroPos().second + 2);
- }
- }
- void gravityJump(){
- if (getHeroPos().second < height) wanaJump = false;
- if (wanaJump){
- setHeroPos(getHeroPos().first , getHeroPos().second - 4);
- }
- }
- /* Activities */
- void moveRight(){
- direction = 2;
- if (isReactBlock() == false) setHeroPos(getHeroPos().first + 2 ,getHeroPos().second);
- }
- void moveLeft(){
- direction = 3;
- if (isReactBlock() == false) setHeroPos(getHeroPos().first - 2 ,getHeroPos().second);
- }
- void jump(){
- if (x > 155 && x < 700 && y == Ground) wanaJump = true;
- height = 450;
- if ((direction == 2 || direction == 0)) direction = 4;
- if ((direction == 1 || direction == 3)) direction = 5;
- }
- void Attack1(){
- if (direction % 2 == 0)direction = 6;
- else{
- direction = 7;
- animation[direction].update(x-100,y);
- }
- }
- void Attack2(){
- if (direction % 2 == 0){
- direction = 8;
- Bullet bullet(x+50,y,0,0);
- timeElapse2 = clockz.getElapsedTime();
- if (timeElapse2 >= cooldownSkill2){
- bullets.push_back(bullet);
- clockz.restart();
- }
- }
- else{
- direction = 9;
- Bullet bullet(x-25,y,0,1);
- timeElapse2 = clockz.getElapsedTime();
- if (timeElapse2 >= cooldownSkill2){
- bullets.push_back(bullet);
- clockz.restart();
- }
- }
- }
- bool checkSkill1(){
- if (timeElapse1 > cooldownSkill1 ){
- return true;
- }
- return false;
- }
- bool checkSkill2(){
- if (timeElapse2 > cooldownSkill2){
- return true;
- }
- return false;
- }
- /*--------------------*/
- void draw(){
- if (y == Ground && direction == 4) direction = 0;
- if (y == Ground && direction == 5) direction = 1;
- if (direction == 2 && y != Ground) direction = 4;
- if (direction == 3 && y != Ground) direction = 5;
- if (direction == 6){
- animation[direction].update(x,y-73);
- }
- else if (direction == 7){
- animation[direction].update(x-50,y-73);
- }
- else{
- animation[direction].update(x,y);
- }
- animation[direction].draw();
- if (direction % 2 == 0 && direction != 4) direction = 0;
- if ( direction % 2 == 1 && direction != 5) direction = 1;
- }
- };
- /*================================Globla Function=======================================*/
- Clock rotation_clock;
- Time rotation_time;
- void updateBullet(){
- /* UPDATE */
- rotation_time = rotation_clock.getElapsedTime();
- for(int i=0;i<bullets.size();i++){
- if (bullets[i].getBulletOwn() == 0){
- if (bullets[i].getBulletDir() == 0)
- bullets[i].setBulletPos(bullets[i].getBulletPos().first + 4 , bullets[i].getBulletPos().second);
- if (bullets[i].getBulletDir() == 1)
- bullets[i].setBulletPos(bullets[i].getBulletPos().first - 4 , bullets[i].getBulletPos().second);
- }
- else{
- bullets[i].setOrigin(bullets[i].getBulletPos().first , bullets[i].getBulletPos().second);
- if (bullets[i].getBulletDir() == 0)
- bullets[i].setBulletPos(bullets[i].getBulletPos().first + 4 , bullets[i].getBulletPos().second);
- if (bullets[i].getBulletDir() == 1)
- bullets[i].setBulletPos(bullets[i].getBulletPos().first - 4 , bullets[i].getBulletPos().second);
- if (bullets[i].getBulletDir() == 2)
- bullets[i].setBulletPos(bullets[i].getBulletPos().first , bullets[i].getBulletPos().second - 4);
- if (bullets[i].getBulletDir() == 3)
- bullets[i].setBulletPos(bullets[i].getBulletPos().first , bullets[i].getBulletPos().second + 4);
- bullets[i].SetRotation(rotation_time.asMilliseconds()/10);
- }
- }
- /* REMOVE */
- while(1){
- int indez = -1;
- for(int i=0;i<bullets.size();i++){
- int x = bullets[i].getBulletPos().first;
- int y = bullets[i].getBulletPos().second;
- if (x < 0 || x > Width || y < 0 || y > Height){
- indez = i;
- break;
- }
- }
- if (indez == -1) break;
- bullets.erase(bullets.begin() + indez,bullets.begin()+indez+1);
- }
- }
- /*-----------------------------------------------------------------------------------------*/
- Time elapsed;
- Clock clocks;
- void randBullet(){
- elapsed = clocks.getElapsedTime();
- if (elapsed >= seconds(0.5)){
- int X = rand()%Width;
- Bullet bullet(X,0,1,3);
- bullet.setScalation(0.1,0.1);
- bullets.push_back(bullet);
- clocks.restart();
- }
- }
- int main(){
- window.setFramerateLimit(120);
- Hero hero;
- hero.setHeroPos(200,400);
- vector<Block> blocks(Width);
- for(int i=0;i<Width;i++){
- blocks[i].setBlockPos(i,Ground+50);
- }
- /*-------------------------*/
- while(window.isOpen()){
- Event event;
- while(window.pollEvent(event)){
- if (event.type == Event::Closed) window.close();
- }
- window.clear();
- cout << bullets.size() << endl;
- randBullet();
- hero.processDeath();
- hero.gravityFall();
- hero.gravityJump();
- updateBullet();
- if (Keyboard::isKeyPressed(Keyboard::Left)) hero.moveLeft();
- if (Keyboard::isKeyPressed(Keyboard::Right)) hero.moveRight();
- if (Keyboard::isKeyPressed(Keyboard::Up)) hero.jump();
- if (Keyboard::isKeyPressed(Keyboard::A)) hero.Attack1();
- if (Keyboard::isKeyPressed(Keyboard::D)) hero.Attack2();
- hero.draw();
- for(int i=200;i<700;i+=10){
- blocks[i].draw();
- }
- for(int i=0;i<bullets.size();i++){
- bullets[i].draw();
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement