make a controllable bubble that floats in all directions, glows in pink when you press a…
- User
make a controllable bubble that floats in all directions, glows in pink when you press a key in pico8
- Assistant
Creating a controllable glowing bubble in PICO-8 involves programming movement and visual effects. Here's a step-by-step guide along with a sample code snippet to help you get started:
Step-by-Step Guide
-
Initialize Bubble Properties: Define the bubble's starting position, color, and glow state.
-
Handle Inputs for Movement: Use the arrow keys to control the bubble's movement in all directions.
-
Handle Input for Glow Effect: Assign a key (e.g., the 'z' key) to toggle the bubble's glow effect.
-
Update Bubble State: Change the bubble's position based on input and update its color if glowing.
-
Render the Bubble: Draw the bubble on the screen, changing its visual appearance when it glows.
Sample Code
-- Bubble properties bubble = { x = 64, y = 64, radius = 8, color = 7, -- Default color (white) glow = false } -- Function to update bubble's position and properties function update_bubble() if btn(0) then bubble.x = bubble.x - 1 end -- Left if btn(1) then bubble.x = bubble.x + 1 end -- Right if btn(2) then bubble.y = bubble.y - 1 end -- Up if btn(3) then bubble.y = bubble.y + 1 end -- Down -- Toggle glow with 'z' key (Key: 5 in PICO-8) if btn(4) then bubble.glow = not bubble.glow end -- Set color based on glow state if bubble.glow then bubble.color = 13 -- Pink else bubble.color = 7 -- White end end -- Function to draw the bubble function draw_bubble() -- Outer glow circle, drawn first if glowing if bubble.glow then circfill(bubble.x, bubble.y, bubble.radius + 2, 13) end -- Main bubble circfill(bubble.x, bubble.y, bubble.radius, bubble.color) end -- Main update function function _update() update_bubble() end -- Main draw function function _draw() cls() -- Clear screen draw_bubble() endExplanation
- Bubble Properties: The
bubbletable holds its position, radius, color, and glow state. - Movement: The
btnfunction checks for arrow key presses to adjust the bubble's position. - Glow Effect: The
btn(4)checks for the 'z' key to toggle the glow. The corresponding color is set to pink (13) when glowing. - Drawing: The bubble, along with an optional glow, is drawn every frame.
Remember, this basic example assumes that you have some familiarity with PICO-8. You can enhance this script by adding boundaries, smoother movement, sound effects, or more complex glowing effects.
-
- User
make the background white
- Assistant
To make the background white in PICO-8, you'll need to draw a white rectangle that covers the entire screen before any other drawing commands are executed. By default, PICO-8 uses an 8-bit color palette where the color index
6represents white.Here's the updated code to set the background to white:
-- Bubble properties bubble = { x = 64, y = 64, radius = 8, color = 7, -- Default color (white) glow = false } -- Function to update bubble's position and properties function update_bubble() if btn(0) then bubble.x = bubble.x - 1 end -- Left if btn(1) then bubble.x = bubble.x + 1 end -- Right if btn(2) then bubble.y = bubble.y - 1 end -- Up if btn(3) then bubble.y = bubble.y + 1 end -- Down -- Toggle glow with 'z' key (Key: 5 in PICO-8) if btn(4) then bubble.glow = not bubble.glow end -- Set color based on glow state if bubble.glow then bubble.color = 13 -- Pink else bubble.color = 7 -- White end end -- Function to draw the bubble function draw_bubble() -- Outer glow circle, drawn first if glowing if bubble.glow then circfill(bubble.x, bubble.y, bubble.radius + 2, 13) end -- Main bubble circfill(bubble.x, bubble.y, bubble.radius, bubble.color) end -- Main update function function _update() update_bubble() end -- Main draw function function _draw() rectfill(0, 0, 127, 127, 6) -- Draw white background (color index 6) draw_bubble() endExplanation
- Setting Background to White: In the
_drawfunction,rectfill(0, 0, 127, 127, 6)is used to draw a rectangle that covers the entire screen (128x128 pixels) using color index6, which corresponds to white in PICO-8's color palette. - Order of Drawing: It's important to draw the background first, so it's effectively overwritten by any subsequent drawing operations like the bubble.
This ensures the entire screen is filled with a white background before you draw the bubble and its glow on top of it.
- Setting Background to White: In the