More Rocks

Today, I make my Ferb game more challenging by placing more rocks.
Last time, I added an explosion whenever the meteors crashed into the ground. Today, I want to make the game more challenging by leaving debris (another rock) at the point of impact. That is, the more impacts, the more rocks. But, I want to make sure the landscape isn't completely covered by rocks or Ferb won't be able to get around. I also want to make sure the rocks aren't placed so near to one another that Ferb can't jump over them. So the plan is:

  1. - place more rocks (this means creating an array to track the rocks and changing existing code to use the array)
  2. - put an upper limit to the number of rocks placed.
  3. - space the rocks (which means spacing the meteors)


I'll tackle these problems one at a time, and do point number 1 today.

Create an Array of Rocks



Rocks are objects, so I can just create another rock object at the point of impact. I also want to make sure I create the rock after the explosion completes its animation. Before I can do any of that, I need an array to keep track of all the rocks. First, I want to refactor my initrocks function which creates a rock, by extracting all the code into a new function.

Screen Shot 2021-04-14 at 8.31.22 AM

Becomes:

Screen Shot 2021-04-14 at 8.34.58 AM

I also need to pass in the x coordinate of the rock since I want to position it at the point of impact.

Screen Shot 2021-04-14 at 8.36.53 AM

Next I want to add the rock to the therocks array. I could do this in either function, but it feels better in initrocks, but would make the code simpler in createarock. I won't go into why, but I'll put it in initrocks where it belongs.


Screen Shot 2021-04-14 at 9.01.03 AM

I now return a rock object from createarock. I insert the rock into the array on line 288. One interesting thing is that in Lua, I would use the command:

table.insert(therocks, rock)

But PICO-8 doesn't seem to support that command and uses add instead.

I now need to change all of the code that deals with rock to use the array instead.

Screen Shot 2021-04-14 at 8.48.31 AM

Becomes:

Screen Shot 2021-04-14 at 8.51.12 AM

Also, unlike Lua, PICO-8 has a simplified syntax for looping over an array (table). I should add a return after line 174, but this is good enough for now.

Screen Shot 2021-04-14 at 8.53.41 AM

beomes:

Screen Shot 2021-04-14 at 8.54.58 AM

Again, I'm just looping through each of the rocks in the array.

Just to make sure everything is as it was before all of these changes, I run the program and see it works as expected.

Creating New Rocks



Now, I need to create a new rock at the point of impact. There are again two places I can do this; either where I reset the meteor after the collision or where I draw the explosion (and call resetmeteor).
.


Screen Shot 2021-04-14 at 9.05.59 AM


Screen Shot 2021-04-14 at 9.04.12 AM

Both options have a code-stink about them (It feels wrong to put the code in either.). That tells me I should refactor drawexplosion. I'll do a minor refactor by moving line 335 into its own function and handle the rock there.

Screen Shot 2021-04-14 at 9.25.05 AM

This feels a bit better. Lines 325-326 creates a new rock at the position of the meteor and adds the new rock to the array. These two lines look similar to the two I added in initrocks. Let's refactor.

Screen Shot 2021-04-14 at 9.30.14 AM

Screen Shot 2021-04-14 at 9.30.45 AM


The new function at line 291 simplifies the code a bit. I also now create the initial rock at a random location. Let's run it and see what happens.



Screen Shot 2021-04-14 at 9.32.24 AM


That looks good, apart from the problems I need to address in the last two points of:

  1. DONE - place more rocks (this means creating an array to track the rocks and changing existing code to use the array)
  2. - put an upper limit to the number of rocks placed.
  3. - space the rocks (which means spacing the meteors)

I'll tackle them next time. Again, here is the cart:

ferb.p8

This site does not track your information.