Advertisement
Guest User

Untitled

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