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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Josh LL

Pages: [1]
1
OHHH I figured it out nvm thanks soo much everyone!!! (especially nogoodname!)

2
now how do I get it to un-pause.... crap....

3
Ohhhh that makes soo much sense cause I noticed if I moved my mouse while the game was paused it would un-pause and I wasn't sure why. Thank you soooo so much!!

4
Thank you sooo much for pointing these things out I really really appreciate it :) :).
I do have one question tho why is it so important to have the keypressed event in the event loop cause it totally works in where I had it. will it cause problems later on?? or is it just good practice to do so?

5
OMGOSH it actually works thanks soo much. now I actually used a while statement instead of switch cause I actually don't have any switch statements in my game. I hope this isn't a bad thing?? Here is my code if u want to check it out.

#include <iostream>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
using namespace sf;
using namespace std;


int main(){
    Clock clock;
    clock.restart();
   
   
    RenderWindow window(sf::VideoMode(1000, 1000), "My window");
    window.setTitle("SFML window");
   
    window.setFramerateLimit(60);
   
    FloatRect SwindowBounds({49.f, 0.f},{955.f, 970.f} );
   
    FloatRect AwindowBounds({0.f, 0.f},{1000.f, 1000.f} );
   
    Text text;
   
    Font font;
    if (!font.loadFromFile("sansation.ttf")){
        return EXIT_FAILURE;
    }
    text.setCharacterSize(100);
    text.setFillColor(sf::Color::Black);
    text.setString("five");
    text.setFont(font);
    text.setStyle(sf::Text::Bold | sf::Text::Underlined);
   
    Texture backGround;
    if(!backGround.loadFromFile("2616074.jpg")){
        return EXIT_FAILURE;
    }
    Sprite BG(backGround);
   
    CircleShape triangle(80.f, 3);
   
    Texture Astroid;
    if(!Astroid.loadFromFile("Astroid.png")){
        return EXIT_FAILURE;
    }
    Sprite sprite1(Astroid);
   
    Sprite sprite2(Astroid);
   
    Sprite sprite3(Astroid);
   
    Sprite sprite4(Astroid);
   
    Sprite sprite5(Astroid);
   
    BG.setScale(.7, .7);
   
    triangle.setPosition(425, 625);
   
    triangle.setScale(.4, .4);
   
    sprite1.setPosition(700, -50);
   
    sprite1.setScale(.15, .15);
   
    sprite2.setPosition(500, -50);
   
    sprite2.setScale(.17, .17);
   
    sprite3.setPosition(300, -50);
   
    sprite3.setScale(.12, .12);
   
    sprite4.setPosition(600, -50);
   
    sprite4.setScale(.18, .18);
   
    sprite5.setPosition(400, -50);
   
    sprite5.setScale(.15, .15);
   
   
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed){
                    window.close();
            }
           
    }
        //here is how I paused it
        bool isPaused = false;
        while(sf::Event::KeyReleased){
            if(event.key.code == sf::Keyboard::Escape)
                isPaused = true;
        break;
        }
        if (!isPaused) {
       
       
        window.clear(Color::White);
       
        window.draw(BG);
       
        Time elapsed = clock.getElapsedTime();
        int sec = elapsed.asSeconds();
        cout << sec << endl;
        if(sec >= 2){
            window.draw(sprite1);
            sprite1.move(0.0f,8.0f);
        }
        if(sec >= 5){
            window.draw(sprite2);
            sprite2.move(0.0f,7.0f);
        }
        if(sec >= 10){
            window.draw(sprite3);
            sprite3.move(0.0f,8.0f);
        }
        if(sec >= 15){
            window.draw(sprite4);
            sprite4.move(0.0f,9.0f);
        }
        if(sec >= 20){
            window.draw(sprite5);
            sprite5.move(0.0f,6.0f);
        }
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
            triangle.move(Vector2f(-15.f, 0.f));
        }
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            triangle.move(Vector2f(15.f, 0.f));
        }
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
            triangle.move(Vector2f(0.f, -15.f));
        }
       
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
            triangle.move(Vector2f(0.f, 15.f));
        }
       
        FloatRect shipBox = triangle.getGlobalBounds();
       
        FloatRect astroidBox1 = sprite1.getGlobalBounds();
       
        FloatRect astroidBox2 = sprite2.getGlobalBounds();
       
        FloatRect astroidBox3 = sprite3.getGlobalBounds();
       
        FloatRect astroidBox4 = sprite4.getGlobalBounds();
       
        FloatRect astroidBox5 = sprite5.getGlobalBounds();
       
        if (!shipBox.intersects(SwindowBounds))
        {
            window.close();
        }
        if (shipBox.intersects(astroidBox1))
        {
            window.close();
        }
        if (shipBox.intersects(astroidBox2))
        {
            window.close();
        }
        if (shipBox.intersects(astroidBox3))
        {
            window.close();
        }
        if (shipBox.intersects(astroidBox4))
        {
            window.close();
        }
        if (shipBox.intersects(astroidBox5))
        {
            window.close();
        }
        if (!astroidBox1.intersects(AwindowBounds))
        {
            sprite1.setPosition(rand()%1000-1,-50);
        }
        if (!astroidBox2.intersects(AwindowBounds))
        {
            sprite2.setPosition(rand()%1000-1,-50);
        }
        if (!astroidBox3.intersects(AwindowBounds))
        {
            sprite3.setPosition(rand()%1000-1,-50);
        }
        if (!astroidBox4.intersects(AwindowBounds))
        {
            sprite4.setPosition(rand()%1000-1,-50);
        }
        if (!astroidBox5.intersects(AwindowBounds))
        {
            sprite5.setPosition(rand()%1000-1,-50);
        }
       
       
        window.draw(triangle);
       
        window.display();}
}
}

 

6
I have tried this but as soon as i de-press the key it instantly turns the boolean value back so I still have the same problem

7
Hello!!

I'm quite new to SFML and C++ but please bear with me lol.

So I'm making a super simple game where you have to guide a spaceship through astroids, but I want to implement a pause feature once a key has been pressed. My problem is I can only get it to work WHILE I hold the key down and I want it so it pauses once I press and release the key, and then unpause when I press and release the key again, just like how normal pause buttons in real games work.

Not sure if this works or not but it would be great if it is possible to do this.

Thanks!!!
Josh LL

Pages: [1]
anything