Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <SFML/Graphics.hpp>
- #include "Bullet.h"
- class Enemy {
- public:
- Enemy(sf::Vector2f size) {
- enemy.setSize(size);
- enemy.setFillColor(sf::Color::Red);
- }
- void setPos(sf::Vector2f newPos) {
- enemy.setPosition(newPos);
- }
- void checkColl(Bullet bullet) {
- if (bullet.getRight() > enemy.getPosition().x &&
- bullet.getTop() < enemy.getPosition().y + enemy.getSize().y
- && bullet.getBottom() > enemy.getPosition().y) {
- enemy.setPosition(sf::Vector2f(4234432, 4234423));
- }
- }
- void draw(sf::RenderWindow &window) {
- window.draw(enemy);
- }
- private:
- sf::RectangleShape enemy;
- };
- /////////////////////////////////////////////////////////
- #pragma once
- #include <SFML/Graphics.hpp>
- class Player {
- public:
- Player(sf::Vector2f size) {
- player.setSize(size);
- player.setFillColor(sf::Color::Green);
- }
- void move(sf::Vector2f dir) {
- player.move(dir);
- }
- int getX() {
- return player.getPosition().x;
- }
- int getY() {
- return player.getPosition().y;
- }
- void draw(sf::RenderWindow &window) {
- window.draw(player);
- }
- private:
- sf::RectangleShape player;
- };
- /////////////////////////////////////////////////////////
- #pragma once
- #include <SFML/Graphics.hpp>
- class Bullet {
- public:
- Bullet(sf::Vector2f size) {
- bullet.setSize(size);
- bullet.setFillColor(sf::Color::Blue);
- }
- void fire(int speed) {
- bullet.move(speed, 0);
- }
- int getRight() {
- return bullet.getPosition().x + bullet.getSize().x;
- }
- int getLeft() {
- return bullet.getPosition().x;
- }
- int getTop() {
- return bullet.getPosition().y;
- }
- int getBottom() {
- return bullet.getPosition().y + bullet.getSize().y;
- }
- void draw(sf::RenderWindow &window) {
- window.draw(bullet);
- }
- void setPos(sf::Vector2f newPos) {
- bullet.setPosition(newPos);
- }
- private:
- sf::RectangleShape bullet;
- };
- /////////////////////////////////////////////////////////
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include "Bullet.h"
- #include "Enemy.h"
- #include "Player.h"
- #include <vector>
- int main() {
- sf::RenderWindow window;
- sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 755, (sf::VideoMode::getDesktopMode().height / 2) - 390);
- window.create(sf::VideoMode(1500, 700), "SFML Tutorial", sf::Style::Titlebar | sf::Style::Close);
- window.setPosition(centerWindow);
- window.setKeyRepeatEnabled(true);
- //Define Objects:
- Player player(sf::Vector2f(50, 50));
- Enemy enemy(sf::Vector2f(50, 50));
- std::vector<Bullet> bulletVec;
- enemy.setPos(sf::Vector2f(500, 50));
- bool isFiring = false;
- //Main Loop:
- while (window.isOpen()) {
- sf::Event Event;
- //Event Loop:
- while (window.pollEvent(Event)) {
- switch (Event.type) {
- case sf::Event::Closed:
- window.close();
- }
- int moveSpeed = 6;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- player.move(sf::Vector2f(0, -moveSpeed));
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
- player.move(sf::Vector2f(0, moveSpeed));
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
- player.move(sf::Vector2f(-moveSpeed, 0));
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
- player.move(sf::Vector2f(moveSpeed, 0));
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
- isFiring = true;
- }
- }
- window.clear();
- if (isFiring == true) {
- Bullet newBullet(sf::Vector2f(50, 5));
- newBullet.setPos(sf::Vector2f(player.getX(), player.getY()));
- bulletVec.push_back(newBullet);
- isFiring = false;
- }
- for (int i = 0; i < bulletVec.size(); i++) {
- bulletVec[i].draw(window);
- bulletVec[i].fire(3);
- }
- for (int i = 0; i < bulletVec.size(); i++) {
- enemy.checkColl(bulletVec[i]);
- }
- player.draw(window);
- enemy.draw(window);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement