Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
Created April 2, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwjlyons/40f72a37558460fa2717f2305ffd6681 to your computer and use it in GitHub Desktop.
Save nwjlyons/40f72a37558460fa2717f2305ffd6681 to your computer and use it in GitHub Desktop.
Tron game built for the PICO8 fantasy console
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
line_weight = 1
border_colour = 7
up = 2
right = 1
down = 3
left = 0
ply1 = {}
ply1.x = 96
ply1.y = 64
ply1.colour = 12
ply1.name = "blue"
ply1.dir = up
ply1.num = 0
ply1.lost = false
ply1.score = 0
ply2 = {}
ply2.x = 32
ply2.y = 64
ply2.colour = 8
ply2.name = "red"
ply2.dir = up
ply2.num = 1
ply2.lost = false
ply2.score = 0
function _init()
cls()
rect(
0, 0, 127, 127, border_colour)
end
function another_game()
cls()
rect(
0, 0, 127, 127, border_colour)
ply1.lost = false
ply1.x = 96
ply1.y = 64
ply1.dir = up
ply2.lost = false
ply2.x = 32
ply2.y = 64
ply2.dir = up
end
menuitem(
1,
"another game",
another_game
)
function _update()
if is_game_active() then
update_dir(ply1)
update_dir(ply2)
move(ply1)
move(ply2)
collision(ply1)
collision(ply2)
increment_score()
end
end
function _draw()
rectfill(
0, 0, 127, 12, border_colour)
print(
" red: " .. ply2.score,
1, 1, ply2.colour
)
print(
"blue: " .. ply1.score,
1, 7, ply1.colour
)
if is_game_active() then
head(ply1)
head(ply2)
else
winner = get_winner()
print(winner.name .. " wins", 44, 74, winner.colour)
end
end
function head(ply)
rectfill(
ply.x,
ply.y,
ply.x,
ply.y,
ply.colour
)
end
function move(ply)
if ply.dir == up then
ply.y -= line_weight
elseif ply.dir == right then
ply.x += line_weight
elseif ply.dir == down then
ply.y += line_weight
elseif ply.dir == left then
ply.x -= line_weight
end
end
function update_dir(ply)
prev_dir = ply.dir
if btnp(up, ply.num) and prev_dir ~= down then
ply.dir = up
elseif btnp(right, ply.num) and prev_dir ~= left then
ply.dir = right
elseif btnp(down, ply.num) and prev_dir ~= up then
ply.dir = down
elseif btnp(left, ply.num) and prev_dir ~= right then
ply.dir = left
end
end
function collision(ply)
coord = pget(ply.x, ply.y)
if coord == ply1.colour or coord == ply2.colour or coord == border_colour then
ply.lost = true
end
end
function is_game_active()
if ply1.lost or ply2.lost then
return false
else
return true
end
end
function get_winner()
if ply1.lost then
return ply2
else
return ply1
end
end
function increment_score()
if ply1.lost then
ply2.score += 1
elseif ply2.lost then
ply1.score += 1
end
end
@nwjlyons
Copy link
Author

nwjlyons commented Apr 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment