Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What is wrong with this code?  (Read 4385 times)

0 Members and 1 Guest are viewing this topic.

Tekova

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
What is wrong with this code?
« on: December 02, 2021, 10:57:37 am »
So guys I'm making a shooting game in SFML where the player sprite shoots at 5 enemies who are randomly generated on the screen once destroyed. I was just testing out my code and I noticed that my bullet class isn't working. Everytime I press spacebar the sprite slows down but no bullets shoot. I am fairly new to SFML (started last week) so I guess I made a very stupid mistake (and also I might be following an old tutorial. I couldn't find a viable tutorial to rely on so I chose an old but good one). Here's the code :-
(dunno how to do the code thing)

Bullet.h :-
#pragma once
#include <SFML/Graphics.hpp>
#include "Player.h"

class Bullet {

public:
        Bullet(sf::Vector2f size, sf::Color color) {

                bullet.setSize(size);
                bullet.setFillColor(color);

        }

        void setPosition(sf::Vector2f position) {
                bullet.setPosition(position);
        }

        void playerShoot(Player& player, float velocity){
                        rotation = player.getRotation();
                        fire.x = sin(rotation * (3.14 / 180)) * velocity;
                        fire.y = -cos(rotation * (3.14 / 180)) * velocity;
                        bullet.move(fire);             
        }

        void enemyShoot() {



        }

        void draw(sf::RenderWindow& window) {

                window.draw(bullet);

        }

        int getTop() {
                return bullet.getPosition().x;
        }

        int getRight() {
                return bullet.getPosition().x + bullet.getSize().x;
        }

        int getBottom() {
                return bullet.getPosition().y + bullet.getSize().y;
        }

        int getLeft() {
                return bullet.getPosition().x;
        }

private:
        sf::RectangleShape bullet;
        sf::Vector2f fire;
        double rotation;
};
 

Player.h :-
pragma once
#include<SFML/Graphics.hpp>

class Player {

public:

        Player(sf::Vector2f size) {
                player.setSize(size);
                player.setRotation(0);
        }

        sf::Vector2f getSize() {
                return player.getSize();
        }

        void setPosition(sf::Vector2f position) {
                player.setPosition(position);
        }

        sf::Vector2f getPosition() {
                return player.getPosition();
        }

        void setOrigin(sf::Vector2f origin) {
                player.setOrigin(origin);
        }

        sf::Vector2f getOrigin() {
                return player.getOrigin();
        }

        void setRotation(double rotation) {
                player.setRotation(rotation);
        }

        double getRotation() {
                return player.getRotation();
        }

        void setTexture(sf::Texture &texture) {
                player.setTexture(&texture);
        }

        void setTexture(sf::IntRect texture) {
                player.setTextureRect(texture);
        }

        void move(double angle, float velocity) {

                rotation = angle;
                vec.x = sin(rotation * (3.14 / 180)) * velocity;
                vec.y = -cos(rotation * (3.14 / 180)) * velocity;
                player.move(vec);

        }

        void draw(sf::RenderWindow& window) {
                window.draw(player);
        }

private:

        sf::RectangleShape player;
        sf::Vector2f vec;
        double rotation;

};
 

main.cpp :-
#include<SFML\Graphics.hpp>
#include<SFML\Audio.hpp>
#include<iostream>
#include "Player.h"
#include "Bullet.h"
#include "Enemy.h"

int main() {

        int i = 0;
        bool isShooting = false;
        std::vector<sf::Vector2f> position;

        sf::Texture backgroundTex;
        sf::Sprite background;

        if (!backgroundTex.loadFromFile("space.jpg")) std::cout << "There was an unprecedented problem";
        background.setTexture(backgroundTex);

        sf::RenderWindow window(sf::VideoMode(700, 700), "Test", sf::Style::Default);
       
        Player player(sf::Vector2f(49, 56));
        player.setOrigin(sf::Vector2f(player.getSize().x / 2, player.getSize().y / 2));
        player.setPosition(sf::Vector2f(350, 350));
        player.setRotation(36);

        sf::Texture tex;

        if (!tex.loadFromFile("sheet.png")) {

                std::cout << "Error loading texture" << std::endl;

        }

        player.setTexture(tex);

        sf::Vector2u texSize = tex.getSize();

        texSize.x /= 3;
        texSize.y /= 3;

        sf::IntRect texture(texSize.x * 2, texSize.y * 1, texSize.x, texSize.y);
        player.setTexture(texture);

        sf::Vector2f bulletInitPos(player.getOrigin().x, player.getOrigin().y + player.getSize().y / 2);

        std::vector<Bullet> bulletVec;

        while (window.isOpen()) {

                sf::Event event;

                while (window.pollEvent(event)) {

                        switch (event.type) {

                        case sf::Event::Closed:
                                window.close();
                                break;

                        }
                }



                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                                player.move(player.getRotation(), 0.5);
                        }if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                                player.move(player.getRotation(), -0.5);
                        }if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                                player.setRotation(player.getRotation() - 0.5);
                        }if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                                player.setRotation(player.getRotation() + 0.5);
                        }


                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
                                isShooting = true;
                        }
               

                if (isShooting == true) {
                        Bullet newBullet(sf::Vector2f(30, 5), sf::Color::Green);
                        newBullet.setPosition(sf::Vector2f(player.getPosition().x, player.getPosition().y));
                        bulletVec.push_back(newBullet);
                        isShooting = false;
                }

                for (int i = 0; i < bulletVec.size(); i++) {
                        if (i == 0) {
                                bulletVec.pop_back();
                        }
                        else {
                                bulletVec[i].draw(window);
                                bulletVec[i].playerShoot(player, 3);
                        }
                }

                window.clear();
                window.draw(background);
                player.draw(window);
                window.display();

        }
}
« Last Edit: December 02, 2021, 01:29:42 pm by eXpl0it3r »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: What is wrong with this code?
« Reply #1 on: December 02, 2021, 06:32:00 pm »
well, the first thing is probably here:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
when spacebar is pressed, you are starting to shoot. the problem is that when you hit spacebar with your hand for like 0,1 second, the shoothing loop happens tons of times. the computer processes it really quick. so thats why it slows everything down, you are creating tons of bullets per second.

try changing it for sf::Event::KeyReleased :
https://www.sfml-dev.org/tutorials/2.5/window-events.php#the-keypressed-and-keyreleased-events


the second point is probably that you are clearing the window after drawing your bullets.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything