Skip to content

Creating Interactions

There’s one last thing we need to do to tie this game together. Let’s add code to decide what happens when two different objects hit each other!

Look for the on sprite of kind _ overlaps otherSprite of kind _ block via search or in the Sprites tab:

Snapshot from Makecode

Place it as a new separate block in your workspace. Make sure the first kind is Enemy and the second kind is Player. That means this block will execute when an Enemy (like our asteroids) overlaps a Player (like our spaceship).

Snapshot from Makecode

[!warning] Which sprites do I use? The above block is special, and you should only use sprite or otherSprite in these blocks. Do not use mySprite, myAsteroid, or any sprites created outside of this block.

Next, look for the destroy mySprite block via search or in the Math tab, and place it in the overlaps block we just created.

Snapshot from Makecode Snapshot from Makecode

Then, drag the otherSprite capsule towards mySprite to replace it.

Snapshot from Makecode

Pres the (+) icon, and add any effect you want! We will use fire.

Snapshot from Makecode

Now, if you try press Play ▶, your spaceship will be destroyed when an asteroid hits it!

Snapshot from Makecode

Now that the spaceship can get destroyed by asteroids, it’s getting difficult to survive for long. Let’s make the spaceship shoot lasers to help!

Look for the on A button pressed block via search or in the Controller tab, and place it into your workspace.

Snapshot from Makecode

Then, look for the set mySprite to sprite _ of kind Player block via search or in the Sprites tab, and:

  1. Rename mySprite to myLaser, or anything you prefer, and
  2. Change kind Player to Projectile:
Snapshot from Makecode

Next:

  1. Look for the set mySprite position to x _ y _ block via search or in the Sprites tab, then:
  2. Change mySprite to myLaser (or whichever you used previously), and
  3. Look for the mySprite x block in Sprites
Snapshot from Makecode

In the end, you should have a block that looks like below:

Snapshot from Makecode

Finally, look for the set mySprite velocity to vx _ vy_ block in Sprites. Change mySprite to mylaser and set vy to -100.

This is the final result:

Snapshot from Makecode

Press Play ▶, and press the A button on the controller, or press x on your keyboard. Your spaceship will now shoot lasers!

Snapshot from Makecode

You can shoot lasers, but they don’t do anything right now. Let’s give them the ability to destroy asteroids!

Once again, look for the on sprite of kind _ overlaps otherSprite of kind _ block via search or in the Sprites tab.

This time, set the first kind to Projectile (your laser) and the second kind to Enemy (the asteroids).

Snapshot from Makecode

Then,

  1. Look for the destroy mySprite block and insert it. Change mySprite to otherSprite and optionally choose whatever effect you’d like.
  2. Grab another destroy mySprite block and insert it. Change mySprite to sprite.

This way, both the asteroid and laser disappear when they collide.

Snapshot from Makecode

Press Play ▶, and start shooting asteroids. Now you’ll see the asteroids burn up when the lasers hit them!

Snapshot from Makecode