_ ªª ªª ªªªªªªªªªªªªªªªª ªª  ªª wwpffgefvgffvgffvgffvpffwwwwpff ÇaÆÇlgfgfÆpff wwwwpffÇafvÇlÆÏgf÷|gffvpffwwC3A333343#""2222222f`"!!#"!""!"""! >À?""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""ÂÌ"ÂÌ"Ì",ÌÌ,ÌÌ""""ÂÌ,ÌÂ,ÂÌ"ÂÌ,ÌÌ,"""ÂÌ,ÂÌ"Ì",ÌÌ,ÌÌ""""Â,"Â""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""Ì",Ì",Ì",Ì""Ì",""""Ì"ÌÌ,Ì",Ì""Ì"""""Ì,"Ì",Ì",Ì""Ì","""Â,"","""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""Ì""Ì",Ì",ÌÌ"Ì",""""Ì"ÌÌ,Ì",ÌÂ,ÌÌ""""ÂÌ"Ì",Ì",ÌÌ"Ì",""""""","""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""Ì",Ì",ÂÌ"Ì""ÌÌ"""""Ì",,,ÌÌ,Ì",Ì""""""Ì,ÌÌ,ÂÌ"Ì""Ì","""Â,"","""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""ÂÌ"ÂÌ"","ÌÌ,Ì","""ÂÌ,,",Ì",ÂÌ,ÌÌ,"""ÌÌ"Ì",","ÌÌ,ÌÌ""""Â,"Â"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª ªª C3 ªª  ªª ªªªªÀÌ Ì ÀÌÌ ÌÌ ÀÌ ÌÀÌ A33ªªªªªªªªªªªªÌ Ì Ì ÌÀÌÌ ÌÌ3343ªªªªªªªªªªªªÀÌÌÌ Ì Ì ÌÌÀÌÌ #""2ªªªªªªªªªªªªÌ ÌÀ ÌÌ ÌÀÌÌ ÌÀ 22ªªªªªªªª ªª ÌÌÌ Ì Ì ÌÌ ÌÌÌÀÌÌ22 ªª  ªª  ªª 22 ªª  ªª  ªª  ªª ªª C3 ªª ªªªªÀÌ ÀÌÀÌÌÌÌÌ ÌÌ A33ªªªªªªªªÌ Ì Ì Ì ÌÌÀ À3343ªªªªªªªªÀÌÌÌ Ì ÌÌÌÌ#""2ªªªªªªªªÌ Ì Ì ÌÌÌÌÀ22ªªªª ªª ÌÌÀÌÀÌÌ ÌÌ ÌÌ 22 ªª  ªª 22 ªª  ªª  ªª ªª C3 ªª ªªªªA33ªªªªªªªª3343ªªªªªªªª#""2ªªªªªªªª22ªªªª ªª 22 ªª  ªª 22 ªª  ªª  ªª ªª C3C3C3C3C3C3C3C3C3C3C3 ªª ªªªªA33A33A33A33A33A33A33A33A33A33A33ªªªªªªªª33433343334333433343334333433343334333433343ªªªªªªªª#""2#""2#""2#""2#""2#""2#""2#""2#""2#""2#""2ªªªªªªªª2222222222222222222222ªªªª ªª 2222222222222222222222 ªª  ªª 2222222222222222222222 ªª  ªª  ªª ªª C3 ªª ªªªªA33ªªªªªªªª3343ªªªªªªªª#""2ªªªªªªªª22ªªªª ªª 22 ªª  ªª 22 ªª  ªª f` ªª ªª  ªª ªªªª"!ªªªªªªªª!#"ªªªªªªªª!""ªªªªªªªª!""ªªªª ªª "! ªª  ªª  ªª  ªª  ªª ªª  ªª ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª ªª  ªª  ªª  ªª  ªª  ªª ªª C3C3 ªª ªªªªA33A33ªªªªªªªª33433343ªªªªªªªª#""2#""2ªªªªªªªª2222ªªªª ªª 2222 ªª  ªª 2222 ªª  ªª  ªª ªª C3 ªª ªªªªA33ªªªªªªªª3343ªªªªªªªª#""2ªªªªªªªª22ªªªª ªª 22 ªª  ªª 22 ªª  ªª wwwwwwwwwwwwwwwwwwww ªª ªª Àffpffpffpffpffpffpffpffpffpff ªª ªªªªl|gefvgefvgefvgefvgefvgefvgefvgefvÇaƪªªªªªªªpÆ|gffvgffvgffvgffvgffvgffvgffvgffvÇlªªªªªªªªpfvgffvgffvgffvgffvgffvgffvgffvgffvgfªªªªªªªªlfvgffvgffvgffvgffvgffvgffvgffvgffvgfƪªªª ªª Àffpffpffpffpffpffpffpffpffpff ªª  ªª wwwwwwwwwwwwwwwwwwww ªª  ªª  ªª ªª C3C3 ªª ªªªªA33A33ªªªªªªªª33433343ªªªªªªªª#""2#""2ªªªªªªªª2222ªªªª ªª 2222 ªª  ªª 2222 ªª  ªª  ªª ªª C3C3 ªª ªªªªA33A33ªªªªªªªª33433343ªªªªªªªª#""2#""2ªªªªªªªª2222ªªªª ªª 2222 ªª  ªª 2222 ªª  ªª  ªª ªª C3C3 ªª ªªªªA33A33ªªªªªªªª33433343ªªªªªªªª#""2#""2ªªªªªªªª2222ªªªª ªª 2222 ªª  ªª 2222 ªª  ªª  ªª ªª C3C3 ªª ªªªªA33A33ªªªªªªªª33433343ªªªªªªªª#""2#""2ªªªªªªªª2222ªªªª ªª 2222 ªª  ªª 2222 ªª  ªª  ªª f` ªª  ªª ªª  ªª C3C3C3C3 ªª  ªª ªªªªªªªªA33A33A33A33"!ªªªªªªªªªªªªªªªª3343334333433343!#"ªªªªªªªªªªªªªªªª#""2#""2#""2#""2!""ªªªªªªªªªªªªªªªª22222222!""ªªªªªªªª ªª  ªª 22222222"! ªª  ªª  ªª  ªª 22222222 ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª  ªª # -- title: multi-snake -- author: jminor -- desc: what if snake + multiball? -- site: ? -- license: MIT License (change this to your license of choice) -- version: 0.1 -- script: lua t=0 step=0 dir=nil debug=nil width=30 height=17 snakes={} score=0 function TIC() if btnp(0) then dir={x=0,y=-1} end if btnp(1) then dir={x=0,y= 1} end if btnp(2) then dir={x=-1,y=0} end if btnp(3) then dir={x= 1,y=0} end cls(13) t=t+1 if t%16==0 then step=step+1 sim() dir=nil end map() for i,s in ipairs(snakes) do draw_snake(s) end print("SNAKES: "..#snakes,10,10,12) print(" SCORE: "..score,10,18,12) if debug then print(" DEBUG: "..debug,10,26,12) end end function sim() for i,s in ipairs(snakes) do update_snake(s) end for i,s in ipairs(snakes) do if s.dead then table.remove(snakes, i) end end if step%50==0 then spawn() end end function setup() spawn() snakes={mk_snake(width//2,height//2)} end function mk_snake(x,y) local snake={} snake.head={x=x,y=y} snake.vx=1 snake.vy=0 snake.body={} snake.len=1 return snake end function draw_snake(s) if s.dead then return end -- head local p=map2pix(s.head) local flip=s.vx==-1 and 1 or 0 local rot=0 if s.vx==0 then rot=s.vy==1 and 1 or 3 end spr(4-(t//8)%2,p.x,p.y,-1,1,flip,rot) -- body --for i,b in ipairs(s.body) do --p=map2pix(b) --spr(2,p.x,p.y) --end end function update_snake(s) if s.dead then return end if dir then if not (dir.x==-s.vx or dir.y==-s.vy) then s.vx=dir.x s.vy=dir.y end end table.insert(s.body, 1, s.head) mset(s.head.x,s.head.y,2) s.head={ x=s.head.x+s.vx, y=s.head.y+s.vy } if solid(s.head) then bonk(s) elseif fruit(s.head) then eat(s) end -- tail if #s.body>=s.len then local tail=s.body[s.len] table.remove(s.body, s.len) mset(tail.x,tail.y,0) end end function bonk(s) s.dead=true for i,b in ipairs(s.body) do mset(b.x,b.y,5) end -- don't do the head, that tile -- is already solid end function eat(s) score=score+1 -- grow s.len=s.len+1 -- turn s.vx,s.vy=-s.vy,s.vx -- split local ns=copy_snake(s) ns.vx,ns.vy=-ns.vx,-ns.vy table.insert(snakes, ns) spawn() end function spawn() local x=math.random(2,width-2) local y=math.random(2,height-2) mset(x,y,18) end function copy_snake(s) return deep_copy(s) end function deep_copy(obj, seen) if type(obj) ~= 'table' then return obj end if seen and seen[obj] then return seen[obj] end local s = seen or {} local res = setmetatable({}, getmetatable(obj)) s[obj] = res for k, v in pairs(obj) do res[deep_copy(k, s)] = deep_copy(v, s) end return res end function solid(m) local t=mget(m.x,m.y) return fget(t,0) end function fruit(m) local t=mget(m.x,m.y) local fruit=fget(t,2) mset(m.x,m.y,0) return fruit end function map2pix(m) return {x=m.x*8,y=m.y*8} end setup()