Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include "Collision.h"
- #include <bits/stdc++.h>
- using namespace std;
- using namespace sf;
- using namespace Collision;
- typedef pair<int ,int > II;
- const int Height = 900;
- const int Width = 900;
- const int Ground = 600;
- const string path_RightStand = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightStand/RightStand.txt";
- const string path_LeftStand = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftStand/LeftStand.txt";
- const string path_RightMove = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightMove/RightMove.txt";
- const string path_LeftMove = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftMove/LeftMove.txt";
- const string path_RightJump = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightJump/RightJump.txt";
- const string path_LeftJump = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftJump/LeftJump.txt";
- const string path_RightAttack1 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightAttack1/RightAttack1.txt";
- const string path_LeftAttack1 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftAttack1/LeftAttack1.txt";
- const string path_RightAttack2 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightAttack2/RightAttack2.txt";
- const string path_LeftAttack2 = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftAttack2/LeftAttack2.txt";
- const string path_RightBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/RightBullet/RightBullet.txt";
- const string path_LeftBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/LeftBullet/LeftBullet.txt";
- const string path_Rocket = "/home/hoaf13/workspace/SFMLtutorial/zero_images/ricardo/ricardo.txt";
- const string path_Ricardo = "/home/hoaf13/workspace/SFMLtutorial/zero_images/ricardo/bossRicardo.txt";
- const string path_CircleBullet = "/home/hoaf13/workspace/SFMLtutorial/zero_images/CircleBullet/CircleBullet.txt";
- const Time timeStage1 = seconds(10);
- Clock realClock, createClock , rotateClock;
- Time realTime, createTime , rotateTime;
- RenderWindow window(VideoMode(Width , Height) , "Gaming" , Style::Close | Style::Resize);
- 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 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]);
- }
- Sprite getAnimationSprite(){
- return sprite;
- }
- void update(int x, int y){
- frame += speed;
- int n = frames.size();
- if (frame > n) frame -= float(n);
- sprite.setPosition(x,y);
- sprite.setTexture(texture[frame]);
- sprite.setTextureRect(frames[frame]);
- }
- void scaleAnimation(float x, float y){
- sprite.scale(x,y);
- }
- void rotateAnimation(float x){
- sprite.setRotation(x);
- }
- void originAnimation(float x, float y){
- sprite.setOrigin(x , y);
- }
- void setPosition(int para1 , int para2){
- sprite.setPosition(para1 , para2);
- }
- void draw(){
- window.draw(sprite);
- }
- };
- class Hero{
- private:
- int x;
- int y;
- int height;
- int direction;
- int life;
- bool alive;
- bool wanaJump;
- Animation animation[6]; // 0.RightStand 1.LeftStand 2.RightMove 3.LeftMove 4.RightJump 5.LeftJump
- public:
- Hero(){
- x = 200;
- y = 400;
- life = 3;
- wanaJump = false;
- animation[0].setAnimation(path_RightStand , 0.009f);
- animation[1].setAnimation(path_LeftStand, 0.009f);
- animation[2].setAnimation(path_RightMove,0.3f);
- animation[3].setAnimation(path_LeftMove,0.3f);
- animation[4].setAnimation(path_RightJump,0.04f);
- animation[5].setAnimation(path_LeftJump,0.04f);
- }
- bool getHeroAlive(){
- return alive;
- }
- void setHeroAlive(bool x){
- alive = x;
- }
- 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;
- }
- II getHeroPos(){
- return make_pair(x,y);
- }
- void processDeath(){
- if (y > Height) alive = false;
- if (alive == false){
- wanaJump = false;
- direction = 0;
- setHeroPos(300,450);
- alive = true;
- }
- }
- 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 < height) wanaJump = false;
- if (wanaJump){
- setHeroPos(getHeroPos().first , getHeroPos().second - 4);
- }
- }
- Sprite getSprite(){
- return animation[direction].getAnimationSprite();
- }
- /* Activities */
- void moveRight(){
- direction = 2;
- if (isReactBlock() == false) setHeroPos(getHeroPos().first + 2 ,getHeroPos().second);
- }
- void moveLeft(){
- direction = 3;
- if (isReactBlock() == false) setHeroPos(getHeroPos().first - 2 ,getHeroPos().second);
- }
- void jump(){
- if (x > 155 && x < 700 && y == Ground) wanaJump = true;
- height = 450;
- if ((direction == 2 || direction == 0)) direction = 4;
- if ((direction == 1 || direction == 3)) direction = 5;
- }
- /*--------------------*/
- void draw(){
- if (y == Ground && direction == 4) direction = 0;
- if (y == Ground && direction == 5) direction = 1;
- if (direction == 2 && y != Ground) direction = 4;
- if (direction == 3 && y != Ground) direction = 5;
- animation[direction].update(x,y);
- animation[direction].draw();
- if (direction % 2 == 0 && direction != 4) direction = 0;
- if ( direction % 2 == 1 && direction != 5) direction = 1;
- }
- };
- Hero hero;
- class Boss{
- private:
- int x;
- int y;
- int direction; //0.phai 1.trai
- Animation animation;
- public:
- friend class CircleBullet;
- Boss(){
- x = 200;
- y = -30;
- direction = 0;
- animation.setAnimation(path_Ricardo,0.3f);
- animation.scaleAnimation(0.5,0.5);
- }
- void setAnimation(string path , float speed){
- animation.setAnimation(path, speed);
- }
- II getBossPos(){
- return make_pair(x,y);
- }
- void setBossPos(int x ,int y){
- this->x = x;
- this->y = y;
- }
- int getBossDir(){
- return direction;
- }
- void setBossDir(int para){
- direction = para;
- }
- void update(){
- if (x > 600) direction = 1;
- if (x < 0) direction = 0;
- if (direction == 0) x+=4;
- else x-=4;
- }
- void draw(){
- animation.update(x,y);
- animation.draw();
- }
- };
- Boss ricardo;
- class Bullet{
- private:
- int x;
- int y;
- int direction;
- Animation animation;
- public:
- Bullet(int x, int y , int direction){
- this->x = x;
- this->y = y;
- this->direction = direction;
- animation.setAnimation(path_Rocket , 0.04f);
- }
- II getBulletPos(){
- return make_pair(x,y);
- }
- void setBulletPos(int x, int y){
- this->x = x;
- this->y = y;
- }
- int getBulletDir(){
- return direction;
- }
- void setBulletDir(int para){
- direction = para;
- }
- void setBulletScale(float para1 , float para2){
- animation.scaleAnimation(para1 , para2);
- }
- Sprite getSprite(){
- return animation.getAnimationSprite();
- }
- void setBulletRot(float para){
- animation.rotateAnimation(para);
- }
- void setBulletOri(int para1 , int para2){
- animation.originAnimation(para1,para2);
- }
- void draw(){
- animation.update(x,y);
- animation.draw();
- }
- };
- vector<Bullet> bullets;
- class CircleBullet{
- private:
- int x;
- int y;
- Texture texture;
- Sprite sprite;
- public:
- CircleBullet(){
- x = ricardo.getBossPos().first;
- y = ricardo.getBossPos().second;
- texture.loadFromFile("/home/hoaf13/workspace/SFMLtutorial/zero_images/CircleBullet/0.png");
- sprite.setTexture(texture);
- sprite.scale(0.5,0.5);
- }
- II getCircleBulletPos(){
- return make_pair(x,y);
- }
- void setCircleBulletPos(int x, int y){
- this->x = x;
- this->y = y;
- }
- void update(double timeElapsed){
- x = sprite.getPosition().x + timeElapsed * x;
- y = sprite.getPosition().y + timeElapsed * y;
- sprite.setPosition(x,y);
- }
- void draw(){
- window.draw(sprite);
- }
- };
- /*----Global Function----*/
- void createBullet(){
- createTime = createClock.getElapsedTime();
- if (createTime >= seconds(0.07f)){
- int X = ricardo.getBossPos().first + 120 + rand()%100 - 50;
- Bullet bullet(X, 80 , 2);
- bullet.setBulletScale(0.8 , 0.8);
- bullets.push_back(bullet);
- createClock.restart();
- }
- }
- void updateBullet(){
- rotateTime = rotateClock.getElapsedTime();
- for(int i=0;i<bullets.size();i++){
- if (bullets[i].getBulletDir() == 0) bullets[i].setBulletPos(bullets[i].getBulletPos().first + 4 , bullets[i].getBulletPos().second );
- if (bullets[i].getBulletDir() == 1) bullets[i].setBulletPos(bullets[i].getBulletPos().first - 4 , bullets[i].getBulletPos().second );
- if (bullets[i].getBulletDir() == 2) bullets[i].setBulletPos(bullets[i].getBulletPos().first , bullets[i].getBulletPos().second + 4 );
- if (bullets[i].getBulletDir() == 3) bullets[i].setBulletPos(bullets[i].getBulletPos().first , bullets[i].getBulletPos().second - 4);
- }
- while(1){
- int indez = -1;
- for(int i=0;i<bullets.size();i++){
- int X = bullets[i].getBulletPos().first;
- int Y = bullets[i].getBulletPos().second;
- if (X < 0 || X > Width || Y < 0 || Y > Height){
- indez = i;
- break;
- }
- }
- if (indez == -1) break;
- bullets.erase(bullets.begin() + indez , bullets.begin() + indez + 1);
- }
- }
- void processCrashing(){
- int X = hero.getHeroPos().first;
- int Y = hero.getHeroPos().second;
- for(int i=0;i<bullets.size();i++){
- int a = bullets[i].getBulletPos().first;
- int b = bullets[i].getBulletPos().second;
- for(int j=X - 20 ; j <= X + 20 ;j++ ){
- for(int k= Y - 20 ; k <= Y + 20 ; k++){
- if (j == a && k == b){
- hero.setHeroAlive(false);
- return;
- }
- }
- }
- }
- }
- /*-----------------------------*/
- int main()
- {
- /* Init */
- window.setFramerateLimit(120);
- vector<Block> blocks(Width);
- for(int i=0;i<Width;i++){
- blocks[i].setBlockPos(i,Ground+50);
- }
- ricardo.setAnimation(path_Ricardo,0.4f);
- CircleBullet circleBullet;
- /*=======================*/
- while(window.isOpen()){
- Event event;
- while(window.pollEvent(event)){
- if (event.type == Event::Closed) window.close();
- }
- window.clear();
- /*-----Platform-----*/
- for(int i=200;i<700;i+=20){
- blocks[i].draw();
- }
- /*------Entity----------*/
- processCrashing();
- cout << circleBullet.getCircleBulletPos().first << endl;
- realTime = realClock.getElapsedTime();
- circleBullet.update(realTime.asSeconds()/100);
- hero.processDeath();
- hero.gravityFall();
- hero.gravityJump();
- if (Keyboard::isKeyPressed(Keyboard::A)) hero.moveLeft();
- if (Keyboard::isKeyPressed(Keyboard::D)) hero.moveRight();
- if (Keyboard::isKeyPressed(Keyboard::W)) hero.jump();
- /*
- realTime = realClock.getElapsedTime();
- if (realTime > seconds(5)){
- createBullet();
- updateBullet();
- ricardo.draw();
- }
- */
- /*=======Stage Area==========*/
- /*===========================*/
- ricardo.draw();
- hero.draw();
- circleBullet.draw();
- for(int i=0;i<bullets.size();i++){
- bullets[i].draw();
- }
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement