Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 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.  
  11. typedef pair<int ,int > II;
  12.  
  13.  
  14. RenderWindow window(VideoMode(Width , Height) , "Gaming" , Style::Close | Style::Resize);
  15.  
  16.  
  17. class Animation{
  18.     private:
  19.         float speed , frame;
  20.         Sprite sprite;              // window.draw
  21.         vector<IntRect> frames;     // size of image
  22.         vector<Texture> texture;    // image
  23.     public:
  24.         Animation(){}
  25.         void setAnimation(string path, float speed){
  26.             this -> speed = speed;
  27.             frame = 0;
  28.             ifstream fin;
  29.             fin.open(path);
  30.             string path_image;
  31.             Texture tmp_texture;
  32.             while(!fin.eof()){
  33.                 getline(fin , path_image);
  34.                 if (path_image != ""){
  35.                     tmp_texture.loadFromFile(path_image);
  36.                     texture.push_back(tmp_texture);
  37.                     frames.push_back(IntRect(0,0,tmp_texture.getSize().x , tmp_texture.getSize().y));
  38.                 }
  39.             }
  40.             this -> sprite.setTexture(texture[0]);
  41.             this -> sprite.setTextureRect(frames[0]);
  42.         }
  43.         void update(){
  44.             frame += speed;
  45.             int n = frames.size();
  46.             if (frame > n) frame -= float(n);
  47.             sprite.setTexture(texture[frame]);
  48.             sprite.setTextureRect(frames[frame]);
  49.         }
  50.         void draw(){
  51.             window.draw(sprite);
  52.         }
  53. };
  54.  
  55. class Block{
  56.     private:
  57.         int x;
  58.         int y;
  59.         Texture texture;
  60.         Sprite sprite;
  61.     public:
  62.         Block(){
  63.             texture.loadFromFile("/home/hoaf13/Desktop/block.jpg");
  64.         }
  65.         void setBlockPos(int x, int y){
  66.             this -> x = x;
  67.             this -> y = y;
  68.             sprite.setPosition(this->x,this->y);
  69.             sprite.scale(0.1,0.1);
  70.         }
  71.         II getBlockPos(){
  72.             return make_pair(sprite.getPosition().x,sprite.getPosition().y);
  73.         }
  74.         void draw(){
  75.             sprite.setTexture(texture);
  76.             window.draw(sprite);
  77.         }
  78. };
  79.  
  80.  
  81. class Hero{
  82.     private:
  83.         int x;
  84.         int y;
  85.         int height;
  86.         int direction;  // 0. phai 1.trai
  87.         int life;
  88.         bool alive;
  89.         bool wanaJump;
  90.  
  91.         Texture texture;
  92.         Sprite sprite;
  93.     public:
  94.         Hero(){
  95.             wanaJump = false;
  96.             alive = true;
  97.             life = 3;
  98.             direction = 0;
  99.             texture.loadFromFile("/home/hoaf13/workspace/SFMLtutorial/zero_images/run/0.gif");
  100.         }
  101.  
  102.         int getHeroDirection(){
  103.             return direction;
  104.         }
  105.         void setHeroDirection(int para){
  106.             direction = para;
  107.         }
  108.         int getHeroLife(){
  109.             return life;
  110.         }
  111.         void setHeroLife(int para){
  112.             life = para;
  113.         }
  114.         void setHeroPos(int x, int y){
  115.             this -> x = x;
  116.             this -> y = y;
  117.             if (this->x < 0) this->x = 0;
  118.             if (this->x > Width-40) this->x = Width-40;
  119.             sprite.setPosition(this->x,this->y);
  120.         }
  121.         II getHeroPos(){
  122.             return make_pair(sprite.getPosition().x,sprite.getPosition().y);
  123.         }
  124.         void processDeath(){
  125.             if (y > Height){
  126.                 setHeroPos(200,400);
  127.             }
  128.         }
  129.         bool isReactBlock(){
  130.             if (x > 140 && x < 700 && y >600 && y < 650) return true;
  131.             return false;
  132.         }
  133.         bool checkWanaJump(){
  134.             if (y > height){
  135.                 wanaJump = true;
  136.             }
  137.             else{
  138.                 wanaJump = false;
  139.                 height = Ground;
  140.             }
  141.             return wanaJump;
  142.         }
  143.         void gravityFall(){
  144.             if (!(y == Ground && x > 155 && x < 700) && wanaJump == false){
  145.                 setHeroPos(getHeroPos().first , getHeroPos().second + 2);
  146.             }
  147.         }
  148.         void gravityJump(){
  149.             if (getHeroPos().second < 500) wanaJump = false;
  150.             if (wanaJump){
  151.                 setHeroPos(getHeroPos().first , getHeroPos().second - 3);
  152.             }
  153.         }
  154.         void moveRight(){
  155.             direction = 0;
  156.             if (isReactBlock() == false) setHeroPos(getHeroPos().first + 2 ,getHeroPos().second);
  157.             direction = 1;
  158.  
  159.         }
  160.         void moveLeft(){
  161.             direction = 1;
  162.             if (isReactBlock() == false) setHeroPos(getHeroPos().first - 2 ,getHeroPos().second);
  163.             direction = 2;
  164.  
  165.         }
  166.         void jump(){
  167.             if (x > 155 && x < 700 && y == Ground) wanaJump = true;
  168.             height = 550;
  169.         }
  170.         void draw(){
  171.             sprite.setTexture(texture);
  172.             window.draw(sprite);
  173.         }
  174. };
  175.  
  176.  
  177.  
  178. int main(){
  179.  
  180.     window.setFramerateLimit(120);
  181.  
  182.  
  183.     Hero hero;
  184.     hero.setHeroPos(200,400);
  185.  
  186.     vector<Block> blocks(Width);
  187.     for(int i=0;i<Width;i++){
  188.         blocks[i].setBlockPos(i,Ground+50);
  189.     }
  190.     while(window.isOpen()){
  191.         Event event;
  192.         while(window.pollEvent(event)){
  193.             if (event.type == Event::Closed) window.close();
  194.         }
  195.         window.clear();
  196.         hero.processDeath();
  197.         hero.gravityFall();
  198.         hero.gravityJump();
  199.         if (Keyboard::isKeyPressed(Keyboard::Left)) hero.moveLeft();
  200.         if (Keyboard::isKeyPressed(Keyboard::Right)) hero.moveRight();
  201.         if (Keyboard::isKeyPressed(Keyboard::Up)) hero.jump();
  202.         hero.draw();
  203.         for(int i=200;i<700;i+=10){
  204.             blocks[i].draw();
  205.         }
  206.         window.display();
  207.     }
  208.  
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement