Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<SFML/Graphics.hpp>
- #include<SFML/Audio.hpp>
- using namespace std;
- using namespace sf;
- const int Height = 900;
- const int Width = 900;
- const int Ground = 600;
- typedef pair<int ,int > II;
- RenderWindow window(VideoMode(Width , Height) , "Gaming" , Style::Close | Style::Resize);
- class Animation{
- private:
- float speed , frame;
- Sprite sprite; // window.draw
- vector<IntRect> frames; // size of image
- vector<Texture> texture; // image
- public:
- Animation(){}
- void setAnimation(string path, float speed){
- this -> speed = speed;
- frame = 0;
- ifstream fin;
- fin.open(path);
- string path_image;
- Texture tmp_texture;
- while(!fin.eof()){
- getline(fin , path_image);
- if (path_image != ""){
- tmp_texture.loadFromFile(path_image);
- texture.push_back(tmp_texture);
- frames.push_back(IntRect(0,0,tmp_texture.getSize().x , tmp_texture.getSize().y));
- }
- }
- this -> sprite.setTexture(texture[0]);
- this -> sprite.setTextureRect(frames[0]);
- }
- void update(){
- frame += speed;
- int n = frames.size();
- if (frame > n) frame -= float(n);
- sprite.setTexture(texture[frame]);
- sprite.setTextureRect(frames[frame]);
- }
- void draw(){
- window.draw(sprite);
- }
- };
- class Block{
- private:
- int x;
- int y;
- Texture texture;
- Sprite sprite;
- public:
- Block(){
- texture.loadFromFile("/home/hoaf13/Desktop/block.jpg");
- }
- void setBlockPos(int x, int y){
- this -> x = x;
- this -> y = y;
- sprite.setPosition(this->x,this->y);
- sprite.scale(0.1,0.1);
- }
- II getBlockPos(){
- return make_pair(sprite.getPosition().x,sprite.getPosition().y);
- }
- void draw(){
- sprite.setTexture(texture);
- window.draw(sprite);
- }
- };
- class Hero{
- private:
- int x;
- int y;
- int height;
- int direction; // 0. phai 1.trai
- int life;
- bool alive;
- bool wanaJump;
- Texture texture;
- Sprite sprite;
- public:
- Hero(){
- wanaJump = false;
- alive = true;
- life = 3;
- direction = 0;
- texture.loadFromFile("/home/hoaf13/workspace/SFMLtutorial/zero_images/run/0.gif");
- }
- int getHeroDirection(){
- return direction;
- }
- void setHeroDirection(int para){
- direction = para;
- }
- int getHeroLife(){
- return life;
- }
- void setHeroLife(int para){
- life = para;
- }
- void setHeroPos(int x, int y){
- this -> x = x;
- this -> y = y;
- if (this->x < 0) this->x = 0;
- if (this->x > Width-40) this->x = Width-40;
- sprite.setPosition(this->x,this->y);
- }
- II getHeroPos(){
- return make_pair(sprite.getPosition().x,sprite.getPosition().y);
- }
- void processDeath(){
- if (y > Height){
- setHeroPos(200,400);
- }
- }
- bool isReactBlock(){
- if (x > 140 && x < 700 && y >600 && y < 650) return true;
- return false;
- }
- bool checkWanaJump(){
- if (y > height){
- wanaJump = true;
- }
- else{
- wanaJump = false;
- height = Ground;
- }
- return wanaJump;
- }
- void gravityFall(){
- if (!(y == Ground && x > 155 && x < 700) && wanaJump == false){
- setHeroPos(getHeroPos().first , getHeroPos().second + 2);
- }
- }
- void gravityJump(){
- if (getHeroPos().second < 500) wanaJump = false;
- if (wanaJump){
- setHeroPos(getHeroPos().first , getHeroPos().second - 3);
- }
- }
- void moveRight(){
- direction = 0;
- if (isReactBlock() == false) setHeroPos(getHeroPos().first + 2 ,getHeroPos().second);
- direction = 1;
- }
- void moveLeft(){
- direction = 1;
- if (isReactBlock() == false) setHeroPos(getHeroPos().first - 2 ,getHeroPos().second);
- direction = 2;
- }
- void jump(){
- if (x > 155 && x < 700 && y == Ground) wanaJump = true;
- height = 550;
- }
- void draw(){
- sprite.setTexture(texture);
- window.draw(sprite);
- }
- };
- int main(){
- window.setFramerateLimit(120);
- Hero hero;
- hero.setHeroPos(200,400);
- vector<Block> blocks(Width);
- for(int i=0;i<Width;i++){
- blocks[i].setBlockPos(i,Ground+50);
- }
- while(window.isOpen()){
- Event event;
- while(window.pollEvent(event)){
- if (event.type == Event::Closed) window.close();
- }
- window.clear();
- hero.processDeath();
- hero.gravityFall();
- hero.gravityJump();
- if (Keyboard::isKeyPressed(Keyboard::Left)) hero.moveLeft();
- if (Keyboard::isKeyPressed(Keyboard::Right)) hero.moveRight();
- if (Keyboard::isKeyPressed(Keyboard::Up)) hero.jump();
- hero.draw();
- for(int i=200;i<700;i+=10){
- blocks[i].draw();
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement