Advertisement
Guest User

Untitled

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