Discuss Scratch

blackheel
New Scratcher
7 posts

Lose Life

I'm new to Scratch. Trying to build a “space invaders” like game where a cat tries to shoot falling mice before they hit the bottom of the screen. However unable to come up with a code where once the mouse hits the bottom of the screen, the cat loses a life. Below is the code I'm using to try to make this happen once mouse touches bottom of the screen. Where am I going wrong?



Last edited by blackheel (Oct. 11, 2018 02:26:47)

blackheel
New Scratcher
7 posts

Lose Life

when green flag clicked
set [Lives] to [0]
forever
if <touching edge?> then
wait (1) secs
change [Lives] by (-1)
if <Lives = 0> then
stop [all]
end
end
end
deck26
Scratcher
1000+ posts

Lose Life

That code is going to knock multiple lives off your cat once the mouse gets to the bottom. Sharing what you have would make it easier to help.
blackheel
New Scratcher
7 posts

Lose Life

That's what I don't understand. Why would it knock multiple lives off? But here's what I have so far https://scratch.mit.edu/projects/251316418/#editor
deck26
Scratcher
1000+ posts

Lose Life

blackheel wrote:

That's what I don't understand. Why would it knock multiple lives off? But here's what I have so far https://scratch.mit.edu/projects/251316418/#editor
Because it's a forever loop and you don't seem to move the sprite away from the edge so it will remove a life once a second.

Looking at your project you don't actually hit the edge at the moment but if you did the glide would still be running at the same time as the forever loop so if you go from not touching the edge to touching it you'll continue to be touching it as long as the glide continues.

One option would be to do something like

forever
wait until <touching [edge v]>
change [Lives v] by [-1]
if <(Lives) = [0]> then
stop [all v]
end
wait until <not <touching [edge v]>>
end

Last edited by deck26 (Oct. 22, 2018 16:06:35)

blackheel
New Scratcher
7 posts

Lose Life

deck26 wrote:

blackheel wrote:

That's what I don't understand. Why would it knock multiple lives off? But here's what I have so far https://scratch.mit.edu/projects/251316418/#editor
Because it's a forever loop and you don't seem to move the sprite away from the edge so it will remove a life once a second.

Looking at your project you don't actually hit the edge at the moment but if you did the glide would still be running at the same time as the forever loop so if you go from not touching the edge to touching it you'll continue to be touching it as long as the glide continues.

One option would be to do something like

forever
wait until <touching [edge v]>
change [Lives v] by [-1]
if <(Lives) = [0]> then
stop [all v]
end
wait until <not <touching [edge v]>>
end

My goodness it worked. Original code worked I just applied your advice to have the mouse sprite reach the edge. Thanks!
blackheel
New Scratcher
7 posts

Lose Life

Nevermind now it takes off a life even if the mouse doesn't touch the bottom of the screen
deck26
Scratcher
1000+ posts

Lose Life

blackheel wrote:

Nevermind now it takes off a life even if the mouse doesn't touch the bottom of the screen
You don't currently have the code I suggested in the project you linked to earlier.

Also don't switch backdrop in a forever loop - it's very inefficient. Set it to castle2 and wait until Lives = 0 when you set it to castle4.
blackheel
New Scratcher
7 posts

Lose Life

deck26 wrote:

blackheel wrote:

Nevermind now it takes off a life even if the mouse doesn't touch the bottom of the screen
You don't currently have the code I suggested in the project you linked to earlier.

Also don't switch backdrop in a forever loop - it's very inefficient. Set it to castle2 and wait until Lives = 0 when you set it to castle4.

No that doesn't help either. For some reason it causes the cat to lose a life slower but even when the cat is not touching the bottom. My code is a mess lol
blackheel
New Scratcher
7 posts

Lose Life

Also taking the forever out of the code to switch to castle4 when the lives equals zero does nothing but remove the Game Over backdrop once my cat runs out of lives so I added the forever back into the code
deck26
Scratcher
1000+ posts

Lose Life

blackheel wrote:

Also taking the forever out of the code to switch to castle4 when the lives equals zero does nothing but remove the Game Over backdrop once my cat runs out of lives so I added the forever back into the code
You don't need it and it's a bad habit to kick as soon as you can.

All you need is

set [lives v] to [3]
switch backdrop to [castle2 v]
wait until <(lives) = [0]>
switch backdrop to [castel4 v]
But since you stop all immediately lives=0 the above script may not run. However that actually means you don't even need a separate script to look for lives=0 - just switch the backdrop immediately before the stop all and set the initial backdrop in one of the other green flag scripts.

Last edited by deck26 (Oct. 25, 2018 17:56:27)

Chestnut33333
New Scratcher
1 post

Lose Life

blackheel wrote:

when green flag clicked
set [Lives] to [0]
forever
if <touching edge?> then
wait (1) secs
change [Lives] by (-1)
if <Lives = 0> then
stop [all]
end
end
end

Powered by DjangoBB