ÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÀ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌÀ¬ÀÀÀ¬ÀÀÀ¬ÀÀÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÌ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌ̬ÀÀÀ¬ÀÀÀ¬ÀÀ¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî >»=""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""ÌÌ,ÌÌ,""ÌÌ ÌÌ ÌÌ ÌÌ 333333333333333333333333333333333333333333333333333333333333333333333333ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUàÌÌÌìÐÌÌÜÍÜàÌìÐÌÌýÌ ÐÌíÍÌÌ ÍÌÌ ÏìÍïÜÐìÀÌÌÌýÐÌÌÌÌÌÜÎÌÍ ÐÜÐìð ÍïÐìàÌðÌüÍßÍÍ ÏÜÍÿÌÀüÏÌÌÿÍÐìÌÍ ÐìðîÍ ÐÜÎ ÌÐìÌàÌìàÜÍÍ ÏÌÍÌÀ ÎÌÌÏ ÐìÌÎ ÀüÍ ÐÜÍ Ì ÐìÌÐìÜÐÜÍÍ ÏÌ ÍÍÎ ÍÎ ÌÀüÐìÌÏ ÀÜÍ ÐÜÌÍÌ ÐìÌÀ ÌÀìÍÍ ÏÝ ÍÍ Î ÌÀ ÌÀìÐìÌÎ ÐÌÜÍ ÐÜÌÏÌÌÐìàÌÎ ÍÀìÍÌÌ Í ÏýìÍÎ ÍàÜÐìÌÀìÐÌÌÜÌÍðÌÌüÍ ÐÜÌÎÌ ÐÌÌýÍ Í ÀìÍÍ Ï ÜÍÀ ÌÐÜÐÜÌÀìÐìÌÌÜàÌÌÍ ÐÜÍ Í ÐìÌÌÌ ÐÜÍÍ Ï ÎÌÐìÌÀÌÌÌÌÀüÐìÌïÌÐÌÍ ÐÜÎ Ï ÐìðÌÀüàÜÍÍ Ï ÏÌÐÜÜÏ ÌÌÏ ÐìÌÍ ðÌÍ ÐÜÐìð íÎ ÐìàÜÐìÍßÍÌÌÍ Ï ÐÌàÌìÎ ÍÌÿÍÐìÌÏüÐþàÜÍ ÐÌÌÜÍÜßÌÜÐìÐìàÜÐÌíÍÌÌÌÌÍÌÌ Ï àÌðÌüÍÎ ÌÌýÐÌÌÌÌÐÜðÍÌ ÌÌÌÌÌÌÌÌÌÌÌÌÌÌPUUPUUPUUPUUPUUPUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU DA D DBD$BD$AOD DADADBñ$3OAOôOô33AOO#ADAôB"$3OAB$B$33AOO#32 3Aô2D#3 DADB$B$33ADOD D"D"@"AD23#2 AB$B$33AO 23#ODôA?2#"!ðAOôOô33AO!ðA"B$Að2# DA D D33AOD DOñô33A32#3Õ--- title: TIC Space Invaders -- author: Muse for Terry -- desc: Space Invaders for TIC-80 - shoot the descending invaders before they land -- script: lua -- input: gamepad -- saveid: tic_space_invaders -- TIC Space Invaders for TIC-80, 240x136 - the arcade classic. -- Left/Right: move the cannon. X: fire. Z: pause. -- 5 rows of invaders march down, getting faster as you thin them -- out. Top row 30 points, middle rows 20, bottom rows 10. The -- mystery ship crossing the top is worth 100. Hide behind the -- shields - both sides' shots chew through them. 3 lives. Clear -- a wave and the next one comes faster. local state = "title" -- title, play, pause, levelclear, gameover local player = {x = 112, y = 124, w = 13, h = 5, speed = 2} local bullet = {x = 0, y = 0, active = false} local ebullets = {} local invaders = {} local shields = {} local ufo = {x = -20, y = 16, active = false, dx = 0, timer = 600} local score = 0 local best = 0 local lives = 3 local level = 1 local dir = 1 local move_timer = 0 local shoot_timer = 60 local anim = 0 local anim_timer = 0 local clear_timer = 0 local hit_timer = 0 local ROW_POINTS = {30, 20, 20, 10, 10} local ROW_COLORS = {3, 12, 12, 5, 5} local SPRITES = { -- top row: squid, 10 wide {"....XX....", "...XXXX...", "..XXXXXX..", ".XX.XX.XX.", "XXXXXXXXXX", "..X.XX.X..", ".X......X.", "X........X"}, -- middle rows: crab, 10 wide {"..X....X..", "...X..X...", "..XXXXXX..", ".XX.XX.XX.", "XXXXXXXXXX", "X.XXXXXX.X", "X.X..X.X.X", "...XX.XX.."}, -- bottom rows: octopus, 12 wide {"....XXXX....", ".XXXXXXXXXX.", "XXXXXXXXXXXX", "XXX..XX..XXX", "XXXXXXXXXXXX", "...XX..XX...", "..XX.XX.XX..", ".XX......XX."} } local function sprite_for_row(row) if row == 1 then return SPRITES[1] end if row <= 3 then return SPRITES[2] end return SPRITES[3] end local function invader_w(row) if row == 1 or row <= 3 then return 10 end return 12 end local function build_invaders() invaders = {} for r = 1, 5 do for c = 1, 10 do invaders[#invaders + 1] = { x = 36 + (c - 1) * 17, y = 28 + (r - 1) * 13, row = r, alive = true } end end end local function build_shields() shields = {} for s = 0, 3 do local sx = 30 + s * 56 for cy = 0, 2 do for cx = 0, 5 do -- notch out of the bottom middle, like the arcade if not (cy == 2 and (cx == 2 or cx == 3)) then shields[#shields + 1] = { x = sx + cx * 4, y = 104 + cy * 4, w = 4, h = 4, alive = true } end end end end end local function new_game() score = 0 lives = 3 level = 1 dir = 1 player.x = 112 bullet.active = false ebullets = {} ufo.active = false ufo.timer = 600 ufo.x = -20 hit_timer = 0 build_invaders() build_shields() move_timer = 0 shoot_timer = 60 state = "play" end local function next_level() level = level + 1 dir = 1 player.x = 112 bullet.active = false ebullets = {} ufo.active = false ufo.timer = 600 build_invaders() build_shields() move_timer = 0 shoot_timer = 60 state = "play" end local function alive_count() local n = 0 for i = 1, #invaders do if invaders[i].alive then n = n + 1 end end return n end local function move_interval() local n = alive_count() local iv = 6 + math.floor(n / 2) - (level - 1) * 3 if iv < 3 then iv = 3 end return iv end local function lowest_in_column(col) local best_inv = nil for i = 1, #invaders do local v = invaders[i] if v.alive then local c = math.floor((v.x - 36) / 17) + 1 if c == col and (best_inv == nil or v.y > best_inv.y) then best_inv = v end end end return best_inv end local function invader_shoot() local col = math.random(1, 10) local v = lowest_in_column(col) if v == nil then for c = 1, 10 do v = lowest_in_column(c) if v ~= nil then break end end end if v ~= nil and #ebullets < 3 then ebullets[#ebullets + 1] = { x = v.x + math.floor(invader_w(v.row) / 2), y = v.y + 8, dy = 2 } end shoot_timer = 70 - level * 5 if shoot_timer < 25 then shoot_timer = 25 end shoot_timer = shoot_timer + math.random(0, 30) end local function shield_hit(x, y, w, h) for i = 1, #shields do local s = shields[i] if s.alive and x + w > s.x and x < s.x + s.w and y + h > s.y and y < s.y + s.h then s.alive = false return true end end return false end local function lose_life() lives = lives - 1 bullet.active = false ebullets = {} if lives <= 0 then if score > best then best = score end state = "gameover" else player.x = 112 hit_timer = 45 end end local function update_invaders() move_timer = move_timer - 1 if move_timer > 0 then return end move_timer = move_interval() anim = 1 - anim -- find the edges of the living swarm local minx = 999 local maxx = -999 local maxy = -999 for i = 1, #invaders do local v = invaders[i] if v.alive then if v.x < minx then minx = v.x end if v.x + invader_w(v.row) > maxx then maxx = v.x + invader_w(v.row) end if v.y > maxy then maxy = v.y end end end if minx == 999 then return end local step = 2 * dir if (dir > 0 and maxx + step > 232) or (dir < 0 and minx + step < 8) then dir = -dir for i = 1, #invaders do local v = invaders[i] if v.alive then v.y = v.y + 8 end end if maxy + 8 >= 100 then if score > best then best = score end state = "gameover" end else for i = 1, #invaders do local v = invaders[i] if v.alive then v.x = v.x + step end end end end local function update_ufo() if ufo.active then ufo.x = ufo.x + ufo.dx if (ufo.dx > 0 and ufo.x > 240) or (ufo.dx < 0 and ufo.x < -20) then ufo.active = false ufo.timer = 500 + math.random(0, 400) end else ufo.timer = ufo.timer - 1 if ufo.timer <= 0 then ufo.active = true if math.random(0, 1) == 0 then ufo.x = -20 ufo.dx = 1 else ufo.x = 240 ufo.dx = -1 end ufo.y = 16 end end end local function update_bullet() if not bullet.active then return end bullet.y = bullet.y - 4 if bullet.y < 16 then bullet.active = false return end -- mystery ship if ufo.active and bullet.x + 2 > ufo.x and bullet.x < ufo.x + 16 and bullet.y + 4 > ufo.y and bullet.y < ufo.y + 7 then score = score + 100 ufo.active = false ufo.timer = 500 + math.random(0, 400) bullet.active = false return end -- shields if shield_hit(bullet.x - 1, bullet.y, 3, 5) then bullet.active = false return end -- invaders for i = 1, #invaders do local v = invaders[i] if v.alive and bullet.x + 2 > v.x and bullet.x < v.x + invader_w(v.row) and bullet.y + 4 > v.y and bullet.y < v.y + 8 then v.alive = false score = score + ROW_POINTS[v.row] bullet.active = false if alive_count() == 0 then score = score + level * 100 state = "levelclear" clear_timer = 90 end return end end end local function update_ebullets() for i = #ebullets, 1, -1 do local b = ebullets[i] b.y = b.y + b.dy local gone = false if b.y > 136 then gone = true elseif shield_hit(b.x - 1, b.y, 3, 5) then gone = true elseif b.y + 4 > player.y and b.y < player.y + player.h and b.x + 2 > player.x and b.x < player.x + player.w then gone = true lose_life() end if gone then table.remove(ebullets, i) end end end local function update_play() if hit_timer > 0 then hit_timer = hit_timer - 1 return end if btn(2) then player.x = player.x - player.speed end if btn(3) then player.x = player.x + player.speed end if player.x < 8 then player.x = 8 end if player.x > 232 - player.w then player.x = 232 - player.w end if btnp(4) and not bullet.active then bullet.x = player.x + 6 bullet.y = player.y - 5 bullet.active = true end if btnp(5) then state = "pause" return end shoot_timer = shoot_timer - 1 if shoot_timer <= 0 then invader_shoot() end update_invaders() if state ~= "play" then return end update_ufo() update_bullet() if state ~= "play" then return end update_ebullets() end local function update() if state == "title" then if btnp(4) or btnp(5) then new_game() end elseif state == "play" then update_play() elseif state == "pause" then if btnp(5) or btnp(4) then state = "play" end elseif state == "levelclear" then clear_timer = clear_timer - 1 if clear_timer <= 0 then next_level() end elseif state == "gameover" then if btnp(4) or btnp(5) then state = "title" end end end -- ---- drawing ---- local function draw_hud() rect(0, 0, 240, 14, 0) print("SCORE " .. score, 4, 4, 12) print("BEST " .. best, 96, 4, 4) print("LV " .. level .. " LIVES " .. lives, 168, 4, 5) rect(0, 14, 240, 1, 5) end local function draw_invader(v) local spr = sprite_for_row(v.row) local col = ROW_COLORS[v.row] for r = 1, #spr do local row = spr[r] for c = 1, #row do if row:sub(c, c) == "X" then local dx = c - 1 -- legs wiggle on the alternate frame if anim == 1 and r >= #spr - 1 then dx = dx + 1 end pix(v.x + dx, v.y + r - 1, col) end end end end local function draw_shields() for i = 1, #shields do local s = shields[i] if s.alive then rect(s.x, s.y, s.w, s.h, 6) end end end local function draw_player() if hit_timer > 0 and math.floor(hit_timer / 6) % 2 == 0 then return end rect(player.x, player.y, player.w, player.h, 6) rect(player.x + 5, player.y - 4, 3, 4, 6) rect(player.x, player.y, player.w, 1, 12) end local function draw_ufo() rect(ufo.x, ufo.y + 2, 16, 3, 2) rect(ufo.x + 4, ufo.y, 8, 3, 2) pix(ufo.x + 7, ufo.y + 5, 12) pix(ufo.x + 9, ufo.y + 5, 12) end local function draw_title() cls(0) draw_hud() -- demo swarm for r = 1, 5 do local fake = {x = 88, y = 30 + (r - 1) * 13, row = r, alive = true} draw_invader(fake) local fake2 = {x = 140, y = 30 + (r - 1) * 13, row = r, alive = true} draw_invader(fake2) end print("TIC SPACE INVADERS", 66, 100, 12) print("TIC SPACE INVADERS", 65, 99, 4) print("ARROWS: MOVE X: FIRE Z: PAUSE", 52, 112, 12) print("BY MUSE FOR TERRY", 78, 124, 13) end local function draw_pause() cls(0) draw_hud() rect(72, 56, 96, 24, 0) print("PAUSED", 96, 64, 12) end local function draw_levelclear() cls(0) draw_hud() rect(56, 48, 128, 40, 0) print("WAVE " .. level .. " CLEAR!", 84, 56, 5) print("BONUS " .. (level * 100), 92, 68, 4) end local function draw_gameover() cls(0) draw_hud() rect(56, 48, 128, 48, 0) print("GAME OVER", 88, 56, 2) print("SCORE " .. score, 92, 68, 12) print("BEST " .. best, 96, 80, 4) print("PRESS X TO PLAY AGAIN", 64, 108, 13) end local function draw() if state == "title" then draw_title() return end if state == "pause" then draw_pause() return end if state == "levelclear" then draw_levelclear() return end if state == "gameover" then draw_gameover() return end cls(0) draw_hud() for i = 1, #invaders do local v = invaders[i] if v.alive then draw_invader(v) end end if ufo.active then draw_ufo() end draw_shields() draw_player() if bullet.active then rect(bullet.x - 1, bullet.y, 3, 5, 12) end for i = 1, #ebullets do local b = ebullets[i] rect(b.x - 1, b.y, 3, 5, 3) end end function TIC() update() draw() end