make a color changing spiral that you can control the movement of in pico8
Assistant
Creating a color-changing spiral in PICO-8 that you can control with input is a fun and creative project. Below is a simple example of how you can implement this using PICO-8. This code will create a spiral on the screen whose colors change over time and which you can rotate using the arrow keys.
User
make the spiral spray blue slime when you move it
Assistant
User
make the spray stick on the walls
Assistant
User
i don't see the walls
Assistant
User
make a green wireframe sphere appear when you move the spiral, make the sphere spin as well
Assistant
lua
-- initialize variables
t = 0
angle = 0
center_x = 64
center_y = 64
radius_step = 1.5
num_segments = 100
function _update()
-- Update time
t += 0.05
-- Control rotation with arrow keys
if btn(0) then -- left
angle -= 0.02
elseif btn(1) then -- right
angle += 0.02
end
end
function _draw()
cls()
for i=1, num_segments do
-- Calculate angle and color for each section of the spiral
local current_angle = angle + (i / num_segments) * 6.283 -- 2*PI for a full circle
local color = 7 + (sin(t + i * 0.1) * 4) -- Cycling through colors
-- Calculate radius
local radius = i * radius_step
-- Calculate position
local x = center_x + cos(current_angle) * radius
local y = center_y + sin(current_angle) * radius
-- Draw point
pset(x, y, flr(color))
end
end
Explanation
Initialization: We start by setting up some global variables. t for time, angle for rotation, and center_x & center_y for the center of the spiral. radius_step defines how tight the spiral is, and num_segments determines the number of points in the spiral.
_update() function: The angle of rotation is adjusted based on arrow key inputs. This allows you to rotate the spiral.
_draw() function:
cls() clears the screen before each frame.
A loop runs num_segments times to draw each point of the spiral.
For each point, the current angle is calculated, and a color is determined by a sine function for smooth color transitions.
Using trigonometry, cos and sin, the position of each point in the spiral is calculated and placed with pset().
This code will render a rotating, color-changing spiral that you can control using the left and right arrow keys to adjust the rotation. You can further customize this by altering parameters such as num_segments and radius_step to achieve different visual effects.
To add a feature where the spiral sprays "blue slime" when you move it using the arrow keys, we can implement a simple particle system that emits blue particles whenever the spiral is rotated. Below is an adjusted version of the original code that includes this feature:
lua
-- initialize variables
t = 0
angle = 0
center_x = 64
center_y = 64
radius_step = 1.5
num_segments = 100
-- particles table
particles = {}
function add_particle(x, y, ang)
local particle = {
x = x,
y = y,
ang = ang,
life = 30,
}
add(particles, particle)
end
function update_particles()
for p in all(particles) do
p.x += cos(p.ang) * 2
p.y += sin(p.ang) * 2
p.life -= 1
if p.life <= 0 then
del(particles, p)
end
end
end
function draw_particles()
for p in all(particles) do
pset(p.x, p.y, 12) -- blue color for particles
end
end
function _update()
-- Update time
t += 0.05
-- Control rotation with arrow keys
local moved = false
if btn(0) then -- left
angle -= 0.02
moved = true
elseif btn(1) then -- right
angle += 0.02
moved = true
end
-- Add particles if moved
if moved then
add_particle(center_x, center_y, angle + 0.25)
add_particle(center_x, center_y, angle - 0.25)
end
-- Update particles
update_particles()
end
function _draw()
cls()
-- Draw the spiral
for i=1, num_segments do
local current_angle = angle + (i / num_segments) * 6.283 -- 2*PI for a full circle
local color = 7 + (sin(t + i * 0.1) * 4) -- Cycling through colors
local radius = i * radius_step
local x = center_x + cos(current_angle) * radius
local y = center_y + sin(current_angle) * radius
pset(x, y, flr(color))
end
-- Draw particles
draw_particles()
end
Explanation
Particle System: We've added a simple particle system to simulate the "blue slime" spray.
add_particle(x, y, ang): Adds a new particle emitted from the spiral.
update_particles(): Moves particles based on their angle and reduces their life span. Removes them once expired.
draw_particles(): Renders each particle on the screen as a blue dot using pset() with color 12.
Spray on Movement: In the _update() function, we check if the movement keys are pressed. If so, it calls add_particle() twice to produce a spray effect by emitting particles at angles slightly offset from the current spiral angle.
This code will now emit blue particles whenever you move the spiral using the left or right arrow keys, simulating a "slime spray" effect. You can customize the number of particles, their lifetime, and movement speed for different visual results.
To make the sprayed "blue slime" particles stick on the walls in PICO-8, we need to adjust the movement logic for particles so that they stop moving when they hit the edges of the screen. Here's how we can modify the code to achieve this:
lua
-- initialize variables
t = 0
angle = 0
center_x = 64
center_y = 64
radius_step = 1.5
num_segments = 100
-- particles table
particles = {}
function add_particle(x, y, ang)
local particle = {
x = x,
y = y,
ang = ang,
speed = 2,
life = 30,
stuck = false
}
add(particles, particle)
end
function update_particles()
for p in all(particles) do
if not p.stuck then
-- Move the particle
p.x += cos(p.ang) * p.speed
p.y += sin(p.ang) * p.speed
-- Check collision with screen edges
if p.x < 0 then
p.x = 0
p.stuck = true
elseif p.x > 127 then
p.x = 127
p.stuck = true
end
if p.y < 0 then
p.y = 0
p.stuck = true
elseif p.y > 127 then
p.y = 127
p.stuck = true
end
end
-- Decrease life only if not stuck
if not p.stuck then
p.life -= 1
if p.life <= 0 then
del(particles, p)
end
end
end
end
function draw_particles()
for p in all(particles) do
pset(p.x, p.y, 12) -- blue color for particles
end
end
function _update()
-- Update time
t += 0.05
-- Control rotation with arrow keys
local moved = false
if btn(0) then -- left
angle -= 0.02
moved = true
elseif btn(1) then -- right
angle += 0.02
moved = true
end
-- Add particles if moved
if moved then
add_particle(center_x, center_y, angle + 0.25)
add_particle(center_x, center_y, angle - 0.25)
end
-- Update particles
update_particles()
end
function _draw()
cls()
-- Draw the spiral
for i=1, num_segments do
local current_angle = angle + (i / num_segments) * 6.283 -- 2*PI for a full circle
local color = 7 + (sin(t + i * 0.1) * 4) -- Cycling through colors
local radius = i * radius_step
local x = center_x + cos(current_angle) * radius
local y = center_y + sin(current_angle) * radius
pset(x, y, flr(color))
end
-- Draw particles
draw_particles()
end
Explanation
Particle Movement & Collision Detection:
stuck Property: Each particle now has a stuck property. If a particle hits the edge of the screen, it becomes "stuck", meaning it stops moving but remains visible.
Edge Collision: We check if a particle's position is beyond the screen boundaries (0 to 127 for both x and y). If it is, we set the position to the boundary and mark the particle as stuck.
We only decrease the life of a particle if it is not stuck, removing it only if it times out before reaching a wall.
This modification ensures that particles stick to the walls, creating a "slime" effect that builds up around the edges of the screen as the spiral is rotated.
I apologize for any confusion! When I mentioned "stick on the walls", I was referring to the screen's boundaries. However, if you'd like to visualize these boundaries as actual walls, you can draw borders on the edges of the screen. Once we draw these borders, it will be easier to see the particles "stick" to them when they reach the edges.
Here's how you can update the code to draw visible walls around the screen:
lua
-- initialize variables
t = 0
angle = 0
center_x = 64
center_y = 64
radius_step = 1.5
num_segments = 100
-- particles table
particles = {}
function add_particle(x, y, ang)
local particle = {
x = x,
y = y,
ang = ang,
speed = 2,
life = 30,
stuck = false
}
add(particles, particle)
end
function update_particles()
for p in all(particles) do
if not p.stuck then
-- Move the particle
p.x += cos(p.ang) * p.speed
p.y += sin(p.ang) * p.speed
-- Check collision with screen edges
if p.x < 0 then
p.x = 0
p.stuck = true
elseif p.x > 127 then
p.x = 127
p.stuck = true
end
if p.y < 0 then
p.y = 0
p.stuck = true
elseif p.y > 127 then
p.y = 127
p.stuck = true
end
end
-- Decrease life only if not stuck
if not p.stuck then
p.life -= 1
if p.life <= 0 then
del(particles, p)
end
end
end
end
function draw_particles()
for p in all(particles) do
pset(p.x, p.y, 12) -- blue color for particles
end
end
function draw_walls()
-- Draw top and bottom borders
rectfill(0, 0, 127, 1, 7) -- top border
rectfill(0, 126, 127, 127, 7) -- bottom border
-- Draw left and right borders
rectfill(0, 0, 1, 127, 7) -- left border
rectfill(126, 0, 127, 127, 7) -- right border
end
function _update()
-- Update time
t += 0.05
-- Control rotation with arrow keys
local moved = false
if btn(0) then -- left
angle -= 0.02
moved = true
elseif btn(1) then -- right
angle += 0.02
moved = true
end
-- Add particles if moved
if moved then
add_particle(center_x, center_y, angle + 0.25)
add_particle(center_x, center_y, angle - 0.25)
end
-- Update particles
update_particles()
end
function _draw()
cls()
-- Draw walls
draw_walls()
-- Draw the spiral
for i=1, num_segments do
local current_angle = angle + (i / num_segments) * 6.283 -- 2*PI for a full circle
local color = 7 + (sin(t + i * 0.1) * 4) -- Cycling through colors
local radius = i * radius_step
local x = center_x + cos(current_angle) * radius
local y = center_y + sin(current_angle) * radius
pset(x, y, flr(color))
end
-- Draw particles
draw_particles()
end
Explanation
Draw Walls: A new function draw_walls() is used to draw borders around the edges of the screen using rectfill(). The borders are colored with color 7 (light gray) to distinguish them from the background.
Top and Bottom Borders: Drawn along the top and bottom edges.
Left and Right Borders: Drawn along the left and right edges.
These walls will be drawn at the edges of the screen, making it easier to visualize them as "walls" against which the particles "stick." The particles will stop at these borders, creating the intended sticking effect. Feel free to adjust the colors and thickness of the borders to suit your preferences.
To create a green wireframe sphere that appears and spins when you move the spiral in PICO-8, you'll need to set up some basic 3D rendering for a wireframe sphere and integrate it with the existing spiral and particle system. The sphere will be rendered using a simple 3D projection, and it will rotate around its center.
Here's how you can implement this:
lua
-- initialize variables
t = 0
angle = 0
center_x = 64
center_y = 64
radius_step = 1.5
num_segments = 100
sphere_visible = false
sphere_angle = 0
-- particles table
particles = {}
function add_particle(x, y, ang)
local particle = {
x = x,
y = y,
ang = ang,
speed = 2,
life = 30,
stuck = false
}
add(particles, particle)
end
function update_particles()
for p in all(particles) do
if not p.stuck then
-- Move the particle
p.x += cos(p.ang) * p.speed
p.y += sin(p.ang) * p.speed
-- Check collision with screen edges
if p.x < 0 then
p.x = 0
p.stuck = true
elseif p.x > 127 then
p.x = 127
p.stuck = true
end
if p.y < 0 then
p.y = 0
p.stuck = true
elseif p.y > 127 then
p.y = 127
p.stuck = true
end
end
-- Decrease life only if not stuck
if not p.stuck then
p.life -= 1
if p.life <= 0 then
del(particles, p)
end
end
end
end
function draw_particles()
for p in all(particles) do
pset(p.x, p.y, 12) -- blue color for particles
end
end
function draw_walls()
-- Draw top and bottom borders
rectfill(0, 0, 127, 1, 7) -- top border
rectfill(0, 126, 127, 127, 7) -- bottom border
-- Draw left and right borders
rectfill(0, 0, 1, 127, 7) -- left border
rectfill(126, 0, 127, 127, 7) -- right border
end
function draw_sphere()
local num_points = 20
local radius = 20
for i = 0, num_points do
local lat_angle = i / num_points * 2 * 3.1415
for j = 0, num_points do
local lon_angle = j / num_points * 2 * 3.1415
local x = cos(lon_angle) * sin(lat_angle) * radius
local y = cos(lat_angle) * radius
local z = sin(lon_angle) * sin(lat_angle) * radius
-- Rotate the sphere by modifying x and z
local rotated_x = x * cos(sphere_angle) - z * sin(sphere_angle)
local rotated_z = x * sin(sphere_angle) + z * cos(sphere_angle)
-- Simple 3D to 2D projection
local screen_x = center_x + rotated_x
local screen_y = center_y + y
pset(screen_x, screen_y, 11) -- green color for sphere
end
end
end
function _update()
-- Update time
t += 0.05
-- Control rotation with arrow keys
local moved = false
if btn(0) then -- left
angle -= 0.02
moved = true
elseif btn(1) then -- right
angle += 0.02
moved = true
end
-- Sphere appears and spins when moved
if moved then
sphere_visible = true
sphere_angle += 0.02
add_particle(center_x, center_y, angle + 0.25)
add_particle(center_x, center_y, angle - 0.25)
else
sphere_visible = false
end
-- Update particles
update_particles()
end
function _draw()
cls()
-- Draw walls
draw_walls()
-- Draw the spiral
for i=1, num_segments do
local current_angle = angle + (i / num_segments) * 6.283 -- 2*PI for a full circle
local color = 7 + (sin(t + i * 0.1) * 4) -- Cycling through colors
local radius = i * radius_step
local x = center_x + cos(current_angle) * radius
local y = center_y + sin(current_angle) * radius
pset(x, y, flr(color))
end
-- Draw particles
draw_particles()
-- Draw the sphere if visible
if sphere_visible then
draw_sphere()
end
end
Explanation
Sphere Visibility and Rotation:
The sphere_visible variable controls whether the sphere is displayed. It becomes true when the spiral is moved.
The sphere_angle is incremented to spin the sphere when visible.
Wireframe Sphere:
The draw_sphere() function uses latitude and longitude angles to calculate the 3D positions of points on a sphere's surface.
A simple rotation transformation is applied to give the sphere a spinning effect.
The 3D points are projected to 2D screen coordinates and drawn with pset() using a green color 11.
Integration with Movement:
The sphere becomes visible and spins each time the spiral is moved using the arrow keys.
This setup provides a basic visualization of a spinning sphere that appears when the spiral is moved. Adjust the sphere's complexity by changing the num_points or the projection math for more detailed 3D effects.