̬̬̬̈̬̬̬̬̈̬̬̬̬̬̪쬪̬̬̬̪쬪 H%p%p&`&`'P'P(@(@)0)0* + ,-./////////////// 6667777899::;<==>????????????? 40445556677889::<==>??????????? 00000000001223445667789:;<<=>? 123456778888999966777888888888? @DDD@DDDDDDDD@DD;"-- title: Tixteroids -- author: spenot -- desc: Asteroids clone made in 4 hours -- license: MIT License -- version: 1.0 -- script: lua STARTLIVES=5 SHIPTURNPERFRAME=math.pi/60*2 SHIPACCELERATION=.03 SHIPDECELERATION=.03 SHIPMAXSPEED=3 SHIPRADIUS=5 SHIPCOLOR=4 BULLETRADIUS=1 BULLETSPEED=4 BULLETCOLOR=3 ROCKCOUNT=3 ROCKSPEED=0.3 ROCKSPEEDINCREASEPERSTAGE=0.1 ROCKRADIUS=5 ROCKCOLORS={ {15,14,13},-- grey {7,6,5}, -- green {2,3,4}, -- orange {9,10,11}, -- blue {1,2,3}, -- purple {8,9,10}, -- dark blue {14,13,12},-- white {10,11,12},-- light blue } EXPLOSIONDURATION=3*60 RESPAWNDURATION=3*60 WRAPAROUNDMARGIN=ROCKRADIUS*3 STAGEENDWAIT=3*60 highScore=0 function drawRock(rock) rx=(rock.x+.5)//1 ry=(rock.y+.5)//1 pal=ROCKCOLORS[(level-1)%#ROCKCOLORS+1] circ(rx,ry,rock.radius,pal[1]) circ(rx-rock.radius/10,ry-rock.radius/10,rock.radius/5*3,pal[2]) circ(rx-rock.radius/5,ry-rock.radius/5,rock.radius/5,pal[3]) end function rotate(p,angle) rx=p.x*math.cos(angle)-p.y*math.sin(angle) ry=p.x*math.sin(angle)+p.y*math.cos(angle) return {x=rx,y=ry} end tp1={x=0,y=-5} tp2={x=-3,y=5} tp3={x=3,y=5} function drawShip() rtp1=rotate(tp1,ship.pointAngle) rtp2=rotate(tp2,ship.pointAngle) rtp3=rotate(tp3,ship.pointAngle) tri(ship.x+rtp1.x,ship.y+rtp1.y, ship.x+rtp2.x,ship.y+rtp2.y, ship.x+rtp3.x,ship.y+rtp3.y, SHIPCOLOR) end t=0 level=1 lives=0 function moveObject(o) o.x=o.x+math.sin(o.angle)*o.speed o.y=o.y-math.cos(o.angle)*o.speed if o.x<-WRAPAROUNDMARGIN then o.x=o.x+(240+2*WRAPAROUNDMARGIN) end if o.x>=240+WRAPAROUNDMARGIN then o.x=o.x-(240+2*WRAPAROUNDMARGIN) end if o.y<-WRAPAROUNDMARGIN then o.y=o.y+(136+2*WRAPAROUNDMARGIN) end if o.y>136+WRAPAROUNDMARGIN then o.y=o.y-(136+2*WRAPAROUNDMARGIN) end end function moveShip() moveObject(ship) end -- returns true if object leaves screen function moveAndLoseObject(o) o.x=o.x+math.sin(o.angle)*o.speed o.y=o.y-math.cos(o.angle)*o.speed if o.x<-o.radius or o.x>240+o.radius or o.y<-o.radius or o.y>136+o.radius then return true end return false end bullets={} function shoot() bpos={x=0,y=-(SHIPRADIUS+BULLETRADIUS)} bpos=rotate(bpos,ship.pointAngle) bpos.x=bpos.x+ship.x bpos.y=bpos.y+ship.y table.insert(bullets,{x=bpos.x,y=bpos.y,radius=BULLETRADIUS,angle=ship.pointAngle,speed=BULLETSPEED}) sfx(0,"C-4",-1,1) end function moveBullets() for i=#bullets,1,-1 do if moveAndLoseObject(bullets[i]) then table.remove(bullets,i) end end end function drawBullets() for i=1,#bullets do bullet=bullets[i] circ(bullet.x,bullet.y,BULLETRADIUS,BULLETCOLOR) end end rocks={} function addRock(size) size=size or 3 source=math.random(1,4) if source==1 or source==2 then if source==1 then y=-size*ROCKRADIUS else y=136+size*ROCKRADIUS end x=math.random(-size*ROCKRADIUS,240+size*ROCKRADIUS) else if source==3 then x=-size*ROCKRADIUS else x=240+size*ROCKRADIUS end y=math.random(-size*ROCKRADIUS,136+size*ROCKRADIUS) end -- angle=math.random(0,359)/math.pi*2 angle=((math.random(0,59)+15)+math.random(0,3)*90)/math.pi*2 table.insert(rocks,{x=x,y=y,radius=size*ROCKRADIUS,angle=angle,speed=ROCKSPEED+(level-1)*ROCKSPEEDINCREASEPERSTAGE,size=size}) end function destroyRock(i) r=rocks[i] sfx(r.size,"B-1") score=score+100 table.remove(rocks,i) if r.size>1 then for j=1,3 do angle=math.random(0,359)/math.pi*2 table.insert(rocks,{x=r.x,y=r.y,radius=(r.size-1)*ROCKRADIUS,angle=angle,speed=r.speed*1.5,size=r.size-1}) end end end function moveRocks() for i=1,#rocks do moveObject(rocks[i]) end end function drawRocks() for i=1,#rocks do drawRock(rocks[i]) end end function length(x,y) return math.sqrt(x*x+y*y) end function collide(a,b) centerDistance=length(a.x-b.x,a.y-b.y) return centerDistance<=a.radius+b.radius end function collideBulletsWithRocks() for i=#bullets,1,-1 do for j=#rocks,1,-1 do if collide(bullets[i],rocks[j]) then table.remove(bullets,i) destroyRock(j) break end end end end function collideShipWithRocks() for j=#rocks,1,-1 do if collide(ship,rocks[j]) then return true end end return false end explosionParticles={} function createExplosion() for i=1,30 do angle=math.random(0,359)/math.pi*2 table.insert(explosionParticles,{x=ship.x,y=ship.y,angle=angle,radius=1,speed=3,color=2}) end for i=1,30 do angle=math.random(0,359)/math.pi*2 table.insert(explosionParticles,{x=ship.x,y=ship.y,angle=angle,radius=1,speed=2,color=3}) end for i=1,30 do angle=math.random(0,359)/math.pi*2 table.insert(explosionParticles,{x=ship.x,y=ship.y,angle=angle,radius=1,speed=1,color=4}) end end function animateExplosion() for i=#explosionParticles,1,-1 do if moveAndLoseObject(explosionParticles[i]) then table.remove(explosionParticles,i) end end end function drawExplosion() for i=1,#explosionParticles do ep=explosionParticles[i] pix(ep.x,ep.y,ep.color) end end function destroyExplosion() explosionParticles={} end function resetShip() ship={ x=120, y=68, radius=SHIPRADIUS, angle=0, pointAngle=0, speed=0, } end function gameInit() screen=2 bullets={} rocks={} explosionParticles={} resetShip() ship.state=0 --0:normal,1:explode,2:respawn for i=1,ROCKCOUNT do addRock() end stageEndWaitStart=0 end function gameTIC() cls() if ship.state~=1 then -- not exploding --[[ if btn(0) then ship.speed=ship.speed+SHIPACCELERATION if ship.speed>SHIPMAXSPEED then ship.speed=SHIPMAXSPEED end end if btn(1) then ship.speed=ship.speed-SHIPDECELERATION if ship.speed<0 then ship.speed=0 end end --]] if btn(0) then svx=math.sin(ship.angle)*ship.speed svy=math.cos(ship.angle)*ship.speed svx=svx+math.sin(ship.pointAngle)*SHIPACCELERATION svy=svy+math.cos(ship.pointAngle)*SHIPACCELERATION svl=length(svx,svy) if svl>SHIPMAXSPEED then svx=svx/svl svy=svy/svl svl=SHIPMAXSPEED end ship.angle=math.atan2(svx,svy) ship.speed=svl end if btn(2) then ship.pointAngle=ship.pointAngle-SHIPTURNPERFRAME end if btn(3) then ship.pointAngle=ship.pointAngle+SHIPTURNPERFRAME end if btnp(4) then shoot() end end if ship.state~=1 then -- not exploding moveShip() end moveBullets() moveRocks() collideBulletsWithRocks() if #rocks==0 and stageEndWaitStart==0 then stageEndWaitStart=t end if ship.state==0 then -- normal if collideShipWithRocks() then sfx(4,"C-1",-1,2) createExplosion() explosionStart=t ship.state=1 lives=lives-1 end elseif ship.state==1 then -- exploding animateExplosion() drawExplosion() if t>=explosionStart+EXPLOSIONDURATION then sfx(-1,-1,-1,2) destroyExplosion() if lives>0 then resetShip() ship.state=2 respawnStart=t else gameOverInit() end end elseif ship.state==2 then -- respawning if t>=respawnStart+RESPAWNDURATION then ship.state=0 end end drawRocks() drawBullets() if ship.state==0 or (ship.state==2 and (t-respawnStart)//6%2==0) then drawShip() end displayHUD() t=t+1 if #rocks==0 and t-stageEndWaitStart>STAGEENDWAIT then level=level+1 stageIntroInit() end end ------------------ OTHER SCREENS -- 0 title -- 1 stage intro -- 2 game -- 3 game over screen=0 function TIC() poke(0x3FFB,0) if screen==0 then titleTIC() elseif screen==1 then stageIntroTIC() elseif screen==2 then gameTIC() elseif screen==3 then gameOverTIC() end end function titleInit() screen=0 titleT=0 end titleInit() function titleTIC() cls() centerPrint("TIXTEROIDS",30,3,12) centerPrint("by spenot",55,1,13) highScoreText=string.format("HIGH SCORE %06d",highScore) centerPrint(highScoreText,79,1,12) if titleT%60<30 then centerPrint("INSERT COIN",100,1,12) end titleT=titleT+1 if btnp(4) then level=1 score=0 lives=STARTLIVES stageIntroInit() end end function stageIntroInit() screen=1 stageIntroT=0 end function stageIntroTIC() cls() displayHUD() centerPrint("STAGE "..level,63,2,12) stageIntroT=stageIntroT+1 if stageIntroT>3*60 then gameInit() end end function gameOverInit() screen=3 end function gameOverTIC() cls() centerPrint("GAME OVER",63,2,12) scoreText=string.format("SCORE %06d",score) centerPrint(scoreText,80,1,12) if highScore