Update: Some very kind people in the comments took time out of their day to test this code on their Playdates. It's unfortunately too resource intensive, and will tank the frame rate of your game so it's unplayable
~
I made a basic rain effect in Pulp using the draw function. Since it draws raindrops over the screen, there's no need to animate tiles individually to add rain, and you can turn the rain on or off in a room with simple logic.
Player Script
on draw do
if raining==1 then
x = 0
rainY = rainT
while x<200 do
tell event.game to
call "rain{x}"
end
rainY += rainPhase
if rainY>125 then
rainY -= 130
end
fill "black" at x,rainY,1,3
x++
end
rainT += 2
if rainT>125 then
rainT = -5
end
end
end
Game Script
on rain0 do
rainPhase = 101
end
on rain1 do
rainPhase = 54
end
on rain2 do
rainPhase = 63
end
// Keep adding rain{x} events until you get to rain199, one for each x value
It basically works by looping through each x value on screen, drawing a raindrop at a certain y value for each value of x, and then stepping the whole drawing down slightly each frame.
The secret sauce is using the rain{x}
events like an array to reuse the rainPhase
variable during the while loop. Thanks to Shaun Inman for sharing how to do this! I used a random number generator to enter a value for each event.
You can import my version of the code (pdx and json) here:
It's Raining.zip (45.5 KB)
I you're inspired by this effect or use any of my code, I would love it if you left a comment
PS: I don't have an actual Playdate yet; it's possible calling 200 events every frame is really bad for performance! If you have some hardware to actually test my code, I would appreciate it if you let me know how well it does!