Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.98 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include "Collision.h"
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6. using namespace sf;
  7. using namespace Collision;
  8.  
  9. typedef pair<int ,int > II;
  10.  
  11. const int Height = 900;
  12. const int Width = 900;
  13. const int Ground = 600;
  14. const string path_RightStand = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightStand/RightStand.txt";
  15. const string path_LeftStand = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftStand/LeftStand.txt";
  16. const string path_RightMove = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightMove/RightMove.txt";
  17. const string path_LeftMove = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftMove/LeftMove.txt";
  18. const string path_RightJump = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightJump/RightJump.txt";
  19. const string path_LeftJump = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftJump/LeftJump.txt";
  20. const string path_RightAttack1 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightAttack1/RightAttack1.txt";
  21. const string path_LeftAttack1 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftAttack1/LeftAttack1.txt";
  22. const string path_RightAttack2 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightAttack2/RightAttack2.txt";
  23. const string path_LeftAttack2 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftAttack2/LeftAttack2.txt";
  24. const string path_RightBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightBullet/RightBullet.txt";
  25. const string path_LeftBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftBullet/LeftBullet.txt";
  26. const string path_Rocket = "/home/hoaf13/workspace/SFMLtutorial/zero_images/ricardo/ricardo.txt";
  27. const string path_Ricardo = "/home/hoaf13/workspace/SFMLtutorial/zero_images/ricardo/bossRicardo.txt";
  28. const string path_CircleBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/CircleBullet/CircleBullet.txt";
  29.  
  30. const Time timeStage1 =  seconds(10);
  31.  
  32.  
  33. Clock realClock, createClock , rotateClock;
  34. Time realTime, createTime , rotateTime;
  35.  
  36.  
  37. RenderWindow window(VideoMode(Width , Height) , "Gaming" , Style::Close | Style::Resize);
  38.  
  39. class Block{
  40.     private:
  41.         int x;
  42.         int y;
  43.         Texture texture;
  44.         Sprite sprite;
  45.     public:
  46.         Block(){
  47.             texture.loadFromFile("/home/hoaf13/Desktop/block.jpg");
  48.         }
  49.         void setBlockPos(int x, int y){
  50.             this -> x = x;
  51.             this -> y = y;
  52.             sprite.setPosition(this->x,this->y);
  53.             sprite.scale(0.1,0.1);
  54.         }
  55.         II getBlockPos(){
  56.             return make_pair(sprite.getPosition().x,sprite.getPosition().y);
  57.         }
  58.         void draw(){
  59.             sprite.setTexture(texture);
  60.             window.draw(sprite);
  61.         }
  62. };
  63.  
  64. class Animation{
  65.  
  66.     private:
  67.         float speed , frame;
  68.         Sprite sprite;              // window.draw
  69.         vector<IntRect> frames;     // size of image
  70.         vector<Texture> texture;    // image
  71.     public:
  72.         Animation(){}
  73.         void setAnimation(string path, float speed){
  74.             this -> speed = speed;
  75.             frame = 0;
  76.             ifstream fin;
  77.             fin.open(path);
  78.             string path_image;
  79.             Texture tmp_texture;
  80.             while(!fin.eof()){
  81.                 getline(fin , path_image);
  82.                 if (path_image != ""){
  83.                     tmp_texture.loadFromFile(path_image);
  84.                     texture.push_back(tmp_texture);
  85.                     frames.push_back(IntRect(0,0,tmp_texture.getSize().x , tmp_texture.getSize().y));
  86.                 }
  87.             }
  88.             this -> sprite.setTexture(texture[0]);
  89.             this -> sprite.setTextureRect(frames[0]);
  90.         }
  91.         Sprite getAnimationSprite(){
  92.             return sprite;
  93.         }
  94.         void update(int x, int y){
  95.             frame += speed;
  96.             int n = frames.size();
  97.             if (frame > n) frame -= float(n);
  98.             sprite.setPosition(x,y);
  99.             sprite.setTexture(texture[frame]);
  100.             sprite.setTextureRect(frames[frame]);
  101.         }
  102.         void scaleAnimation(float x, float y){
  103.             sprite.scale(x,y);
  104.         }
  105.         void rotateAnimation(float x){
  106.             sprite.setRotation(x);
  107.         }
  108.         void originAnimation(float x, float y){
  109.             sprite.setOrigin(x , y);
  110.         }
  111.         void setPosition(int para1 , int para2){
  112.             sprite.setPosition(para1 , para2);
  113.         }
  114.         void draw(){
  115.             window.draw(sprite);
  116.         }
  117.  
  118. };
  119.  
  120. class Hero{
  121.     private:
  122.         int x;
  123.         int y;
  124.         int height;
  125.         int direction;
  126.         int life;
  127.         bool alive;
  128.         bool wanaJump;
  129.         Animation animation[6]; // 0.RightStand 1.LeftStand  2.RightMove 3.LeftMove  4.RightJump   5.LeftJump
  130.     public:
  131.         Hero(){
  132.             x = 200;
  133.             y = 400;
  134.             life = 3;
  135.             wanaJump = false;
  136.             animation[0].setAnimation(path_RightStand , 0.009f);
  137.             animation[1].setAnimation(path_LeftStand, 0.009f);
  138.             animation[2].setAnimation(path_RightMove,0.3f);
  139.             animation[3].setAnimation(path_LeftMove,0.3f);
  140.             animation[4].setAnimation(path_RightJump,0.04f);
  141.             animation[5].setAnimation(path_LeftJump,0.04f);
  142.         }
  143.         bool getHeroAlive(){
  144.             return alive;
  145.         }
  146.         void setHeroAlive(bool x){
  147.             alive = x;
  148.         }
  149.         int getHeroDirection(){
  150.             return direction;
  151.         }
  152.         void setHeroDirection(int para){
  153.             direction = para;
  154.         }
  155.         int getHeroLife(){
  156.             return life;
  157.         }
  158.         void setHeroLife(int para){
  159.             life = para;
  160.         }
  161.         void setHeroPos(int x, int y){
  162.             this -> x = x;
  163.             this -> y = y;
  164.         }
  165.         II getHeroPos(){
  166.             return make_pair(x,y);
  167.         }
  168.         void processDeath(){
  169.             if (y > Height) alive = false;
  170.             if (alive == false){
  171.                 wanaJump = false;
  172.                 direction = 0;
  173.                 setHeroPos(300,450);
  174.                 alive = true;
  175.             }
  176.         }
  177.         bool isReactBlock(){
  178.             if (x > 140 && x < 700 && y >600 && y < 650) return true;
  179.             return false;
  180.         }
  181.         bool checkWanaJump(){
  182.             if (y > height){
  183.                 wanaJump = true;
  184.             }
  185.             else{
  186.                 wanaJump = false;
  187.                 height = Ground;
  188.             }
  189.             return wanaJump;
  190.         }
  191.         void gravityFall(){
  192.             if (!(y == Ground && x > 155 && x < 700) && wanaJump == false){
  193.                 setHeroPos(getHeroPos().first , getHeroPos().second + 2);
  194.             }
  195.         }
  196.         void gravityJump(){
  197.             if (getHeroPos().second < height) wanaJump = false;
  198.             if (wanaJump){
  199.                 setHeroPos(getHeroPos().first , getHeroPos().second - 4);
  200.             }
  201.         }
  202.         Sprite getSprite(){
  203.             return animation[direction].getAnimationSprite();
  204.         }
  205. /* Activities */
  206.  
  207.         void moveRight(){
  208.             direction = 2;
  209.             if (isReactBlock() == false) setHeroPos(getHeroPos().first + 2 ,getHeroPos().second);
  210.         }
  211.         void moveLeft(){
  212.             direction = 3;
  213.             if (isReactBlock() == false) setHeroPos(getHeroPos().first - 2 ,getHeroPos().second);
  214.  
  215.         }
  216.         void jump(){
  217.             if (x > 155 && x < 700 && y == Ground) wanaJump = true;
  218.             height = 450;
  219.             if ((direction == 2 || direction == 0)) direction = 4;
  220.             if ((direction == 1 || direction == 3)) direction = 5;
  221.  
  222.         }
  223. /*--------------------*/
  224.         void draw(){
  225.             if (y == Ground && direction == 4) direction = 0;
  226.             if (y == Ground && direction == 5) direction = 1;
  227.             if (direction == 2 && y != Ground) direction = 4;
  228.             if (direction == 3 && y != Ground) direction = 5;
  229.             animation[direction].update(x,y);
  230.             animation[direction].draw();
  231.             if (direction % 2 == 0 && direction != 4) direction = 0;
  232.             if ( direction % 2 == 1 && direction != 5) direction = 1;
  233.         }
  234. };
  235. Hero hero;
  236.  
  237. class Boss{
  238.     private:
  239.         int x;
  240.         int y;
  241.         int direction;  //0.phai    1.trai
  242.         Animation animation;
  243.     public:
  244.         friend class CircleBullet;
  245.         Boss(){
  246.             x = 200;
  247.             y = -30;
  248.             direction = 0;
  249.             animation.setAnimation(path_Ricardo,0.3f);
  250.             animation.scaleAnimation(0.5,0.5);
  251.         }
  252.         void setAnimation(string path , float speed){
  253.             animation.setAnimation(path, speed);
  254.         }
  255.         II getBossPos(){
  256.             return make_pair(x,y);
  257.         }
  258.         void setBossPos(int x ,int y){
  259.             this->x = x;
  260.             this->y = y;
  261.         }
  262.         int getBossDir(){
  263.             return direction;
  264.         }
  265.         void setBossDir(int para){
  266.             direction = para;
  267.         }
  268.         void update(){
  269.             if (x > 600) direction = 1;
  270.             if (x < 0) direction = 0;
  271.             if (direction == 0) x+=4;
  272.             else x-=4;
  273.         }
  274.         void draw(){
  275.  
  276.             animation.update(x,y);
  277.             animation.draw();
  278.         }
  279.  
  280. };
  281. Boss ricardo;
  282.  
  283.  
  284. class Bullet{
  285.     private:
  286.         int x;
  287.         int y;
  288.         int direction;
  289.         Animation animation;
  290.     public:
  291.         Bullet(int x, int y , int direction){
  292.             this->x = x;
  293.             this->y = y;
  294.             this->direction = direction;
  295.             animation.setAnimation(path_Rocket , 0.04f);
  296.         }
  297.         II getBulletPos(){
  298.             return make_pair(x,y);
  299.         }
  300.         void setBulletPos(int x, int y){
  301.             this->x = x;
  302.             this->y = y;
  303.         }
  304.         int getBulletDir(){
  305.             return direction;
  306.         }
  307.         void setBulletDir(int para){
  308.             direction = para;
  309.         }
  310.         void setBulletScale(float para1 , float para2){
  311.             animation.scaleAnimation(para1 , para2);
  312.         }
  313.         Sprite getSprite(){
  314.             return animation.getAnimationSprite();
  315.         }
  316.         void setBulletRot(float para){
  317.             animation.rotateAnimation(para);
  318.         }
  319.         void setBulletOri(int para1 , int para2){
  320.             animation.originAnimation(para1,para2);
  321.         }
  322.         void draw(){
  323.             animation.update(x,y);
  324.             animation.draw();
  325.         }
  326. };
  327. vector<Bullet> bullets;
  328.  
  329. class CircleBullet{
  330.     private:
  331.         int x;
  332.         int y;
  333.         Texture texture;
  334.         Sprite sprite;
  335.     public:
  336.         CircleBullet(){
  337.             x = ricardo.getBossPos().first;
  338.             y = ricardo.getBossPos().second;
  339.             texture.loadFromFile("/home/hoaf13/workspace/SFMLtutorial/zero_images/CircleBullet/0.png");
  340.             sprite.setTexture(texture);
  341.             sprite.scale(0.5,0.5);
  342.         }
  343.         II getCircleBulletPos(){
  344.             return make_pair(x,y);
  345.         }
  346.         void setCircleBulletPos(int x, int y){
  347.             this->x = x;
  348.             this->y = y;
  349.         }
  350.         void update(double timeElapsed){
  351.             x = sprite.getPosition().x  + timeElapsed * x;
  352.             y = sprite.getPosition().y  + timeElapsed * y;
  353.             sprite.setPosition(x,y);
  354.         }
  355.         void draw(){
  356.             window.draw(sprite);
  357.         }
  358. };
  359.  
  360. /*----Global Function----*/
  361.  
  362. void createBullet(){
  363.     createTime = createClock.getElapsedTime();
  364.     if (createTime >= seconds(0.07f)){
  365.         int X = ricardo.getBossPos().first + 120 + rand()%100 - 50;
  366.         Bullet bullet(X, 80 , 2);
  367.         bullet.setBulletScale(0.8 , 0.8);
  368.         bullets.push_back(bullet);
  369.         createClock.restart();
  370.     }
  371. }
  372.  
  373. void updateBullet(){
  374.     rotateTime = rotateClock.getElapsedTime();
  375.     for(int i=0;i<bullets.size();i++){
  376.         if (bullets[i].getBulletDir() == 0) bullets[i].setBulletPos(bullets[i].getBulletPos().first + 4 , bullets[i].getBulletPos().second );
  377.         if (bullets[i].getBulletDir() == 1) bullets[i].setBulletPos(bullets[i].getBulletPos().first - 4 , bullets[i].getBulletPos().second );
  378.         if (bullets[i].getBulletDir() == 2) bullets[i].setBulletPos(bullets[i].getBulletPos().first  , bullets[i].getBulletPos().second + 4 );
  379.         if (bullets[i].getBulletDir() == 3) bullets[i].setBulletPos(bullets[i].getBulletPos().first  , bullets[i].getBulletPos().second - 4);
  380.     }
  381.     while(1){
  382.         int indez = -1;
  383.         for(int i=0;i<bullets.size();i++){
  384.             int X = bullets[i].getBulletPos().first;
  385.             int Y = bullets[i].getBulletPos().second;
  386.             if (X < 0 || X > Width || Y < 0 || Y > Height){
  387.                 indez = i;
  388.                 break;
  389.             }
  390.         }
  391.         if (indez == -1) break;
  392.         bullets.erase(bullets.begin() + indez , bullets.begin() + indez + 1);
  393.     }
  394. }
  395.  
  396. void processCrashing(){
  397.     int X = hero.getHeroPos().first;
  398.     int Y = hero.getHeroPos().second;
  399.     for(int i=0;i<bullets.size();i++){
  400.         int a = bullets[i].getBulletPos().first;
  401.         int b = bullets[i].getBulletPos().second;
  402.         for(int j=X - 20 ; j <= X + 20 ;j++ ){
  403.             for(int k= Y - 20 ; k <= Y + 20 ; k++){
  404.                 if (j == a && k == b){
  405.                     hero.setHeroAlive(false);
  406.                     return;
  407.                 }
  408.             }
  409.         }
  410.     }
  411. }
  412. /*-----------------------------*/
  413.  
  414.  
  415. int main()
  416. {
  417.     /* Init */
  418.  
  419.     window.setFramerateLimit(120);
  420.  
  421.     vector<Block> blocks(Width);
  422.     for(int i=0;i<Width;i++){
  423.         blocks[i].setBlockPos(i,Ground+50);
  424.     }
  425.     ricardo.setAnimation(path_Ricardo,0.4f);
  426.  
  427.  
  428.  
  429.     CircleBullet circleBullet;
  430.  
  431.     /*=======================*/
  432.      while(window.isOpen()){
  433.         Event event;
  434.         while(window.pollEvent(event)){
  435.             if (event.type == Event::Closed) window.close();
  436.         }
  437.         window.clear();
  438.  
  439.         /*-----Platform-----*/
  440.          for(int i=200;i<700;i+=20){
  441.             blocks[i].draw();
  442.         }
  443.         /*------Entity----------*/
  444.         processCrashing();
  445.  
  446.         cout << circleBullet.getCircleBulletPos().first << endl;
  447.         realTime = realClock.getElapsedTime();
  448.         circleBullet.update(realTime.asSeconds()/100);
  449.  
  450.         hero.processDeath();
  451.         hero.gravityFall();
  452.         hero.gravityJump();
  453.         if (Keyboard::isKeyPressed(Keyboard::A)) hero.moveLeft();
  454.         if (Keyboard::isKeyPressed(Keyboard::D)) hero.moveRight();
  455.         if (Keyboard::isKeyPressed(Keyboard::W)) hero.jump();
  456.  
  457.  
  458.         /*
  459.         realTime = realClock.getElapsedTime();
  460.         if (realTime > seconds(5)){
  461.             createBullet();
  462.             updateBullet();
  463.             ricardo.draw();
  464.         }
  465.         */
  466.  
  467.         /*=======Stage Area==========*/
  468.  
  469.  
  470.  
  471.         /*===========================*/
  472.  
  473.         ricardo.draw();
  474.         hero.draw();
  475.         circleBullet.draw();
  476.  
  477.         for(int i=0;i<bullets.size();i++){
  478.             bullets[i].draw();
  479.         }
  480.         window.display();
  481.     }
  482.  
  483.     return 0;
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement