Okay, I’m going to caveat this by saying I’m going to try and teach you to be a programmer, rather than doing it myself. So please take it with meaning that I’m wanting to help you.
90% of programming is problem solving, and trying to figure out things. Your code is clean, and there isn’t much of it, and it wouldn’t be much work to rewrite it. So to take your last question:-
“What’s the easiest way to do that without rewriting or rearranging my existing code any more than strictly necessary?”
It won’t take too much work to rewrite it from scratch, or rearrange it. Also sometimes the easiest way to fix a problem is to rewrite or rearrange code - it’s a common thing for code :)
With that said, I’d break down your task into smaller tasks:-
- Add a Yellow Apple
- Make it appear randomly about a quarter of the time
- Falls twice as fast
- Earns 5 points if collected
I’ll talk you through the first one. You should have enough commands in your code to do the rest :)
To add a yellow apple, the way I’d approach it is to change a lot of the constants into variables, that way you can change variables easily without having to refactor the code. You can add a variable for the sprite number.
So for the spr command, instead of referencing individual sprites, you can reference variables. I’ve looked at your reset_coin function and added some code so that makes the apple randomly split between the yellow and the red apples.
function _init()
x=63
y=85
score=1
reset_coin()
end
function _update()
if btn(⬅️) then
x=x-1
elseif btn(➡️) then
x=x+1
end
ay=ay+1
--catch the coin
if abs(x-ax)<4 and abs(y-ay)<4 then
reset_coin()
score=score+1
elseif abs(ay)>120 then
reset_coin()
end
end
function _draw()
cls()
map(1)
spr(1,x,y)
spr(as,ax,ay) -- display the variable apple sprite
print (score)
end
function reset_coin()
local randomapple -- "local" is new, and is a variable you can only use in this function. it helps keep code clean
ax= rnd(120)
ay=-8
randomapple = rnd(2) -- pick a random number between 0 and 2
if randomapple>1 then
as=3
else
as=2
end
end
Now with that done, I think you should be able to figure out some of the other elements. Begin with task 2. How could you change it so that instead of appearing 1/2 of the time, the yellow apple appears 1/4 of the time? Lines 40-47 should help you.
Hope that is a start for you!