Falling Stars

Adding Some Competition

Your game works and now you can collect points, get special powers from power-ups, and lose. We’re getting somewhere! Maybe it’d be fun to add some competition though — what about including a character that moves around a little, but that you’re not supposed to touch? This will be similar to enemies in the traditional platform games like Super Mario that we’re inspired by here.

First, pick a sprite to add as your enemy. Because our player character is a cat, I chose a dog. There are lots of other sprites you could add though. I also renamed the sprite Enemy, just to make things clearer for me.

Resize the sprite to the right size, and place it somewhere appropriate to start. Here’s what mine looks like:

The dog enemy sprite

Write the easiest code first: set up its block for reacting to the game over message to make the enemy disappear when the player loses the game.

whenIreceivegame overhide

Now you need to write the code for what the enemy does. Use my code here, but consider adding extra bits! (What if they can teleport around to different platforms? What if there’s a power-up that makes them move faster, or slower?)

whenclickedshowsetenemy-move-stepsto5setrotationstyleleft-rightgotox:-25y:-9forevermoveenemy-move-stepsstepsifnottouchingPlatforms?thensetenemy-move-stepstoenemy-move-steps*-1

Note: if you just drag the go to block into the sprite panel and don’t change the x and y values, they’ll be the values for the current location of the Enemy sprite!

The code in the if...then block will make the sprite turn around when they get to the end of the platform!

The next thing you’ll need is for the player to lose a life when their Player Character sprite touches the Enemy sprite. Also, you need to make sure the sprites stop touching really quickly, since otherwise the code that checks for touching will keep running and the player will keep losing lives.

Here’s how I did it, but you can try to improve on this code! I modified the Player Character sprite’s main block. Add the new code before the if block that checks if you’re out of lives.

whenclickedreset-gameforevermain-physicsifyposition<-179thenhidereset-characterchangelivesby-1wait0.05secondsshowiftouchingEnemy?thenhidegotox:-187y:42changelivesby-1wait0.5secondsshowiflives<1thenlose

The new code hides the Player Character sprite, moves it back to its starting position, reduces the lives variable by 1, and after half a second makes the sprite re-appear.