Advertisement
TermSpar

SFML Bullet

Jul 1st, 2016
3,624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <SFML/Graphics.hpp>
  4. #include "Bullet.h"
  5.  
  6. class Enemy {
  7. public:
  8.     Enemy(sf::Vector2f size) {
  9.         enemy.setSize(size);
  10.         enemy.setFillColor(sf::Color::Red);
  11.     }
  12.  
  13.     void setPos(sf::Vector2f newPos) {
  14.         enemy.setPosition(newPos);
  15.     }
  16.  
  17.     void checkColl(Bullet bullet) {
  18.         if (bullet.getRight() > enemy.getPosition().x &&
  19.             bullet.getTop() < enemy.getPosition().y + enemy.getSize().y
  20.             && bullet.getBottom() > enemy.getPosition().y) {
  21.             enemy.setPosition(sf::Vector2f(4234432, 4234423));
  22.         }
  23.     }
  24.  
  25.     void draw(sf::RenderWindow &window) {
  26.         window.draw(enemy);
  27.     }
  28. private:
  29.     sf::RectangleShape enemy;
  30. };
  31.  
  32. /////////////////////////////////////////////////////////
  33.  
  34. #pragma once
  35.  
  36. #include <SFML/Graphics.hpp>
  37.  
  38. class Player {
  39. public:
  40.     Player(sf::Vector2f size) {
  41.         player.setSize(size);
  42.         player.setFillColor(sf::Color::Green);
  43.     }
  44.  
  45.     void move(sf::Vector2f dir) {
  46.         player.move(dir);
  47.     }
  48.  
  49.     int getX() {
  50.         return player.getPosition().x;
  51.     }
  52.  
  53.     int getY() {
  54.         return player.getPosition().y;
  55.     }
  56.  
  57.     void draw(sf::RenderWindow &window) {
  58.         window.draw(player);
  59.     }
  60. private:
  61.     sf::RectangleShape player;
  62. };
  63.  
  64. /////////////////////////////////////////////////////////
  65.  
  66. #pragma once
  67.  
  68. #include <SFML/Graphics.hpp>
  69.  
  70. class Bullet {
  71. public:
  72.     Bullet(sf::Vector2f size) {
  73.         bullet.setSize(size);
  74.         bullet.setFillColor(sf::Color::Blue);
  75.     }
  76.  
  77.     void fire(int speed) {
  78.         bullet.move(speed, 0);
  79.     }
  80.  
  81.     int getRight() {
  82.         return bullet.getPosition().x + bullet.getSize().x;
  83.     }
  84.  
  85.     int getLeft() {
  86.         return bullet.getPosition().x;
  87.     }
  88.  
  89.     int getTop() {
  90.         return bullet.getPosition().y;
  91.     }
  92.  
  93.     int getBottom() {
  94.         return bullet.getPosition().y + bullet.getSize().y;
  95.     }
  96.  
  97.     void draw(sf::RenderWindow &window) {
  98.         window.draw(bullet);
  99.     }
  100.  
  101.     void setPos(sf::Vector2f newPos) {
  102.         bullet.setPosition(newPos);
  103.     }
  104.  
  105. private:
  106.     sf::RectangleShape bullet;
  107. };
  108.  
  109. /////////////////////////////////////////////////////////
  110.  
  111. #include <iostream>
  112. #include <SFML/Graphics.hpp>
  113. #include "Bullet.h"
  114. #include "Enemy.h"
  115. #include "Player.h"
  116. #include <vector>
  117.  
  118. int main() {
  119.     sf::RenderWindow window;
  120.  
  121.     sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 755, (sf::VideoMode::getDesktopMode().height / 2) - 390);
  122.  
  123.     window.create(sf::VideoMode(1500, 700), "SFML Tutorial", sf::Style::Titlebar | sf::Style::Close);
  124.     window.setPosition(centerWindow);
  125.  
  126.     window.setKeyRepeatEnabled(true);
  127.  
  128.     //Define Objects:
  129.     Player player(sf::Vector2f(50, 50));
  130.     Enemy enemy(sf::Vector2f(50, 50));
  131.     std::vector<Bullet> bulletVec;
  132.  
  133.     enemy.setPos(sf::Vector2f(500, 50));
  134.  
  135.     bool isFiring = false;
  136.  
  137.     //Main Loop:
  138.     while (window.isOpen()) {
  139.  
  140.         sf::Event Event;
  141.  
  142.         //Event Loop:
  143.         while (window.pollEvent(Event)) {
  144.             switch (Event.type) {
  145.  
  146.             case sf::Event::Closed:
  147.                 window.close();
  148.             }
  149.  
  150.             int moveSpeed = 6;
  151.  
  152.             if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
  153.                 player.move(sf::Vector2f(0, -moveSpeed));
  154.             } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
  155.                 player.move(sf::Vector2f(0, moveSpeed));
  156.             } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  157.                 player.move(sf::Vector2f(-moveSpeed, 0));
  158.             } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  159.                 player.move(sf::Vector2f(moveSpeed, 0));
  160.             }
  161.  
  162.             if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
  163.                 isFiring = true;
  164.             }
  165.  
  166.         }
  167.  
  168.         window.clear();
  169.         if (isFiring == true) {
  170.             Bullet newBullet(sf::Vector2f(50, 5));
  171.             newBullet.setPos(sf::Vector2f(player.getX(), player.getY()));
  172.             bulletVec.push_back(newBullet);
  173.             isFiring = false;
  174.         }
  175.  
  176.         for (int i = 0; i < bulletVec.size(); i++) {
  177.             bulletVec[i].draw(window);
  178.             bulletVec[i].fire(3);
  179.         }
  180.  
  181.         for (int i = 0; i < bulletVec.size(); i++) {
  182.             enemy.checkColl(bulletVec[i]);
  183.         }
  184.  
  185.         player.draw(window);
  186.         enemy.draw(window);
  187.         window.display();
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement