Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Need help with score meter

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #15636

    Todd

    Hi everyone,

    So I am working on my flappy bird like game, and I am trying to get the score meter at the top to continually increase as the player is moving. I would like this distance score to go increase and stop once you hit a bomb. I currently have it set with my code like this….

    Timer {
          id:distancetimer
          interval: 1;
          running: true
          repeat: true
          onTriggered: {
              score = score+1
          }

    With this the distance is showing during the game but it is increasing very fast and wont stop once I hit the object. Can you help me figure out how to fix this?

    Thanks for all the help you provided so far!

    #15639

    Todd

    By the way I am using the flappy bird template while making this game. So here is the whole numbers.qml file in case you need to see more.

     

    import Felgo 3.0
    import QtQuick 2.0
    import "../entities"
    
    Item {
      id: numbers
      width: row.width
      height: row.heigth
      property string color
      property int number: 0
    
      property string imagePath: "../../assets/img/"
    
      function truncate(_value)
      {
        if (_value<0) return Math.ceil(_value);
        else return Math.floor(_value);
      }
    
      onNumberChanged: {
        if(number > 9999) number = 9999
        var trailingZero = true
    
        var unit = truncate((number / 1000) % 10)
        if((trailingZero && unit <= 0) || unit >= 10) {
          position1000.source = imagePath + "empty_big.png"
        } else {
          trailingZero = false
          position1000.source = imagePath + unit+"_big"+color+".png"
        }
    
        unit = truncate((number / 100) % 10)
        if((trailingZero && unit <= 0) || unit >= 10) {
          position100.source = imagePath + "empty_big.png"
        } else {
          trailingZero = false
          position100.source = imagePath + unit+"_big"+color+".png"
        }
    
        unit = truncate((number / 10) % 10)
        if((trailingZero && unit <= 0) || unit >= 10) {
          position10.source = imagePath + "empty_big.png"
        } else {
          position10.source = imagePath + unit+"_big"+color+".png"
        }
    
        unit = truncate(number % 10)
        // test also if there is remainder, if no remainder it might be 10 and the first number is needed, otherwise a fragment is displayed.
        if(number % 10 && ((trailingZero && unit <= 0) || unit >= 10)) {
          position1.source = imagePath + "empty_big.png"
        } else {
          position1.source = imagePath + unit+"_big"+color+".png"
        }
    
        if(number <= 0) {
          position1.source = imagePath + "0_big"+color+".png"
        }
      }
    
      Timer {
          id:distancetimer
          interval: 1;
          running: true
          repeat: true
          onTriggered: {
              score = score+1
          }
    
      }
    
      Row {
        id: row
        height: position1.height
        x: number >= 1000 ? -12 : number >= 100 ? -24 : number >= 10 ? -36 : -48
    
        MultiResolutionImage {
          id: position1000
          source: imagePath + "empty_big.png"
        }
        MultiResolutionImage {
          id: position100
          source: imagePath + "empty_big.png"
        }
        MultiResolutionImage {
          id: position10
          source: imagePath + "empty_big.png"
        }
    
        MultiResolutionImage {
          id: position1
          source: imagePath + "0_big"+color+".png"
        }
      }
    }

     

    #15642

    Isak

    todd.pocock@prodigygames.org said:

    Hi everyone,

    So I am working on my flappy bird like game, and I am trying to get the score meter at the top to continually increase as the player is moving. I would like this distance score to go increase and stop once you hit a bomb. I currently have it set with my code like this….

    Timer {
          id:distancetimer
          interval: 1;
          running: true
          repeat: true
          onTriggered: {
              score = score+1
          }

    With this the distance is showing during the game but it is increasing very fast and wont stop once I hit the object. Can you help me figure out how to fix this?

    Thanks for all the help you provided so far!

    Hello,

    I am a beginner myself but…

    To me it looks like your timer never stops? I think it would work if you put in running: player in your timer where the player is an bool property which is true as long as the player is alive. So you would also need to set this property to false when player is hitting a bomb.

     

    I’m sure its better ways to do this but i am pretty sure this should work:)

     

    /Isak

     

     

     

    #15646

    Alex
    Felgo Team

    Hi Todd,

    Isak is of course right, your Timer is running but never stops. With QML you have several options of stopping the timer. You could make a so called property binding on the running property of the Timer, like Isak suggested. If you don’t know what a property binding is, please quickly go through our getting started tutorial (there is also a video available).

    Timer {
      running: gameScene.gameIsRunning
      // ... more stuff
    }

    The gameIsRunning property is already available if you look at the GameScene.qml file. By the way, I would also suggest to put your Timer there instead of the Numbers.qml, as the timer is more game logic related and the Numbers.qml is only used to display a number.

    The other possibility is to set the running property of the Timer to false initially, and call distancetimer.start() and distancetimer.stop() manually, e.g. in the startGame() and stopGame() functions of the GameScene.qml. This is again easier if the Timer is part of the GameScene.qml, as you don’t need to make aliases or similar then. This topic is also covered in the getting started tutorial.

    And one last detail that I want to add: You are triggering the timer every millisecond, which is creating a minor overhead. As your game is running at 60FPS, a new frame is rendered 60 times per second, which is about once every 17 millisecond. Any change between the frames won’t be visible immediately, until the next frame is rendered 17milliseconds later. So there is no real reason to trigger the timer 17 times in this time frame, instead of once every 17 milliseconds. If you change the interval property of your timer to 17 (and then maybe increase the score by more than 1 to compensate), you are getting a minor performance improvement, as you eliminate 16/17 timer triggers by getting the same results. This won’t be visible in your small project, but it can have an impact in larger ones. I wanted to mention it anyway, since this forum is also a knowledge base to other users as well right 🙂

    Cheers,
    Alex

    #15655

    Todd

    Thanks guys, yes I now am able to get it to stop once a game over is reached,

    However, I would like to get this score to restart after each game over by resetting the score/timer to 0 after a game over is reached. I tried putting a reset function in the timer but I’ve had no luck 🙁

    any ideas?

    #15656

    Isak

    Hello Todd,

     

    I would do this by calling an reset function when your player hit a bomb, and in that reset the position of the player, the score and the timer.

     

     

    When i made the FlappyBirds tutorial i just did an if else so when my bird begin contact with an pipe and my property gameState(which was an string) was play i run an function called stopGame(in this function i reset my player position, score and all other stuff required to rerun the game). When i simply had an MouseArea in my main qml what checked if my property gameState was gameOver or play if it was gameOver i simply started a function called startGame.

     

    See my code snippet below:

     

    BoxCollider {
            anchors.fill: upperPipe
            bodyType: Body.Static
            collisionTestingOnlyMode: true
            fixture.onBeginContact: {
                if(scene.gameState == "play") {
                    scene.stopGame()
                }
            }
        }

     

    MouseArea {
            id: touchPlay
    
            enabled: true
            anchors.fill: playButton
            onPressed: {
                if(scene.gameState == "gameOver") {
                    scene.startGame()
                    player.push()
                } else if(scene.gameState == "play") {
                    player.push()
                }
            }
        }

     

     

    Of course you could improve this like instead of having two functions startGame and stopGame have one and in that function check if it should start or stop/reset the game:)

     

    This worked for me:)

     

    #15658

    Günther
    Felgo Team

    Hi Todd!

    As Isak mentioned, you can easily add a custom function to reset the game and call it when e.g. the player hits a bomb or when he clicks a “Start Game” button.
    This is also the preferred solution for bigger projects where resetting your game might require some more logic to your relevant game components. You can put all of the required clean-up for resetting the game into this function then.

    If it is just about the Timer and the score, another option would be to always reset the score to 0 when the timer starts running anew. You can do this by listening to changes of the running property, for example like this:

    Timer {
      running: gameScene.gameIsRunning // this property binding stops or starts the Timer when gameScene.gameIsRunning is set to false or true
    
      // listen to changes of the running property
      onRunningChanged: {
        if(running) {
          // timer changed from running = false to running = true, reset score
          score = 0
        }
      }
    }

     

    Best,
    Günther

    #15662

    Todd

    Awesome guys thanks!

Viewing 8 posts - 1 through 8 (of 8 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded