Hey! I am creating a new adventure game right now, and I have an energy bar (Top left corner) that goes down over time, and I can refill it by cranking, but I have no idea how to make the energy bar go down because I made every single frame of the energy bar, even though I have already watched SquidGod's Make an Adventure game. Also the energy bar goes down by 1 every second.
In your draw code, you will want to check if a second has passed. To do this, store the time or frame number as a variable and check if the current time or frame has gone up 1 second or 20 frames (I think the games run at 20 frames per second, but I haven't actually checked.) If it has, update the variable that stores the time or frame and then update a variable that represents your energy level and then run the code for updating your visuals.
new_second = datetime.second // save the current second as a variable; you don't want the time to change while executing this code to affect the code
if new_second != last_second then // we use != here instead of > because sometimes it will be flipping from 59 to 0
last_second = new_second // update the variable
current_energy -= 1 // lower the energy
if current_energy<0 then // I assume you don't want it to go negative
current_energy = 0 // if you want something to happen when you're out of energy, you could put that after this line
end
end // later down we will update the visuals of the bar
if event.frame>last_frame then // we don't need to save the frame number as a variable because it will not change as the game is processing
last_frame = event.frame
curent_energy -= 1
if current_energy <0 then
current_energy = 0
end
end // later down we will update the visuals of the bar
now you need to deal with updating the visuals of the bar. you should probably not have this in the if-block from above as you will want to update it when other things change your energy as well. I see you made different tiles for different energy sizes. You can change a tile by using code like this:
tell 10,2 to
swap "energy3"
end
Make the numbers (10 and 2 in this example) to match the coordinates of the tile you want to change and make the string (energy3 in this example) to match the tile name you want it to become (you can also use the tile number there if you know it.) I really don't recommend this though. Instead, you should just put the tiles for the empty bar down and then use the "fill" command to draw out the current bar Playdate PulpScript . basically, you make w to be the current energy. This will make a rectangle though so it will not have the little curved end like you have in your tile. To make that happen you can either: draw 4 white 1x1 dots in the corners after you draw the bar, or draw the bar twice where one is 2px shorter, one two the right and the full height while the other is full length, one pixel down and 2px thinner. I would just draw the 4 white 1x1 dots.
Thank you for your help, and I have added the "Energy" feature! I have another question, though. Basically, I want to have a way to "recharge" the battery when I turn the crank. I want it so that every 360 degrees I crank, I regain 1 energy part. I just want to know how I would make the crank part, and I am not worried about drawing out the regaining energy part.
You can access the current crank position with event.aa (aa is short for "absolute angle"). It will give you a number from 0-359. You can also use event.ra (relative angle) which tells you how much the crank has moved since the last frame.
You can't actually just write a "when it makes a loop" block of code, you're going to have to determine it. You also need to answer a few questions: must they complete a crank in a certain amount of time? does it matter which direction they crank it? is energy going up each time you make a full rotation from the top position or some other specific question or does it need to be a full rotation from the last time turned the cranking direction around or something else? You'll need to answer these questions and then make a flowchart (perhaps just a mental flowchart) of how you could determine that with just the event.aa and event.ra inputs.
Thank you. I will look into that.