C++ help

Started by
12 comments, last by jpetrie 7 years, 1 month ago
Yes thank you do you think you could take at my code and help me with that?
#include <iostream>
#include <SFML/Graphics.hpp>
#include<cmath>
//#include"block.h"
//sf::RenderWindow win(sf::VideoMode(800,600),"RPG");
using namespace std;
enum class State{PAUSE,PLAYING};

class Block{
sf::Texture tex;
sf::Sprite spr;
sf::Vector2f pos;

public:
int pos_i;
Block(float x=0.f,float y=0.f,float hi=0.f,float wd=0.f){

sf::Vector2f size(hi,wd);
tex.loadFromFile("1.png");
pos.x=x; pos.y=y;
spr.setTexture(tex);
spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));
spr.setPosition(sf::Vector2f(x,y));
}


void draw(sf::RenderWindow* win){

win->draw(spr);
}


void set_size(sf::Vector2f size){spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));}
sf::Vector2f get_pos(){return pos;}
sf::Sprite& get_tex(){return spr;}

void set_pos(sf::Vector2f pos){spr.move(pos);}
};

class weapon{
Block blc;
bool m_up;
bool m_dn;
bool m_lt;
bool m_rt;
sf::Vector2f m_res;

public:
weapon():blc(0,0,8,8){}
void move_rt(){m_rt=true;}
void move_lt(){m_lt=true;}
void move_up(){m_up=true;}
void move_dn(){m_dn=true;}

void stop_rt(){m_rt=false;}
void stop_lt(){m_lt=false;}
void stop_up(){m_up=false;}
void stop_dn(){m_dn=false;}
void setPos(sf::Vector2f pos){blc.set_pos(pos);}
sf::Sprite getWep(){return blc.get_tex();}
void update( sf::Vector2f pos ,sf::Vector2i mse_pos){

if(m_up)
blc.set_pos(pos - sf::Vector2f(0.f,10.f));
if(m_dn)
blc.set_pos(pos + sf::Vector2f(0.f,10.f));
if(m_rt)
blc.set_pos(pos - sf::Vector2f(10.f,0.f));
if(m_lt)
blc.set_pos(pos + sf::Vector2f(10.f,0.f));



float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;

blc.get_tex().setRotation(angle);
}



};

class Player{
Block blc;
weapon Weapon;
sf::Vector2f m_res;


bool m_up;
bool m_dn;
bool m_lt;
bool m_rt;

float m_speed;
sf::Time last_hit;

public:
Player():blc(0,0,16,16){

}

void spawn(sf::Vector2f res,sf::Vector2f loc){
blc.set_pos(loc);
Weapon.setPos(sf::Vector2f(loc.x+5.0f,loc.y+5.0));


m_res.x=res.x;
m_res.y=res.y;

}
weapon& getWep(){return Weapon;}
bool hit(sf::Time time_hit);
sf::FloatRect get_pos(){return blc.get_tex().getGlobalBounds();}
sf::Vector2f get_cen(){return blc.get_pos();}
float get_rot(){return blc.get_tex().getRotation();}
sf::Sprite get_pla(){return blc.get_tex();}

void move_rt(){m_rt=true;}
void move_lt(){m_lt=true;}
void move_up(){m_up=true;}
void move_dn(){m_dn=true;}

void stop_rt(){m_rt=false;}
void stop_lt(){m_lt=false;}
void stop_up(){m_up=false;}
void stop_dn(){m_dn=false;}

void update( sf::Vector2f pos ,sf::Vector2i mse_pos){

if(m_up)

blc.set_pos(pos - sf::Vector2f(0.f,10.f));
if(m_dn)
blc.set_pos(pos + sf::Vector2f(0.f,10.f));
if(m_rt)
blc.set_pos(pos - sf::Vector2f(10.f,0.f));

if(m_lt)
blc.set_pos(pos + sf::Vector2f(10.f,0.f));



float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;

blc.get_tex().setRotation(angle);
}


};
Advertisement

Please use the code tags when posting code.

In the editor it's the button that looks like <>.

Hello to all my stalkers.

Yes this is my first time. I'm new to this whole thing so thanks for all the feedback.

Yes thank you do you think you could take at my code and help me with that?
#include <iostream>
#include <SFML/Graphics.hpp>
#include<cmath>
//#include"block.h"
//sf::RenderWindow win(sf::VideoMode(800,600),"RPG");
using namespace std;
enum class State{PAUSE,PLAYING};
class Block{
sf::Texture tex;
sf::Sprite spr;
sf::Vector2f pos;
public:
int pos_i;
Block(float x=0.f,float y=0.f,float hi=0.f,float wd=0.f){
sf::Vector2f size(hi,wd);
tex.loadFromFile("1.png");
pos.x=x; pos.y=y;
spr.setTexture(tex);
spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));
spr.setPosition(sf::Vector2f(x,y));
}
void draw(sf::RenderWindow* win){
win->draw(spr);
}
void set_size(sf::Vector2f size){spr.setTextureRect(sf::IntRect(0,0,size.x,size.y));}
sf::Vector2f get_pos(){return pos;}
sf::Sprite& get_tex(){return spr;}
void set_pos(sf::Vector2f pos){spr.move(pos);}
};
class weapon{
Block blc;
bool m_up;
bool m_dn;
bool m_lt;
bool m_rt;
sf::Vector2f m_res;
public:
weapon():blc(0,0,8,8){}
void move_rt(){m_rt=true;}
void move_lt(){m_lt=true;}
void move_up(){m_up=true;}
void move_dn(){m_dn=true;}
void stop_rt(){m_rt=false;}
void stop_lt(){m_lt=false;}
void stop_up(){m_up=false;}
void stop_dn(){m_dn=false;}
void setPos(sf::Vector2f pos){blc.set_pos(pos);}
sf::Sprite getWep(){return blc.get_tex();}
void update( sf::Vector2f pos ,sf::Vector2i mse_pos){
if(m_up)
blc.set_pos(pos - sf::Vector2f(0.f,10.f));
if(m_dn)
blc.set_pos(pos + sf::Vector2f(0.f,10.f));
if(m_rt)
blc.set_pos(pos - sf::Vector2f(10.f,0.f));
if(m_lt)
blc.set_pos(pos + sf::Vector2f(10.f,0.f));
float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;
blc.get_tex().setRotation(angle);
}
};
class Player{
Block blc;
weapon Weapon;
sf::Vector2f m_res;
bool m_up;
bool m_dn;
bool m_lt;
bool m_rt;
float m_speed;
sf::Time last_hit;
public:
Player():blc(0,0,16,16){
}
void spawn(sf::Vector2f res,sf::Vector2f loc){
blc.set_pos(loc);
Weapon.setPos(sf::Vector2f(loc.x+5.0f,loc.y+5.0));
m_res.x=res.x;
m_res.y=res.y;
}
weapon& getWep(){return Weapon;}
bool hit(sf::Time time_hit);
sf::FloatRect get_pos(){return blc.get_tex().getGlobalBounds();}
sf::Vector2f get_cen(){return blc.get_pos();}
float get_rot(){return blc.get_tex().getRotation();}
sf::Sprite get_pla(){return blc.get_tex();}
void move_rt(){m_rt=true;}
void move_lt(){m_lt=true;}
void move_up(){m_up=true;}
void move_dn(){m_dn=true;}
void stop_rt(){m_rt=false;}
void stop_lt(){m_lt=false;}
void stop_up(){m_up=false;}
void stop_dn(){m_dn=false;}
void update( sf::Vector2f pos ,sf::Vector2i mse_pos){
if(m_up)
blc.set_pos(pos - sf::Vector2f(0.f,10.f));
if(m_dn)
blc.set_pos(pos + sf::Vector2f(0.f,10.f));
if(m_rt)
blc.set_pos(pos - sf::Vector2f(10.f,0.f));
if(m_lt)
blc.set_pos(pos + sf::Vector2f(10.f,0.f));
float angle=(atan2(mse_pos.y-m_res.y/2,mse_pos.x-m_res.x/2))*180/3.14f;
blc.get_tex().setRotation(angle);
}
};

yes I forgot player/weapon update in main

sf::Vector2f plaPos(pla.get_cen());
pla.update(plaPos,sf::Mouse::getPosition());
pla.getWep().update(plaPos,sf::Mouse::getPosition());
}

You already have a thread for reviewing this code here.

This topic is closed to new replies.

Advertisement