This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
nitrogenfingers's profile picture

Game: LaserBlast

Started by nitrogenfingers, 04 March 2012 - 02:59 PM
nitrogenfingers #1
Posted 04 March 2012 - 03:59 PM
I recently made a game in Lua, with ComputerCraft. It's called LaserBlast:

[media]http://youtu.be/9_KK_ahFLCA[/media]

It took about 5 hours to write, I have no idea where I got the idea from- I think I might have played something similar on my dad's Powerbook when I was 10 or something.
It's actually pretty good fun to play.

I've recently update this for colour as well, but is still compatible on B&W monitors.

Controls:
Arrow Keys- move left and right
Space Bar- fire laser
Enter- quit game

Instructions:
Collect the powerup P's while shooting down asteroid O's and fragment *'s.

Download: http://pastebin.com/uBfD8SAe
Espen #2
Posted 04 March 2012 - 04:35 PM
Very nice, looks pretty solid. *thumbs up*

All of your computer's programs can be found here:
.minecraftsavesYOURWORLDcomputer
Within this folder there will be numbered directories.
Each number represents the ID of a computer and within each will be your programs for that computer.
Hope that helps. :unsure:/>/>
nitrogenfingers #3
Posted 04 March 2012 - 04:53 PM
It does indeed, thanks Lua Pro!
Sleep is not coming tonight so I may as well upload the source. It's just below.
That was a lot of fun to make, hope someone has fun playing it.



w,h = term.getSize()
plpos = w/2

--music stuff
minterval = 1
mtimer = 1
left = false

level = 1
score = 0
gameover=false
killc = 0

--x,y,dir
projlist = {}
--x,y,intvspeed,dtimer
baddylist = {}
btimer = 0
bintv = 1
utime = 0.05
bsmp = 6
powerup = 0

function drawHeader()
  term.setCursorPos(5, 1)
  term.write("Score: "..score)
  if score~=0 then term.write("00") end
  local lstr = "Level: "..level
  term.setCursorPos(w-#lstr-5,1)
  term.write(lstr)
end

function drawWorld()
  term.clear()
  drawHeader()
  drawLandscape()
  term.setCursorPos(plpos-1, h-1)
  term.write("@@@")
  for i=1,#projlist do
    local proj = projlist[i]
    term.setCursorPos(proj.x, proj.y)
    term.write("|")
  end
  for i=1,#baddylist do
    local baddy = baddylist[i]
    term.setCursorPos(baddy.x, baddy.y)
    if baddy.dtimer==0 then
      if baddy.pup then term.write("P")
      elseif baddy.frag then term.write("*")
      else term.write("O") end
    else
      term.write("#")
    end
  end
end

function drawLandscape()
  term.setCursorPos(1,h)
  local land = string.rep("-", w)
  term.write(land)
  local m6="                       _____________              "
  local m5="          _______     /             ____         "
  local m4="         /       ___/___                ________"
  local m3="        /                                        "
  local m2="       /                  ________               "
  local m1="______/                            ______________"
  term.setCursorPos(1, h-8)
  term.write(m6)
  term.setCursorPos(1, h-7)
  term.write(m5)
  term.setCursorPos(1, h-6)
  term.write(m4)
  term.setCursorPos(1, h-5)
  term.write(m3)
  term.setCursorPos(1, h-4)
  term.write(m2)
  term.setCursorPos(1, h-3)
  term.write(m1)
end

function updateWorld()
  --The music stuff  
  redstone.setOutput("back", false)
  mtimer=mtimer-utime
  if mtimer<=0 then
    mtimer = minterval
    if left then
      redstone.setOutput("left", true)
      redstone.setOutput("right", false)
    else
      redstone.setOutput("left", false)
      redstone.setOutput("right", true)
    end
    left = not left
  end

  local i=1
  while i<=#projlist do
    projlist[i].y = projlist[i].y+projlist[i].dir
    if projlist[i].y < 0 or projlist[i].y > h-1 then
      table.remove(projlist,i)
      i=i-1
    end
    i=i+1
  end
  i=1
  while i<=#baddylist do
    local baddy = baddylist[i]
    baddy.timer=baddy.timer+utime

    if baddy.y==h-1 and math.abs(baddy.x-plpos)<2 then
      if baddy.pup then powerup = 10
      else
        gameover = true 
        redstone.setOutput("back", true)
      end
    end

    j=1
    while j<=#projlist do
      local proj = projlist[j]
      if baddy.x==proj.x and math.abs(baddy.y-proj.y)<2
      and baddy.dtimer==0 then
        baddy.dtimer = 0.5
        table.remove(projlist,j)
        j=j-1
        score=score+5
        redstone.setOutput("back", true)
        killc=killc+1
        if killc>5+(level*5) and level<10 then levelUp() end

        --Adds fragments
        if math.random(1, 5) == 2 and not baddy.frag then
          table.insert(baddylist, {
            x = baddy.x-1,
            y = baddy.y,
            pup = false,
            frag = true,
            timer = 0,
            dtimer = 0,
            speed = baddy.speed/2
          })
          table.insert(baddylist, {
            x = baddy.x+1,
            y = baddy.y,
            pup = false,
            frag = true,
            timer = 0,
            dtimer = 0,
            speed = baddy.speed/2
          })
        end
      end
      j=j+1
    end

    if baddy.timer>baddy.speed and baddy.dtimer==0 then
      baddy.y=baddy.y+1
      baddy.timer = 0
      if baddy.y==h then
        table.remove(baddylist,i)
        i=i-1
        score=score-1
      end
    elseif baddy.dtimer>0 then
      baddy.dtimer=baddy.dtimer-utime
      if baddy.dtimer<=0 then
        table.remove(baddylist,i)
        i=i-1
      end
    end    
    i=i+1
  end  
  btimer=btimer+utime
  if btimer > bintv then
    table.insert(baddylist, {
      x = math.random(w/4, 3*(w/4)),
      y = 1,
      speed = utime*bsmp,
      timer = 0,
      dtimer = 0,
      pup = math.random(1,20)==5,
      frag = false
    })
    btimer=0
  end
end

function levelUp()
  level=level+1
  bintv=bintv-0.10
  bsmp=bsmp-0.5
  killc=0
  minterval=minterval-0.10
end

function updatePlayer(key)
  if powerup>0 then
    powerup = powerup-utime
  end

  if key==203 and plpos>1 then
    plpos=plpos-1
  elseif key==205 and plpos<w then
    plpos=plpos+1
  elseif key==57 then
    if powerup>0 then
      table.insert(projlist, {
        dir = -1,
        x = plpos+1,
        y = h-2
      })
      table.insert(projlist, {
        dir = -1,
        x = plpos-1,
        y = h-2
      })
    else
      table.insert(projlist, {
        dir = -1,
        x = plpos,
        y = h-2
      })
    end
  end
end

local wtimer os.startTimer(utime)
while not gameover do
  local e, v = os.pullEvent()

  if e=="timer" then
    updateWorld()
    wtimer = os.startTimer(utime)
  elseif e=="key" then
    if v==28 then break end
    updatePlayer(v)
  end
  drawWorld()
end

term.setCursorPos(plpos-1, h-1)
term.write("###")
local go = "Game Over!"
term.setCursorPos(w/2 - #go/2, 10)
term.write(go)
term.setCursorPos(1,h)
sleep(5)
redstone.setOutput("back", false)
term.clear()
</w>
Mega1mpact #4
Posted 04 March 2012 - 05:29 PM
sweet game! but it is so hard ;.;
Espen #5
Posted 04 March 2012 - 05:31 PM
Lua Pro is just a title/group name. The actual username is above that on the blue line, in my case: Espen
But no biggie, thanks for sharing! :unsure:/>/>
nitrogenfingers #6
Posted 05 March 2012 - 09:55 AM
So sorry Espen! Very late night last night, clearly wasn't thinking very clearly.

I agree the game is too hard, I've modified it so level progression is 50% slower and I cap at level 10 rather than that fiendish level 6.
That one seemed to do alright, and it was a lot of fun to make. I might do another one at some stage- probably keep it in the one post so as not to bloat the forums.

Thanks for playing!
Baldfrost #7
Posted 05 March 2012 - 04:18 PM
ok so i tried just writing off all the code into my computer in smp but i get this message.

bios:206: [string "LaserBlast"]:38 '=' expected

what whould i do to fix it?
Espen #8
Posted 05 March 2012 - 08:21 PM
ok so i tried just writing off all the code into my computer in smp but i get this message. bios:206: [string "LaserBlast"]:38 '=' expected what whould i do to fix it?
Works perfectly for me. You must've mistyped somewhere.
Go to the line it tells you and see if it's different to what you've copied by hand.
Sometimes it's not exacty on the given line, but 1 above or below.
It's just some missing brackets or something similar, I'm sure.
Baldfrost #9
Posted 05 March 2012 - 10:29 PM
is the line then 206 or 38?

found it.

now just to fix some other mistypes.

thx for the help.

There was one problem i couldn't find any mistakes anywhere.

It says
LaserBlast:34: attempt to index ? (a nil value)

couldn't find any mistakes in the code i copied.
nitrogenfingers #10
Posted 05 March 2012 - 11:58 PM
That sounds like a bug on my end- I'll see if I can replicate it.

Line 34 on my version calls term.clear() so I have no idea how that error has occurred.
I've added a download to mediafire at the top of the page so might be worth trying that one.
Edited on 05 March 2012 - 11:05 PM
FuzzyPurp #11
Posted 06 March 2012 - 09:10 AM
Dude…this is nice!
Baldfrost #12
Posted 06 March 2012 - 09:39 AM
That sounds like a bug on my end- I'll see if I can replicate it.

Line 34 on my version calls term.clear() so I have no idea how that error has occurred.
I've added a download to mediafire at the top of the page so might be worth trying that one.

it works for me in singleplayer where i put it in the save game folder, the bug is on multiplayer.
Cloudy #13
Posted 06 March 2012 - 12:02 PM
That sounds like a bug on my end- I'll see if I can replicate it.

Line 34 on my version calls term.clear() so I have no idea how that error has occurred.
I've added a download to mediafire at the top of the page so might be worth trying that one.

it works for me in singleplayer where i put it in the save game folder, the bug is on multiplayer.

The code should be executing exactly the same - it is probably an issue with typing it out, or the person who put it on the server.
Baldfrost #14
Posted 06 March 2012 - 02:57 PM
That sounds like a bug on my end- I'll see if I can replicate it.

Line 34 on my version calls term.clear() so I have no idea how that error has occurred.
I've added a download to mediafire at the top of the page so might be worth trying that one.

it works for me in singleplayer where i put it in the save game folder, the bug is on multiplayer.

The code should be executing exactly the same - it is probably an issue with typing it out, or the person who put it on the server.

it was me who put in on the server (typing it on the computer) and fixed all the mistypes, think the last mistype was on line 186 or something and then there was a mistake on line 34 yesterday which aren't there today or maybe because i deleted some code and re typed it exacly like it was before.
Baldfrost #15
Posted 07 March 2012 - 04:54 PM
That sounds like a bug on my end- I'll see if I can replicate it.

Line 34 on my version calls term.clear() so I have no idea how that error has occurred.
I've added a download to mediafire at the top of the page so might be worth trying that one.

i no longer have that bug. now it just give me a number, so far i it have said 13 12 and 16.
BrettSmith21 #16
Posted 13 May 2012 - 02:13 PM
Nice can i add it in …os its my os i will link this post
nitrogenfingers #17
Posted 14 May 2012 - 02:08 AM
Sure, you're free to it! If you do end up using it, let me know; I'd love to see where it ends up.
libraryaddict #18
Posted 14 May 2012 - 10:14 AM
Kind of amazed a nice game like this sank into oblivion for a while..
djm1999 #19
Posted 02 November 2012 - 09:23 AM
how do u play it? im having trouble with the vid
Skullblade #20
Posted 02 November 2012 - 09:36 AM
this is a really cool game and if you are not updating it it the pretty colors of computercraft 1.45 can you give me permission to? thx
casr144 #21
Posted 02 November 2012 - 09:57 AM
Yeah, a colour version of this would be amazing.
Skullblade #22
Posted 02 November 2012 - 11:20 AM
Did you see the date that this was made though? He might not be watching this thread anymore…sadly:( This is a really impressive program!
BigSHinyToys #23
Posted 02 November 2012 - 11:56 AM
It would not take much work to just add color but to "rework" it specifically for color would be a bit more complex. look for the print() statement and add a term.setColor() before it. Should be petty simple. to add a colored background would be a more complex rewrite. casr144 you should have a crack at adding color.
casr144 #24
Posted 02 November 2012 - 12:20 PM
I was going to look at the code and attempt to add colour tonight but I think I'll leave it for the weekend. I'll seek permission from nitrofingers to post an updated colour version if I do end up making one.
Skullblade #25
Posted 02 November 2012 - 12:44 PM
I was going to update it also maybe I can work with you. IDK how much help I can be though because I lost power form the hurricane so I only get to go on the computer when my generator is running (which is not much cause I'm running low on gas). If I can help I would love to!
remiX #26
Posted 02 November 2012 - 01:51 PM
Wow this is epic :D/>/>
nitrogenfingers #27
Posted 02 November 2012 - 11:20 PM
Laserblast is back on the first page of the forums? There's a blast from the past…

Y Axial collision detection stopped working as a result of something in 1.3, and as of 1.4 wasn't working. I couldn't figure out why exactly and had resolved to rewrite a lot of the code from scratch, but with NPaintPro and a few other projects popping up it never quite got done.

I'll get around to it at some point, sooner rather than later if the interest is there (which it seems to be). I had no plans to bring it to Colour but it'd be cool to see, so I'm happy for someone to patch it in as a feature and give them a dev credit or something.


This is sort of off topic but it's nice to see this game still garnering interest, it was something I really thought was good fun. So fun in fact, I've been working on a related project…



But that's for another time :D/>/>
casr144 #28
Posted 03 November 2012 - 05:51 AM
Cool ! Nitro, I'm a big fan of your videos and love your programs :D/>/>
casr144 #29
Posted 03 November 2012 - 05:56 AM
And Skullblade, I would happily work with you on a colour version of LaserBlast with you but I've decided it best to leave it to a further date or to let someone else do it. This is mainly because I have my exams in 3 weeks and that I'm currently making a lapis/redstone mc mod ! :D/>/>
nitrogenfingers #30
Posted 05 August 2013 - 11:29 PM
This is probably not worth necroing the topic but I've updated this game to work with colour, as well as optimized drawing a bit:
Spoiler
It also fixed a long standing bug with this program to do with the change in monitor size, but it still only really works with 51 x 19. The download has been updated in the main post.
TheOddByte #31
Posted 09 August 2013 - 09:36 AM
Really nice that you've added colors to this! :D/>
JohntheDipper #32
Posted 15 August 2013 - 02:40 AM
May I include this game in my OS? (It's not released, I don't have permissions to make a topic yet :s)
It's extremely addicting.
nitrogenfingers #33
Posted 15 August 2013 - 11:39 AM
Sure. Same deal with all my programs: Leave the header intact, credit me somewhere, besides that go nuts.