instagram

Students use the distance formula and their data structures to determine when two or more characters in their games have collided. They extend their update handlers to generate a new structure that represents the game after a collision has occurred.

Product Outcomes

  • Students add collision-detection to their games

Materials

🔗Collision Detection 30 minutes

Overview

Students implement a simple Boolean-producing function, which composes with the distance function they implemented.

Launch

Once we know how to calculate the distance between two points, what do we do with it? How does that help us implement collision in our games?

How close should your danger and your player be, before they hit each other?

  • Turn to Word Problem: is-collision you’ll find the Word Problem for is-collision.

  • Fill in the Contract, two examples, and then write the code. Remember: you WILL need to make use of the distance function you just wrote!

  • When you’re done, type it into your game, underneath distance.

Now that you have a function which will check whether two things are colliding, you can use it in your game! For extra practice, You can also implement collision detection in this Ninja Cat Starter File.

This is the program we’ll be altering for this lesson. In Ninja Cat, when the cat collides with the dog, we want to put the dog offscreen so that it can come back to attack again.

Investigate

Out of the major functions in the game (next-state-tick, draw-state, or next-state-key), which do you think you’ll need to edit to handle collisions, changing the GameState when two characters collide?

We’ll need to make some more if branches for next-state-tick.

  • How could you check whether the cat and dog are colliding? Have you written a function to check that?

  • What do the inputs need to be?

  • How do you get the playery out of the GameState? playerx?

  • How do you get the dangerx out of the GameState? dangery?

if is-collision(
  g.playerx,
  g.playery,
  g.dangerx,
  g.dangery):   ...result...

Remember that next-state-tick produces a GameState, so what function should come first in our result?

if is-collision(
  g.playerx,
  g.playery,
  g.dangerx,
  g.dangery):
game(
  ...playerx...,
  ...playery...,
  ...dangerx...,
  ...dangery...,
  ...dangerspeed...
  ...targetx...
  ...targety...
  ...targetspeed...)

  • What should happen when the cat and dog collide?

  • Can you think of a number that puts the dog off the screen on the left side?

  • What about the dog’s y-coordinate?

We could choose a number and always place it at the same y-coordinate each time, but then the game would be really easy! To make it more challenging, we’d like the dog to appear at a random y-coordinate each time it collides with the cat. Thankfully, Pyret has a function which produces a random number between zero and its input:

# num-random :: Number -> Number

if is-collision(
  g.playerx,
  g.playery,
  g.dangerx,
  g.dangery):
game(
  g.playerx,
  200,
  num-random(480),
  0,
  0,
  g.targetx,
  g.targety,
  g.targetspeed)

Collision detection must be part of the next-state-tick function because the game should be checking for a collision each time the GameState is updated, on every tick.

Students may assume that draw-state should handle collision detection, but point out that the Range of draw-state is an Image, and their function must return a new GameState in order to set the locations of the characters after a collision.

Once you’ve finished, write another branch to check whether the player and the target have collided. Challenges:

  • Change your first condition so that the danger gets reset only when the player and danger collide AND the cat is jumping. (What must be true about the player’s y-coordinate for it to be jumping?)

  • Add another condition to check whether the player has collided with the danger while the player is on the ground. This could be a single expression within next-state-tick, or you can write a helper function called game-over to do this work, and use it in other functions as well (maybe the GameState is drawn differently once the game is over.)

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). CCbadge Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.