Sprite collisions and variables question

I have a student who is creating his own side scroller game in Lesson 21 of Unit 3 in CS discoveries. He has several sprites interacting but we are having some issues. We want to place limits on variables increasing so that we can have more complex sprite interactions. Hopefully this all makes sense! Thanks for any help!

Link to the project or level: [replace with link to the curriculum or get the “Share” link to a project]

What I expect to happen: The alien sprite should collect sword sprite and gem sprite and avoid the germ sprite. We’ve made a swordscore for everytime the alien is touching the sword. After the swordscore is >5 the sword is “placed” in the alien’s hand. Now the sword can “kill” the germ sprite before it hits the alien. We want the germ to reduce the sword score variable so after it goes below 5, the alien loses the sword and the sword goes back to looping on the screen.

What actually happens: Instead the alien collects the sword when the swordscore is > 5, but because their x and y are matching the swordscore variable continues to increase, making it so the alien in invincible, rather than having a temporary shield.

What I’ve tried: we are trying to find limits on variable increases, but unsure. Essentially we want the swordscore variable to increase as the alien collects it, but once the alien “has it” the swordscore variable should stop increasing.

Hi @bleowolf,

I am not able to view the project. Sharing has not been enabled for this student. You can adjust this in class settings.

In thinking about the project, here are some thoughts:

If the swordscore is < 5, points are earned for both the gem and sword sprites and lost with the germ sprite. The germ, gem and sword loop through the game as collectibles (I assume).

If the swordscore is > 5, the sword sprite changes position (to the aliens hands) and points should no longer be earned for the sword sprite, the germ sprite can now be killed and the gem continues to loop and score continues to increase if you collide with the gem sprite.

It should be workable by controlling each of the items listed above in the conditional statement for the swordscore. For example, if movement is set outside the swordscore, move the movement into the condition that tests if swordscore is < 5 and is in the aliens hands if swordscore is > 5, etc.

Perhaps all of this is already in place and there is some other logic that I am not thinking of. If so, please share the project and we’ll take a look at the code.

~Michelle

Hi Michelle,
Thanks for taking the time to reply. I changed the sharing settings, so if you want to take a look, you should now be able to.

The student made a few other changes since I posted this, adding some extras to the sprite after it collects the sword and giving it more moving controls .

Now, you can move the sprite to the right side of the screen, and if the germ hits the sword frequently enough, it causes the alien sprite to “lose” the sword as intended. But we need this “hack” to do so. The sword then resumes floating through the air to be collected.

I’m sure it will all make more sense once you can see the code.

thanks again,
Brendan

The game is great and the logic is sound. To solve the final issues, in my mind, everything should fall under the if statement that dictates what it does. I remixed here with the pseudocode I mentioned above which does mean nested if statements (the student had a few already). I made both swordscore and score visible. I think the remix solves the issues but if not, hopefully makes it more clear how to more forward.

Good luck!
Michelle