Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML/Graphics.hpp>
- using namespace std;
- class Character{
- public:
- string sprite;
- int health;
- int defense;
- int speed;
- int highspeed;
- int experience;
- bool move;
- int x_pos;
- int y_pos;
- bool GoingRight = false;
- bool GoingLeft = false;
- bool GoingUp = false;
- bool GoingDown = false;
- sf::Texture texture;
- //Constructor - Ran everytime a new instance of the class is created
- Character(string image){
- health = 100;
- defense = 100;
- speed = 6;
- experience = 0;
- x_pos = 0;
- y_pos = 0;
- texture.loadFromFile(image);
- }
- sf::Sprite getSprite() {
- sf::Sprite sprite;
- sprite.setTexture(texture);
- sprite.setTextureRect(sf::IntRect(0, 0, 100, 100));
- sprite.setPosition(x_pos, y_pos);
- return sprite;
- }
- //Destructor - Ran when the object is destroyed
- ~Character(){
- }
- //Methods
- void forward();
- void backward();
- void left();
- void right();
- void Highspeed();
- void Lowspeed();
- void attack();
- };
- void Character::forward(){
- y_pos = y_pos - speed;
- GoingUp = true;
- GoingDown = false;
- GoingLeft = false;
- GoingRight = false;
- }
- void Character::backward(){
- y_pos = y_pos + speed;
- GoingDown = true;
- GoingUp = false;
- GoingLeft = false;
- GoingRight = false;
- }
- void Character::left(){
- x_pos = x_pos - speed;
- GoingLeft = true;
- GoingRight = false;
- GoingUp = false;
- GoingDown = false;
- }
- void Character::right(){
- x_pos = x_pos + speed;
- GoingRight = true;
- GoingLeft = false;
- GoingUp = false;
- GoingDown = false;
- }
- void Character::Highspeed(){
- speed = 10;
- }
- void Character::Lowspeed(){
- speed = 6;
- }
- Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Player.png");
- int main() {
- // insert code here...
- int windowWidth = 1150;
- int windowHeight = 750;
- sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
- while (window.isOpen()){
- // check all the window's events that were triggered since the last iteration of the loop
- sf::Event event;
- while (window.pollEvent(event)){
- // "close requested" event: we close the window
- if (event.type == sf::Event::Closed)
- window.close();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
- player.left();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
- player.right();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
- player.forward();
- }
- if (sf:: Keyboard::isKeyPressed(sf::Keyboard::Down)){
- player.backward();
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
- {
- player.Highspeed();
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
- {
- player.Lowspeed();
- }
- window.clear(sf::Color(255, 255, 255));
- window.draw(player.getSprite());
- window.display();
- window.setFramerateLimit(70);
- }
- }
- }
Add Comment
Please, Sign In to add comment