Advertisement
Cheese428

Untitled

Dec 8th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. // game loop
  2. bool isFiring = false;
  3. while (window.isOpen())
  4. {
  5. sf::Event event;
  6.  
  7. sf::Vector2f player1position = player1.getPosition();
  8.  
  9. nameplate.setPosition(player1position.x,player1position.y-50);
  10.  
  11. // handle events
  12.  
  13. // allows the player to move
  14. if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
  15. player1.move(0, -10);
  16. }
  17. if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  18. player1.move(-10, 0);
  19. }
  20. if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
  21. player1.move(0, 10);
  22. }
  23. if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  24. player1.move(10, 0);
  25. }
  26.  
  27. // fires the projectile if the button is clicked
  28.  
  29. if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
  30. if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
  31. sf::Vector2i mouseposition = sf::Mouse::getPosition(window);
  32. isFiring = true;
  33. }
  34. }
  35.  
  36. // makes sure the player is inbounds
  37.  
  38. if (player1position.x < 0) {
  39. player1.setPosition(0, player1position.y);
  40. }
  41. if (player1position.x > windowwidth) {
  42. player1.setPosition(windowwidth, player1position.y);
  43. }
  44. if (player1position.y < 0) {
  45. player1.setPosition(player1position.x, 0);
  46. }
  47. if (player1position.y > windowheight) {
  48. player1.setPosition(player1position.x, windowheight);
  49. }
  50.  
  51.  
  52. while (window.pollEvent(event)) { // if the x button on the window is pressed, it closes
  53. switch (event.type) {
  54. case sf::Event::Closed:
  55. window.close();
  56. break;
  57. }
  58.  
  59. }
  60.  
  61. window.clear();
  62.  
  63. if (isFiring == true) {
  64. Projectile newProjectile;
  65. sf::Texture projectileTexture;
  66. projectileTexture.loadFromFile("textures/projectile.png");
  67. newProjectile.sprite.setTexture(projectileTexture);
  68. newProjectile.setScale(.15, .15);
  69. newProjectile.setOrigin(407, 69);
  70. newProjectile.setPosition(-10, 0);
  71. newProjectile.setPosition(player1position.x,player1position.y);
  72. projectileVec.push_back(newProjectile);
  73. isFiring = false;
  74. }
  75.  
  76. for (int i = 0; i < projectileVec.size(); i++) {
  77. projectileVec[i].draw(window);
  78. projectileVec[i].fire(7);
  79. }
  80.  
  81. for (int i = 0; i < projectileVec.size(); i++) {
  82. //enemy.checkColl(projectileVec[i]);
  83. }
  84.  
  85. // draw objects here
  86.  
  87. window.draw(player1.sprite);
  88. window.draw(nameplate.rectangle);
  89. window.draw(nameplate.text);
  90.  
  91. // display objects
  92.  
  93. window.display();
  94. }
  95. system("pause");
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement