We have a complete, if dull and far too easy, game. Now we’ve the basics set up, let’s make it harder – and hopefully a bit more playable as a result!
Part 6: Die Hard
There are a number of ways we can make the game harder, but the simplest one is just to make the enemy faster. We can do this just by changing the value of d from 0.05 to a bigger number. Instead of that, though, why not slowly increment d so that the enemy gets faster and faster the more time the player has spent alive?
We can do this with a counter that increments every loop of the game logic. When it hits a certain threshold, increment d and reset the counter. Let’s call this series of counting “ticks”, and after 200 ticks (so 200 loops of the game logic – essentially 200 frames) d increases. We’ll also choose that d will increase by 0.05 each time.
For this, we need three new variables: ticks (the tick counter), tickstrigger (the number at which ticks triggers a speed increase), and dinc (how much d increased by each time).
We’ll put these in _init():
ticks = 0 tickstrigger = 200 dinc = 0.05
Now we need a simple routine to increment ticks, check if ticks has spilled over the tickstrigger, and if so, increment d by dinc. We can put that in _update(), after the rest of the game logic:
ticks += 1 if ticks > tickstrigger then d += dinc ticks = 0 end
Now, you’ll notice that ticks increases by one each loop, as does score, so you can watch the score counter on the screen increase and at 200, 400, 600 and every other multiple of 200, the enemy will speed up.
That’s made the game somewhat harder. Of course, eventually the enemy will be faster than the player and there’ll be no way to continue playing, but we can deal with that another time.
Our code so far, then:
function _init() -- player start location x = 64 y = 64 -- enemy start location ex = 120 ey = 120 -- enemy "speed" d = 0.05 -- enemy ticks ticks = 0 tickstrigger = 200 dinc = 0.05 -- score score = 0 end function _update() -- check for collision if checkcol(x,y,ex,ey) then stop() end --[[ check button presses and screen edge btn(0) is left btn(1) is right btn(2) is up btn(3) is down ]] if btn(0) and x > 0 then x -= 1 end if btn(1) and x < 120 then x += 1 end if btn(2) and y > 0 then y -= 1 end if btn(3) and y < 120 then y += 1 end -- move enemy if x < ex then ex -= d end if x > ex then ex += d end if y < ey then ey -= d end if y > ey then ey += d end -- increment score score += 1 -- if ticks are triggered, make enemy faster ticks += 1 if ticks > tickstrigger then d += dinc ticks = 0 end end function _draw() cls(2) -- clear the screen to mauve print(score,100,0,7) -- print score spr(1,x,y) -- draw sprite 1 at x, y spr(2,ex,ey) -- draw sprite 2 (enemy) at ex, ey end function checkcol(ax,ay,bx,by) if bx+8>ax and bx<ax+8 and by+8>ay and by<ay+8 then return true else return false end end
Next time, we’ll make the game harder in a different way by creating multiple enemies, and to do that we need to delete and rewrite half our code. It’ll be worth it!
Pingback: How to make a PICO-8 game: Part 7 - deKay's Blog