Advertisement
Guest User

Untitled

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