#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../webpage-assets/ce.css" />
#+HTML_HEAD: <link rel="icon" type="image/png" href="http://rachel.likespizza.com/web-assets/images/favicon-moose.png">

# ---------------------------------------------------------------- #

* Lesson 12: Object collisions

** Continuing on from [[file:lesson-npc.html][Lesson 11]]

We are going to build on top of the Lesson 11 code, but you still
might want to save it as a new program:

#+BEGIN_SRC terminal
  > save NEWNAME.lua
  cart NEWNAME.lua saved!
#+END_SRC

** Code for the score

In the SETUP portion let's add two variables: a player score and a
shark score. We are going to set them to 0 to begin with.

#+BEGIN_SRC lua
pscore = 0            -- Player score
sscore = 0            -- Shark  score
#+END_SRC

Let's also print the scores to the screen. We could either keep it
at a fixed location like at the top of the screen...:

#+BEGIN_SRC lua
  -- Draw
  cls(11)
  spr(0,px,py)          -- Player
  spr(1,bx,by)          -- Bubble
  spr(2,sx,sy)          -- Shark
  print(pscore,5,5)
  print(sscore,220,5)
#+END_SRC

[[file:images/lesson-collision-coordinates.png]]

Or put the score above the player and shark, so it follows them
around as they move:

#+BEGIN_SRC lua
  -- Draw
  cls(11)
  spr(0,px,py)          -- Player
  spr(1,bx,by)          -- Bubble
  spr(2,sx,sy)          -- Shark
  print(pscore,px,py-8)
  print(sscore,sx,sy-8)
#+END_SRC

[[file:images/lesson-collision-coordinates2.gif]]


** Distance formula

Now let's bring in the Distance function again. Remember that this
has to go at the very top of the program before everything else!

#+BEGIN_SRC lua
function getDistance( x1, y1, x2, y2 )
  return math.sqrt( ( x2 - x1 )^2 + ( y2 - y1 )^2 )
end
#+END_SRC

Now we can check to see if the PLAYER is touching the BUBBLE (then
player gets points), or the SHARK is touching the BUBBLE (then shark
gets points).

I'm going to implement the code for the player getting the bubble,
and you can do the shark's code.

First I need the distance... (I am putting this before the Draw
code!)

#+BEGIN_SRC lua
  -- Player vs. Bubble
  dist = getDistance(px,py,bx,by)
#+END_SRC

Then I can compare the distance to see if it counts as the player
catching the bubble...

#+BEGIN_SRC lua
  if dist <= 8 then
   -- Player caught bubble!
  end
#+END_SRC

I will want to add 1 to the score:

#+BEGIN_SRC lua
  if dist <= 8 then
   -- Player caught bubble!
   pscore = pscore + 1
  end
#+END_SRC

If I leave the logic like this, then the player will get lots of
points by just touching the bubble over time.

[[file:images/lesson-collision-animation1.gif]]

But maybe we only want one point at a time. In this case, I will
move the bubble back to the bottom of the screen in a random
location:

#+BEGIN_SRC lua
  if dist <= 8 then
   -- Player caught bubble!
   pscore = pscore + 1
   bx = math.random(0,230)
   by = 136
  end
#+END_SRC

Now the player only gets one point when it gets the bubble:

[[file:images/lesson-collision-animation2.gif]]

** Challenges

#+BEGIN_EXPORT html
<div class="attention-box cf">
  <h3> CHALLENGES! </h3>
  <p> Can you use what you've learned so far to expand the game?
      Here are some ideas:</p>
  <ol>
    <li> Add code so that the shark can also collect the bubble
         and gain points! </li>
    <li> Play a sound effect when the bubble is collected </li>
  </ol>
</div>
#+END_EXPORT

** Testing

[[file:images/lesson-npc-animation.gif]]

Now the shark moves around on its own chasing the bubble. Perhaps we
could make a game where both objects are chasing the bubble...?
Hmmm.......

** Save your work

Make sure to press the *CTRL+S* key, or press *ESC* to go to the
prompt and type =save= to save your work! If you want to access the
folder with your code, type =folder=.

If you have a USB flash drive, make sure to copy your file over so
you don't lose it when the computer resets!

** Back to the course page!

[[../index.org][⬅️ Once you're all done, go back to the course homepage!]]
