Using the x position of a sprite to change score

Hi!
In this game, a plane shoot bullets to destroy incoming ‘lasers’.
When a laser is hit the score increments up by 1.
I cannot figure out how to get the score to decrement by one when the laser gets past the plane.
Please help. I experimented with tile maps, grid data etc and then just went back to a background image and tried using the x position data, however, it doesn’t run my if statement when laser x < 20.

Great game!

Because you’re creating them as projectiles, the bullet sprites are destroyed when they leave the screen. So, this block might be helpful:

The conditional can test whether `sprite` has left the screen. (Hint: It won’t be when `x < 20`. What is the `x` coordinate of the bullet when it reaches the right side of the screen?)

Also, do you want the player’s score to go negative? If not, then you can create a compound conditional (a “test” with an and or or in it), only deducting a point if the score is positive.

I’ve hidden my code behind a spoiler so that you can try it yourself. If you get stuck, then just click on the image to reveal it.

If you want the score to go negative, then you only need the first part of the conditional.

2 Likes

Thanks for the help! It definitely aided my brain and I rethought the problem and found another two solutions that seemed to work. See below. I like your solution, but don’t understand why the x position is > or equal to screen width. The laser is coming from the right of the screen when the x position should equal the screen width. If the player doesn’t hit and destroy the laser then the x position is <0 as it passes to the left of the screen. Maybe the answer is that x is always between 0-screen width and never = <0? The player position is around x = 23 and that’s why I used x<20 (as in the laser got behind the player).

solution 1: https://makecode.com/_A7EMo5bXg0aH

solution 2: less sophisticated. Just +2 and -1 from the two different on sprite destroy blocks so that the overall effect on the code is +1 when hit or -1 when not hit.

I misread your question - I thought you wanted the score to decrease when a bullet left the screen. You were talking about the lasers, though.

You have the right idea, for sure. This is how I did it:

You also need to change the code in your `on game update` loop where you create the lasers, and change the auto-destroy flag to `ON`.