Guest User

Untitled

a guest
Jan 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. class Character{
  9.  
  10. public:
  11. string sprite;
  12. int health;
  13. int defense;
  14. int speed;
  15. int highspeed;
  16. int experience;
  17. bool move;
  18. int x_pos;
  19. int y_pos;
  20.  
  21. bool GoingRight = false;
  22. bool GoingLeft = false;
  23. bool GoingUp = false;
  24. bool GoingDown = false;
  25.  
  26. sf::Texture texture;
  27.  
  28. //Constructor - Ran everytime a new instance of the class is created
  29. Character(string image){
  30. health = 100;
  31. defense = 100;
  32. speed = 6;
  33. experience = 0;
  34. x_pos = 0;
  35. y_pos = 0;
  36.  
  37. texture.loadFromFile(image);
  38.  
  39.  
  40. }
  41. sf::Sprite getSprite() {
  42. sf::Sprite sprite;
  43. sprite.setTexture(texture);
  44. sprite.setTextureRect(sf::IntRect(0, 0, 100, 100));
  45. sprite.setPosition(x_pos, y_pos);
  46. return sprite;
  47. }
  48.  
  49. //Destructor - Ran when the object is destroyed
  50. ~Character(){
  51.  
  52. }
  53. //Methods
  54. void forward();
  55. void backward();
  56. void left();
  57. void right();
  58. void Highspeed();
  59. void Lowspeed();
  60. void attack();
  61. };
  62.  
  63.  
  64.  
  65. void Character::forward(){
  66. y_pos = y_pos - speed;
  67. GoingUp = true;
  68. GoingDown = false;
  69. GoingLeft = false;
  70. GoingRight = false;
  71. }
  72. void Character::backward(){
  73. y_pos = y_pos + speed;
  74. GoingDown = true;
  75. GoingUp = false;
  76. GoingLeft = false;
  77. GoingRight = false;
  78. }
  79. void Character::left(){
  80. x_pos = x_pos - speed;
  81. GoingLeft = true;
  82. GoingRight = false;
  83. GoingUp = false;
  84. GoingDown = false;
  85. }
  86. void Character::right(){
  87. x_pos = x_pos + speed;
  88. GoingRight = true;
  89. GoingLeft = false;
  90. GoingUp = false;
  91. GoingDown = false;
  92. }
  93. void Character::Highspeed(){
  94. speed = 10;
  95. }
  96. void Character::Lowspeed(){
  97. speed = 6;
  98. }
  99.  
  100. Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Player.png");
  101.  
  102.  
  103.  
  104. int main() {
  105. // insert code here...
  106. int windowWidth = 1150;
  107. int windowHeight = 750;
  108. sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
  109.  
  110. while (window.isOpen()){
  111.  
  112. // check all the window's events that were triggered since the last iteration of the loop
  113. sf::Event event;
  114. while (window.pollEvent(event)){
  115. // "close requested" event: we close the window
  116. if (event.type == sf::Event::Closed)
  117. window.close();
  118.  
  119. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
  120. player.left();
  121. }
  122. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
  123. player.right();
  124. }
  125. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
  126. player.forward();
  127. }
  128. if (sf:: Keyboard::isKeyPressed(sf::Keyboard::Down)){
  129. player.backward();
  130. }
  131. if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
  132. {
  133. player.Highspeed();
  134. }
  135. if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
  136. {
  137. player.Lowspeed();
  138. }
  139.  
  140.  
  141.  
  142.  
  143. window.clear(sf::Color(255, 255, 255));
  144. window.draw(player.getSprite());
  145. window.display();
  146. window.setFramerateLimit(70);
  147.  
  148.  
  149.  
  150.  
  151. }
  152.  
  153.  
  154.  
  155. }
  156. }
Add Comment
Please, Sign In to add comment