How to give the player an extra life?
Blitz3D Forums/Blitz3D Beginners Area/How to give the player an extra life?
| ||
So I’m pretty new to blitz and programming in general but I can't seem to figure out how to do it. When my player’s score reaches 25,000 I want my player to gain an extra life, but when ever I try to code it, it ends up adding 1 to it every time it loops through. The code that I’m adding: If score >= 25000 lives = lives + 1 EndIf I understand why it’s adding one, I just can't find away to only add it once. Its probably really simple =) Any help would be appreciated |
| ||
Check the score every time it's increased to see if it qualifies for a bonus life. If the code above is in the main loop, it's being looked at every single loop...as long as your score is 25000 or higher, it's going to keep adding 1 life every loop. Try using a function and calling that function only when the score is increased. Here's what I mean...Global EnemyLife = 100 Global PlayerScore = 0 Global PlayerLives = 3 ; Main Loop If MyShotHitEnemy = True Then EnemyLife = EnemyLife - 10 If EnemyLife = 0 ; Enemy is Dead PlayerScore = PlayerScore + 100 ; Increase the Player's Score CheckScore() ; Run the CheckScore Function EnemyLife = 100 ; Reset the Enemy's life (or delete this enemy and create a new one) so this doesn't keep looping EndIf ; End Main Loop Function CheckScore() If PlayerScore = 25000 Then PlayerLives = PlayerLives + 1 If PlayerScore = 50000 Then PlayerLives = PlayerLives + 1 If PlayerScore = 100000 Then PlayerLives = PlayerLives + 1 End Function |
| ||
I'd suggest not doing what Stayne suggests as he replaces one error with another. His won't award an extra life unless your score stops dead on 25,000. If it goes from 24,999 to 25,001 then you don't get an extra life. It works fine in his example because he hardcodes the score so that it only ever increases by 100, but if you just copy it into your game and go, you may not immediately notice the problem until you start changing the scores. Rather I would have a function which awards points: Function AwardPoints(Pts%) If PlayerScore<25000 If PlayerScore+Pts>=25000 PlayerLives=PlayerLives+1 End If End If PlayerScore=PlayerScore+Pts End Function |
| ||
or you can set a highscore and increase it. If score>=highscore then lives=lives+1:highscore=higscore+25000 (how do you write code in this green letters with black background?) |
| ||
Yep, if you want a new life on 50,000 and 75,000, etc then Dimas' way is probably better. ( You get the code box by surrounding your code with {code} and {/code} but using square braces [] instead of curly ones {} ) |
| ||
Thanks for all of your help, thats exactly what I was looking for Dimas. |
| ||
I know it's late but... f score >= 25000 lives = lives + 1 score=0 EndIf |
| ||
Gabriel I was just trying to show him a simple example of how to get out of the main loop and keep his score from repeatedly raising. Gotta give 'em some room for study else they'll never learn. |
| ||
;Extra lives definition at start of your program global extralife_at[5] extralife_at[0] = 10000 extralife_at[1] = 25000 extralife_at[2] = 50000 extralife_at[3] = 100000 extralife_at[4] = 200000 global extralife_id extralife_id = 0 ;Reset to the first extra life score ;In your game if extralife_id < 5 then ;Avoid an array overflow if score >= extralife_at[extralife_id] then lives = lives + 1 extralife_id = extralife_id + 1 ;Point to the next extra life score endif endif |
| ||
Can't you just have? targetscore=25000 If score > targetscore Lives= lives +1 targetscore=targetscore + 25000 endif |
| ||
I agree with Pete. That's the method I use - all the rest are just overkill. |
| ||
It's also the method Dimas provided 21 hours ago. |
| ||
Yes but Dima's won't work ;) highscore=higscore+25000 |
| ||
Depends on if you want the lives to be given at those set intervals ;o) |
| ||
Yes but Dima's won't work ;) Ah see, I knew there was a simple reason why people were continuing to post solutions ( two of which are completely broken ) for a problem which had been solved. For some reason, I jumped to the conclusion that the typo hadn't been an issue when the original poster thanked Dimas and said it was exactly what he was looking for. |
| ||
There's no problem to have a lot of different methods for doing this kind of things. It will help most user searching the forum for extra lives solutions :) |
| ||
I missed Dima's post because people were posting much more complex ways of doing it i thought no one would have posted the simple version. but theres no point in making it any more complex. |
| ||
Can't you just have? targetscore=25000 If score > targetscore Lives= lives +1 targetscore=targetscore + 25000 endif No that's far too simple! As a British citizen I won't stand for such a solution! It must be at least 500 times more complicated and involve charging the tax payer in some way. /sarcasm Actually that's a very nice solution. |