Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <SFML\Graphics.hpp>
- class Player {
- public:
- Player(std::string directory) {
- if (!playerTexture.loadFromFile(directory)) {
- std::cerr << "Error\n";
- }
- playerSprite.setTexture(playerTexture);
- }
- void movePlayer(sf::Vector2f distance) {
- playerSprite.move(distance);
- }
- void drawPlayer(sf::RenderWindow &window) {
- window.draw(playerSprite);
- }
- int getX() {
- return playerSprite.getPosition().x;
- }
- int getY() {
- return playerSprite.getPosition().y;
- }
- void setPos(sf::Vector2f newPos) {
- playerSprite.setPosition(newPos);
- }
- private:
- sf::Texture playerTexture;
- sf::Sprite playerSprite;
- };
- #pragma once
- #include <SFML\Graphics.hpp>
- class Enemy {
- public:
- Enemy(sf::Vector2f size) {
- enemy.setSize(size);
- enemy.setFillColor(sf::Color::Red);
- }
- void drawEnemy(sf::RenderWindow &window) {
- window.draw(enemy);
- }
- void setPos(sf::Vector2f newPos) {
- enemy.setPosition(newPos);
- }
- int getLeft() {
- return enemy.getPosition().x;
- }
- int getBottom() {
- return enemy.getPosition().y + enemy.getSize().y;
- }
- bool getDestroyed() {
- return isDestroyed;
- }
- void getColl() {
- bottom = enemy.getPosition().y + enemy.getSize().y;
- left = enemy.getPosition().x;
- right = enemy.getPosition().x + enemy.getSize().x;
- top = enemy.getPosition().y;
- }
- void checkCollision(Bullet bullet) {
- if(enemy.getGlobalBounds().intersects(bullet.bounds())){
- isDestroyed = true;
- enemy.setPosition({ 143332, 23432 });
- bullet.setDestroyed(true);
- }
- }
- private:
- sf::RectangleShape enemy;
- bool isDestroyed = false;
- float bottom;
- float left;
- float right;
- float top;
- };
- #pragma once
- #include <SFML\Graphics.hpp>
- class Bullet {
- public:
- Bullet() {
- //Default
- }
- Bullet(sf::Vector2f size, char nDirection) {
- direction = nDirection;
- bullet.setFillColor(sf::Color::Blue);
- bullet.setSize(size);
- }
- void setDirection(char nDir) {
- direction = nDir;
- }
- void setPos(sf::Vector2f newPos) {
- bullet.setPosition(newPos);
- }
- void fire(float moveSpeed) {
- if (direction == 'l') {
- bullet.move(-moveSpeed, 0);
- } else if (direction == 'r'){
- bullet.move(moveSpeed, 0);
- } else if (direction == 'u'){
- bullet.move(0, -moveSpeed);
- }
- }
- 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 setDestroyed(bool value) {
- isDestroyed = value;
- }
- bool getDestroyed() {
- return isDestroyed;
- }
- sf::FloatRect bounds(){
- return bullet.getGlobalBounds();
- }
- ~Bullet() {
- }
- private:
- sf::RectangleShape bullet;
- char direction;
- bool isDestroyed = false;
- };
- #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) - 955, (sf::VideoMode::getDesktopMode().height / 2) - 540);
- window.create(sf::VideoMode(1900, 1000), "SFML Project", sf::Style::Titlebar | sf::Style::Close);
- window.setPosition(centerWindow);
- //Player Object:
- Player player("tank.png");
- player.setPos(sf::Vector2f(700, 500));
- //Bullet vector:
- std::vector<Bullet> bulletVec;
- //Enemy Objects:
- bool isFiring = false;
- char direction = 'r';
- bool drawEnemy = true;
- //Main Loop:
- while (window.isOpen()) {
- static float moveSpeed = 0.2;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
- player.movePlayer(sf::Vector2f(-moveSpeed, 0));
- direction = 'l';
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
- player.movePlayer(sf::Vector2f(moveSpeed, 0));
- direction = 'r';
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
- player.movePlayer(sf::Vector2f(0, -moveSpeed));
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
- player.movePlayer(sf::Vector2f(0, moveSpeed));
- }
- //Handle firing bullets:
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
- static int i = 0;
- i++;
- if (i <= 3) {
- isFiring = true;
- } else {
- isFiring = false;
- i = 0;
- }
- moveSpeed = 1;
- }
- sf::Event Event;
- //Event Loop:
- while (window.pollEvent(Event)) {
- switch (Event.type) {
- case sf::Event::Closed:
- window.close();
- case sf::Event::KeyReleased:
- if (Event.key.code == sf::Keyboard::Space) {
- isFiring = false;
- }
- }
- }
- window.clear();
- //Create bullet and add it to vector:
- if (isFiring == true) {
- Bullet newBullet(sf::Vector2f(50, 10), direction);
- newBullet.setPos(sf::Vector2f(player.getX() + 90, player.getY() + 40));
- bulletVec.push_back(newBullet);
- isFiring = false;
- sf::sleep;
- }
- //Draw and fire bullet:
- for (int i = 0; i < bulletVec.size(); i++) {
- bulletVec[i].draw(window);
- bulletVec[i].fire(40);
- }
- //Drawings:
- player.drawPlayer(window);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement