Re-MakeCode Py Hunter: Collisions and Game Play

Most of the events in the game come down to one thing -- collisions. When a freeze ray hits and enemy, the play bumps into a bystander car, or runs into a tree, all of these things are collisions.

MakeCode Arcade determines when collisions occur by using the sprite overlap blocks. We can set them to activate when all the different types of collisions happen, and then run other blocks depending on the pairings. For example, here's a simple one: when a player car sprite overlaps an innocent bystander car sprite, we'll create a bump effect by moving the player over six pixels in one direction and the bystander 10 pixels in the other direction.

Don't Bump Innocent Cars!

Here the sprite of kind Player overlaps otherSprite of kind Bystander.

We'll play a short tone to indicate the contact.

Then, we check to see if the player car's x position is less than the bystander car's x position. If so, this means the player is to the left of the bystander car, so we'll move the bystander (otherSprite) 10 pixels on x and the player (sprite) -6 pixels on x.

If the player's x position is greater than that of the bystander car's, then the player is to the right of the bystander and we send them in the other directions.

Finally, we penalize the player by lowering their score by -100 points.

Enemies Bump Bystanders

Here you can see a similar collision setup for when Bystander sprites overlap Enemy sprites. In this case, only the bystander car is moved to the side. This makes the bad guys seem even badder.

Player Slams into Enemy

When the player bumps into an enemy we use a similar setup as before. However we also do the following:

  • add 50 points to the player's score
  • change the CarHealth by -3 points
  • shake the camera for hard hitting impact!

Hitting Hazards

Another type of collision to account for are the trees and oil slicks. These are similar to the car-on-car sprite overlaps.

Projectile Hits

When the freeze rays or smoke screens hit an enemy car or bystander car, we'll have them run off to the side of the screen.

If it's an enemy car, the player will gain 300 points; if it's an innocent bystander car, they'll loose 300 points.

Here's how this works:

  • check for sprite of kind Projectile overlaps otherSprite of kind Enemy (the Bystander loop is similar, but we'll just go over the details of Enemy)
  • Play a tone
  • if sprite = freezeray then destroy the freezeray sprite, and start a cool radial effect on the otherSprite
  • change CarHealth by 2 to give the player some health back

Run off the road

Next, we check if the Enemy sprite is on the left or right half of the screen by using if otherSprite x > screen width / 2

The enemy is then launch off to the appropriate side at the speed of the scene with set otherSprite velocity to vx 60 vy sceneSpeed

Since the enemyA car already has a regularly updating loop using vx to steer it toward the player car, we need to flip the enemyAShot variable to 1, which prevents the steering from activating, allowing it to careen off the road.

This same process happens with the smoke sprite type, but using the warm radial effect instead.

Custom Health Meter

We'll get pretty fancy with the user interface (UI) here and create our own custom health meter. The player starts with 100 points of health, each bump with an enemy car or tree removes a few points, and each successful hit of an enemy adds back a few points.

In order to display this visually, we'll use the custom meter graphic and a number of if...if else logic tests -- as the health reaches each 10 point threshold we'll display an associated version of the graphic.

Game Over

When the CarHealth value reaches 0 it's game over!

Now, you can play the game in your browser, upload it to the PyGamer or PyBadge, or even start making your own modifications to the game!

This guide was first published on Jun 11, 2019. It was last updated on Jun 11, 2019. This page (Re-MakeCode Py Hunter: Collisions and Game Play) was last updated on Sep 10, 2019.