Skip to content

Instantly share code, notes, and snippets.

@mu578
Last active March 14, 2024 18:41
Show Gist options
  • Save mu578/04dd465a34425b3b980bfda247fd6464 to your computer and use it in GitHub Desktop.
Save mu578/04dd465a34425b3b980bfda247fd6464 to your computer and use it in GitHub Desktop.
--[[ INVADERS THE GAME ]]
--[[ GALACTIC DEFENSE ]]
--[[
Portable minidoc, PICO-8 built-in:
`cls()` - Clears the screen.
`spr(id, x, y, [w, h], [flip_x, flip_y])` - Draws a sprite at the specified position.
`btn(id)` - Checks if a button is pressed.
`btnp(id)` - Checks if a button was pressed in the current frame.
`map(x, y, [sx, sy], [sw, sh])` - Draws a map at the specified position.
`pset(x, y, [c])` - Sets a pixel at the specified position.
`pget(x, y)` - Retrieves the color of the pixel at the specified position.
`sfx(id, [note], [pitch], [volume], [channel])` - Plays a sound effect.
`music([id], [loop_start], [loop_length], [channel_mask])` - Plays background music.
`peek(addr)` - Reads a byte from memory.
`poke(addr, value)` - Writes a byte to memory.
`time()` - Retrieves the current time in milliseconds.
`rnd([seed])` - Generates a random number.
`sin(x)` - Calculates the sine of an angle.
`cos(x)` - Calculates the cosine of an angle.
`flr(x)` - Rounds a number down to the nearest integer.
`ceil(x)` - Rounds a number up to the nearest integer.
`abs(x)` - Returns the absolute value of a number.
`min(a, b)` - Returns the minimum of two numbers.
`max(a, b)` - Returns the maximum of two numbers.
`mid(x, y, z)` - Returns the middle value of three numbers.
`_draw()` - Plugin callback function for drawing operations.
`_update()` - Plugin callback function for game logic update.
]]
--[[ declare and initialize variables ]]
local invaders_aliens = { --[[ x, y, alive ]] }
local invaders_alien_speed = 0.3
local invaders_alien_spawn_timer = 0
local invaders_alien_spawn_interval = 60
--[[ declare and initialize bullet variables ]]
local invaders_bullet = {
x = 0
, y = 0
, speed = 2
, active = false
}
--[[ declare and initialize player variables ]]
local invaders_player = {
x = 60
, y = 120
, speed = 2
}
--
--[[ GAME ENGINE RUNNING ]]
-- update aliens state, change settings and grid at will.
for i = 0, 9 do
invaders_aliens[i] = {
x = i * 8 + 4
, y = 20
, alive = true
}
end
--
-- PICO-8 game engine callback `_draw` / graphic context, paint pixels.
--
function _draw()
-- clear game screen.
cls()
-- draw player.
spr(1, invaders_player.x, invaders_player.y)
-- draw bullet if active.
if invaders_bullet.active then
spr(3, invaders_bullet.x, invaders_bullet.y)
end
-- draw aliens.
for _, alien in pairs(invaders_aliens) do
if alien.alive then
spr(2, alien.x, alien.y)
end
end
end
--
-- PICO-8 game engine callback `_update` / event loop context.
--
function _update()
-- move player
if (btn(0)) then
invaders_player.x -= invaders_player.speed
elseif (btn(1)) then
invaders_player.x += invaders_player.speed
end
-- shoot bullet
if (not invaders_bullet.active and btnp(4)) then
invaders_bullet.x = invaders_player.x + 3
invaders_bullet.y = invaders_player.y - 4
invaders_bullet.active = true
end
-- move bullet
if invaders_bullet.active then
invaders_bullet.y -= invaders_bullet.speed
if invaders_bullet.y < 0 then
invaders_bullet.active = false
end
end
-- move aliens
invaders_alien_spawn_timer += 1
if invaders_alien_spawn_timer >= invaders_alien_spawn_interval then
for _, alien in pairs(invaders_aliens) do
alien.y += 8
if alien.y > 120 then
-- game over condition
invaders_player = nil
end
end
invaders_alien_spawn_timer = 0
end
-- check collision
for _, alien in pairs(invaders_aliens) do
if alien.alive
and invaders_bullet.active
and invaders_bullet.x >= alien.x
and invaders_bullet.x <= alien.x + 7
and invaders_bullet.y >= alien.y
and invaders_bullet.y <= alien.y + 7
then
alien.alive = false
invaders_bullet.active = alien.alive
end
end
end
-- EOG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment