Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include<vector>
- #include<ctime>
- using namespace std;
- struct Birdblock
- {
- sf::Sprite bird;
- sf::Texture texture;
- float x=250,y=384,timefall=0;
- float scale_ratio=0.1588f;
- sf::Vector2f scale;
- float width=272*scale_ratio,height=225*scale_ratio;
- Birdblock()
- {
- if(!texture.loadFromFile("redbird.png"))
- cout<<"Error";
- bird.setTexture(texture);
- bird.setOrigin(sf::Vector2f(136.f,112.5f));
- bird.setScale(sf::Vector2f(scale_ratio,scale_ratio));
- scale = bird.getScale();
- bird.setPosition(x,y);
- }
- void update(float window_width,float window_height)
- {
- bird.setPosition(sf::Vector2f(x,y));
- }
- void draw(sf::RenderWindow& window)
- {
- window.draw(bird);
- }
- void moveleft(float window_width, float window_height)
- {
- bird.setScale(scale.x * -1, scale.y * 1);
- if(bird.getRotation()!=45)
- bird.setRotation(bird.getRotation() + 45);
- x+=-0.4;
- y+=-0.4;
- timefall=0;
- if(x-width/2<=0)
- x=0+width/2;
- }
- void moveright(float window_width, float window_height)
- {
- bird.setScale(scale.x * 1, scale.y * 1);
- if(bird.getRotation()!=315)
- bird.setRotation(bird.getRotation() + 315);
- x+=0.4;
- y+=-0.4;
- timefall=0;
- if(x+width/2>=window_width)
- x=window_width-width/2;
- }
- void fall(float window_width, float window_height)
- {
- if(bird.getRotation()<=45&&bird.getRotation()>0)
- {
- bird.setRotation(bird.getRotation()-1);
- }
- if(bird.getRotation()>=315&&bird.getRotation()<360)
- {
- bird.setRotation(bird.getRotation()+1);
- }
- y+=timefall;
- timefall+=0.001;
- if(x+width/2==window_width)
- {
- bird.setScale(scale.x * -1, scale.y * 1);
- x+=-50;
- y+=-40;
- }
- if(x-width/2==0)
- {
- bird.setScale(scale.x * 1, scale.y * 1);
- x+=50;
- y+=-40;
- }
- }
- };
- struct Background
- {
- sf::Sprite background;
- sf::Texture texture;
- float x = 0, y = 0;
- float scale = 1.f;
- float width = 432*scale, height = 768*scale;
- Background()
- {
- if (!texture.loadFromFile("background.png"))
- {
- cout << "Error" << endl;
- }
- background.setTexture(texture);
- //background.setOrigin(sf::Vector2f(f,f));
- background.setScale(sf::Vector2f(scale, scale));
- }
- void update(float window_width, float window_height)
- {
- }
- void draw(sf::RenderWindow& window)
- {
- window.draw(background);
- }
- };
- sf::Texture brickTexture;
- struct Brickblock
- {
- sf::Sprite brick;
- float x = 200, y = 0;
- float scale = 0.8f;
- float width = 81*scale, height = 41*scale;
- Brickblock()
- {
- if (!brickTexture.loadFromFile("brick.png"))
- {
- cout << "Error" << endl;
- }
- brick.setTexture(brickTexture);
- brick.setOrigin(sf::Vector2f(40.5f, 41.f));
- brick.setScale(sf::Vector2f(scale, scale));
- }
- void setX(float _x)
- {
- x=_x;
- }
- void draw(sf::RenderWindow& window)
- {
- window.draw(brick);
- }
- void update(float window_width, float window_height)
- {
- brick.setPosition(sf::Vector2f(x, y));
- y+=0.1;
- }
- bool collide(float birdx,float birdy,float birdwidth,float birdheight)
- {
- if(y-height<=birdy+birdheight/2 && y>=birdy-birdheight/2) //canh tren gach<=canh duoi chim va canh duoi gach>=canh tren chim
- {
- if(x+width/2>=birdx-birdwidth/2 && x-width/2<=birdx+birdwidth/2) //canh phai gach>=canh trai chim va canh trai gach<=canh phai chim
- {
- return true;
- }
- }
- return false;
- }
- };
- struct Rowbricks
- {
- vector<Brickblock> row;
- int brick_number;
- float rand_number,xx;
- Rowbricks()
- {
- rand_number=rand();
- brick_number=(int)rand_number%3+1;
- if(brick_number==1)
- {
- rand_number=rand();
- xx=(int)rand()%432;
- Brickblock brick;
- brick.setX(xx);
- row.push_back(brick);
- }
- if(brick_number==2)
- {
- for(int i=0; i<2; i++)
- {
- if(i==0)
- {
- Brickblock brick;
- rand_number=rand();
- xx=(int)rand_number%190;
- brick.setX(xx);
- row.push_back(brick);
- }
- if(i==1)
- {
- Brickblock brick;
- rand_number=rand();
- xx=(int)rand_number%190+241;
- brick.setX(xx);
- row.push_back(brick);
- }
- }
- }
- if(brick_number==3)
- {
- for(int i=0; i<3; i++)
- {
- if(i==0)
- {
- Brickblock brick;
- rand_number=rand();
- xx=(int)rand_number%120;
- brick.setX(xx);
- row.push_back(brick);
- }
- if(i==1)
- {
- Brickblock brick;
- rand_number=rand();
- xx=(int)rand_number%99+165;
- brick.setX(xx);
- row.push_back(brick);
- }
- if(i==2)
- {
- Brickblock brick;
- rand_number=rand();
- xx=(int)rand_number%31+400;
- brick.setX(xx);
- row.push_back(brick);
- }
- }
- }
- }
- void draw(sf::RenderWindow& window)
- {
- for(int i=0; i<brick_number; i++)
- {
- row[i].draw(window);
- }
- }
- void update(float window_width, float window_height)
- {
- for(int i=0; i<brick_number; i++)
- {
- row[i].update(window_width,window_height);
- }
- }
- bool collide(float birdx,float birdy,float birdwidth,float birdheight)
- {
- for(int i=0; i<brick_number; i++)
- {
- if(row[i].collide(birdx,birdy,birdwidth,birdheight)==true)
- return true;
- }
- return false;
- }
- };
- struct Allbricks
- {
- vector<Rowbricks>all;
- void draw(sf::RenderWindow& window)
- {
- for(unsigned int i=0; i<all.size(); i++)
- {
- all[i].draw(window);
- }
- }
- void update(long timeloop,float window_width, float window_height)
- {
- if(timeloop%2250==0)
- {
- Rowbricks row;
- all.push_back(row);
- cout<<"time loop"<<" "<<timeloop<<endl;
- cout<<"all.size"<<" "<<all.size()<<endl;
- }
- for(unsigned int i=0; i<all.size(); i++)
- {
- all[i].update(window_width,window_height);
- }
- }
- bool collide(float birdx,float birdy,float birdwidth,float birdheight)
- {
- for(unsigned int i=0; i<all.size(); i++)
- {
- if(all[i].collide(birdx,birdy,birdwidth,birdheight)==true)
- return true;
- }
- return false;
- }
- };
- struct Status
- {
- float window_width=432,window_height=768;
- Background background;
- Birdblock bird;
- //Brickblock brick;
- //Rowbricks row;
- Allbricks all;
- long timeloop=0;//225
- bool gameover=false;
- void displayStatus(sf::RenderWindow& window)
- {
- window.clear(sf::Color::Black);
- background.draw(window);
- //brick.draw(window);
- //row.draw(window);
- all.draw(window);
- bird.draw(window);
- window.display();
- }
- void updateStatus()
- {
- if(gameover==false)
- {
- if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
- bird.moveleft(window_width,window_height);
- else if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
- bird.moveright(window_width,window_height);
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- bird.moveleft(window_width,window_height);
- else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- bird.moveright(window_width,window_height);
- else
- bird.fall(window_width,window_height);
- }
- else
- {
- bird.y+=-0.5;
- bird.fall(window_width,window_height);
- }
- if(gameover==false)
- {
- //brick.update(window_width,window_height);
- //row.update(window_width,window_height);
- all.update(timeloop,window_width,window_height);
- }
- bird.update(window_width,window_height);
- if(all.collide(bird.x,bird.y,bird.width,bird.height)==true||bird.y>=window_height)
- {
- //cout<<"cham gach"<<endl;
- gameover=true;
- }
- timeloop++;
- }
- };
- int main()
- {
- srand(time(0));
- Status status;
- sf::RenderWindow window(sf::VideoMode(status.window_width,status.window_height), "Dump Jump");
- if (!brickTexture.loadFromFile("brick.png"))
- {
- cout << "Error" << endl;
- }
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- status.updateStatus();
- status.displayStatus(window);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement