Advertisement
Guest User

Untitled

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