Advertisement
nitrogenfingers

LaserBlast Repository

Oct 9th, 2014
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.69 KB | None | 0 0
  1. MKDIR laserblast
  2. MKFIL laserblast/laserblast
  3. WRITE 395
  4. --[[
  5. Laser Blast
  6.  
  7. Inspired by Space Invaders, Astro Smash and a bit of Arkanoid
  8. Destroy falling asteroids and collect powerups
  9.  
  10. Written by: Nitrogen Fingers
  11. ]]--
  12.  
  13. local version = 1.0
  14. local reponame = "Laser Blast"
  15. local requiresColour = false
  16. if requiresColour then
  17. print("This game can only be played on an Advanced Computer.")
  18. end
  19.  
  20. local function displayNitrosoftTitle()
  21. shell.run("clear")
  22. local _w,_h = term.getSize()
  23. for i=1,math.random(1,10)+10 do
  24. term.setCursorPos(math.random(1,_w), math.random(1,_h))
  25. term.setTextColour(colours.yellow)
  26. term.write(".")
  27. end
  28. local _t1,_t2 = "nitrosoft", "games"
  29. term.setCursorPos(math.floor(_w/2-#_t1), math.floor(_h/2))
  30. if term.isColour() then term.setTextColour(colours.blue) end
  31. term.write(_t1)
  32. term.setCursorPos(math.floor(_w/2-#_t2/2),math.floor(_h/2)+1)
  33. term.setTextColour(colours.white)
  34. term.write(_t2)
  35. term.setCursorPos(math.floor(_w/2-#_t1), math.floor(_h/2)+2)
  36. if term.isColour() then term.setTextColour(colours.red) end
  37. term.write(string.rep("-",#_t1*2))
  38. if term.isColour() then
  39. term.setBackgroundColour(colours.green)
  40. term.setCursorPos(_w/2+#_t1-4, math.floor(_h/2)-2)
  41. term.write(" ")
  42. term.setCursorPos(_w/2+#_t1-4, math.floor(_h/2)-1)
  43. term.write(" ")
  44. term.setBackgroundColour(colours.lime)
  45. term.setCursorPos(_w/2+#_t1-3, math.floor(_h/2)-1)
  46. term.write(" ")
  47. term.setCursorPos(_w/2+#_t1-3, math.floor(_h/2))
  48. term.write(" ")
  49. end
  50. term.setBackgroundColour(colours.black)
  51. term.setTextColour(colours.white)
  52. term.setCursorPos(1,_h)
  53. term.write("v"..version)
  54. os.pullEvent("key")
  55. end
  56. displayNitrosoftTitle()
  57.  
  58. local w,h = term.getSize()
  59. local plpos = math.floor(w/2)
  60.  
  61. --music stuff
  62. local minterval = 1
  63. local mtimer = 1
  64. local left = false
  65.  
  66. local level = 1
  67. local score = 0
  68. local gameover=false
  69. local killc = 0
  70.  
  71. --x,y,dir
  72. local projlist = {}
  73. --x,y,intvspeed,dtimer
  74. local baddylist = {}
  75. local btimer = 0
  76. local bintv = 1
  77. local utime = 0.05
  78. local bsmp = 6
  79. local powerup = 0
  80.  
  81. --Landscape and stars
  82. local stars = {}
  83. for i=1,math.random()*10+10 do
  84. stars[i] = { x = math.ceil(math.random()*w),
  85. y = math.ceil(math.random() * h-8) }
  86. end
  87. local landscape = {
  88. [6]=" _____________ ";
  89. [5]=" _______ / \\_____ ";
  90. [4]=" / \\___/____ \\_________";
  91. [3]=" / \\ ";
  92. [2]=" / \\_________ ";
  93. [1]="______/ \\______________";
  94. }
  95.  
  96. function drawHeader()
  97. if term.isColour() then term.setTextColour(colours.white) end
  98. term.setCursorPos(5, 1)
  99. term.write("Score: "..score)
  100. if score~=0 then term.write("00") end
  101. local lstr = "Level: "..level
  102. term.setCursorPos(w-#lstr-5,1)
  103. term.write(lstr)
  104.  
  105. if powerup > 0 then
  106. local pstr = "POWERUP"
  107. term.setCursorPos(w/2 - #pstr/2,1)
  108. if term.isColour() then term.setTextColour(colours.cyan) end
  109. term.write(pstr)
  110. end
  111. end
  112.  
  113. function drawPlayer()
  114. if term.isColour() then term.setTextColour(colours.white) end
  115. term.setCursorPos(plpos-1, h-1)
  116. term.write("@@@")
  117. end
  118.  
  119. function drawProjectile(proj)
  120. if term.isColour() then term.setTextColour(colours.red) end
  121. term.setCursorPos(proj.x, proj.y)
  122. term.write("|")
  123. end
  124.  
  125. function drawVacance(x,y)
  126. term.setCursorPos(x, y)
  127. for _,baddy in pairs(baddylist) do
  128. if baddy.x == x and baddy.y == y and baddy.dtimer > 0 then
  129. drawBaddie(baddy)
  130. return
  131. end
  132. end
  133.  
  134. if y >= h-8 and y <= h-3 then
  135. if term.isColour() then term.setTextColour(colours.lime) end
  136. term.write(string.sub(landscape[h - y - 2], x, x))
  137. elseif y < h-8 then
  138. for i=1,#stars do
  139. if x == stars[i].x and y == stars[i].y then
  140. if term.isColour() then term.setTextColour(colours.yellow) end
  141. term.write(".")
  142. return
  143. end
  144. end
  145. term.write(" ")
  146. else
  147. term.write(" ")
  148. end
  149. end
  150.  
  151. function drawBaddie(baddy)
  152. term.setCursorPos(baddy.x, baddy.y)
  153. if baddy.dtimer==0 then
  154. if baddy.pup then
  155. if term.isColour() then term.setTextColour(colours.blue) end
  156. term.write("P")
  157. elseif baddy.frag then
  158. if term.isColour() then term.setTextColour(colours.brown) end
  159. term.write("*")
  160. else
  161. if term.isColour() then term.setTextColour(colours.brown) end
  162. term.write("O")
  163. end
  164. else
  165. if term.isColour() then term.setTextColour(colours.orange) end
  166. term.write("#")
  167. end
  168. end
  169.  
  170. --This now only needs to be called once. Awesome!
  171. function drawWorld()
  172. drawLandscape()
  173. drawPlayer()
  174. drawHeader()
  175. end
  176.  
  177. function drawLandscape()
  178. if term.isColour() then
  179. term.setTextColour(colours.yellow)
  180. end
  181. for i=1,#stars do
  182. term.setCursorPos(stars[i].x, stars[i].y)
  183. term.write(".")
  184. end
  185.  
  186. term.setCursorPos(1,h)
  187. local land = string.rep("-", w)
  188. if term.isColour() then
  189. term.setTextColour(colours.green)
  190. end
  191. term.write(land)
  192. if term.isColour() then
  193. term.setTextColour(colours.lime)
  194. end
  195.  
  196. for y,line in ipairs(landscape) do
  197. term.setCursorPos(1,h-y-2)
  198. term.write(line)
  199. end
  200. end
  201.  
  202. function updateWorld()
  203. --The music stuff
  204. redstone.setOutput("back", false)
  205. mtimer=mtimer-utime
  206. if mtimer<=0 then
  207. mtimer = minterval
  208. if left then
  209. redstone.setOutput("left", true)
  210. redstone.setOutput("right", false)
  211. else
  212. redstone.setOutput("left", false)
  213. redstone.setOutput("right", true)
  214. end
  215. left = not left
  216. end
  217.  
  218. local i=1
  219. while i<=#projlist do
  220. drawVacance(projlist[i].x, projlist[i].y)
  221. projlist[i].y = projlist[i].y+projlist[i].dir
  222. if projlist[i].y <= 1 or projlist[i].y > h-1 then
  223. table.remove(projlist,i)
  224. i=i-1
  225. else drawProjectile(projlist[i]) end
  226. i=i+1
  227. end
  228. i=1
  229. while i<=#baddylist do
  230. local baddy = baddylist[i]
  231. baddy.timer=baddy.timer+utime
  232.  
  233. if baddy.y==h-1 and math.abs(baddy.x-plpos)<2 then
  234. if baddy.pup then
  235. powerup = 10
  236. drawPlayer()
  237. else
  238. gameover = true
  239. redstone.setOutput("back", true)
  240. end
  241. end
  242.  
  243. j=1
  244. while j<=#projlist do
  245. local proj = projlist[j]
  246. if baddy.x==proj.x and math.abs(baddy.y-proj.y)<2
  247. and baddy.dtimer==0 then
  248. baddy.dtimer = 0.5
  249. drawBaddie(baddy)
  250. drawVacance(projlist[j].x, projlist[j].y)
  251. table.remove(projlist,j)
  252. j=j-1
  253. score=score+5
  254. redstone.setOutput("back", true)
  255. killc=killc+1
  256. if killc>5+(level*5) and level<10 then levelUp() end
  257. drawHeader()
  258.  
  259. --Adds fragments
  260. if math.random(1, 5) == 2 and not baddy.frag then
  261. table.insert(baddylist, {
  262. x = baddy.x-1,
  263. y = baddy.y,
  264. pup = false,
  265. frag = true,
  266. timer = 0,
  267. dtimer = 0,
  268. speed = baddy.speed/2
  269. })
  270. drawBaddie(baddylist[#baddylist])
  271. table.insert(baddylist, {
  272. x = baddy.x+1,
  273. y = baddy.y,
  274. pup = false,
  275. frag = true,
  276. timer = 0,
  277. dtimer = 0,
  278. speed = baddy.speed/2
  279. })
  280. drawBaddie(baddylist[#baddylist])
  281. end
  282. end
  283. j=j+1
  284. end
  285.  
  286. if baddy.timer>baddy.speed and baddy.dtimer==0 then
  287. drawVacance(baddy.x, baddy.y)
  288. baddy.y=baddy.y+1
  289. if baddy.y==h then
  290. table.remove(baddylist,i)
  291. i=i-1
  292. score=score-1
  293. drawHeader()
  294. else
  295. drawBaddie(baddy)
  296. baddy.timer = 0
  297. end
  298. elseif baddy.dtimer>0 then
  299. baddy.dtimer=baddy.dtimer-utime
  300. if baddy.dtimer<=0 then
  301. drawVacance(baddy.x, baddy.y)
  302. table.remove(baddylist,i)
  303. i=i-1
  304. end
  305. end
  306. i=i+1
  307. end
  308. btimer=btimer+utime
  309. if btimer > bintv then
  310. table.insert(baddylist, {
  311. x = math.random(w/4, 3*(w/4)),
  312. y = 2,
  313. speed = utime*bsmp,
  314. timer = 0,
  315. dtimer = 0,
  316. pup = math.random(1,20)==5,
  317. frag = false
  318. })
  319. drawBaddie(baddylist[#baddylist])
  320. btimer=0
  321. end
  322. end
  323.  
  324. function levelUp()
  325. level=level+1
  326. bintv=bintv-0.10
  327. bsmp=bsmp-0.5
  328. killc=0
  329. minterval=minterval-0.10
  330. end
  331.  
  332. function updatePlayer(key)
  333. if powerup>0 then
  334. powerup = powerup-utime
  335. end
  336.  
  337. if key==203 and plpos>1 then
  338. term.setCursorPos(plpos+1,h-1)
  339. term.write(" ")
  340. plpos=plpos-1
  341. drawPlayer()
  342. elseif key==205 and plpos<w then
  343. term.setCursorPos(plpos-1,h-1)
  344. term.write(" ")
  345. plpos=plpos+1
  346. drawPlayer()
  347. elseif key==57 then
  348. if powerup>0 then
  349. table.insert(projlist, {
  350. dir = -1,
  351. x = plpos+1,
  352. y = h-2
  353. })
  354. drawProjectile(projlist[#projlist])
  355. table.insert(projlist, {
  356. dir = -1,
  357. x = plpos-1,
  358. y = h-2
  359. })
  360. drawProjectile(projlist[#projlist])
  361. else
  362. table.insert(projlist, {
  363. dir = -1,
  364. x = plpos,
  365. y = h-2
  366. })
  367. drawProjectile(projlist[#projlist])
  368. end
  369. end
  370. end
  371.  
  372. term.setBackgroundColour(colours.black)
  373. term.clear()
  374. drawWorld()
  375. local wtimer os.startTimer(utime)
  376. while not gameover do
  377. local e, v = os.pullEvent()
  378.  
  379. if e=="timer" then
  380. updateWorld()
  381. wtimer = os.startTimer(utime)
  382. elseif e=="key" then
  383. if v==28 then break end
  384. updatePlayer(v)
  385. end
  386. end
  387.  
  388. term.setCursorPos(plpos-1, h-1)
  389. if term.isColour() then term.setTextColour(colours.red) end
  390. term.write("###")
  391. local go = "Game Over!"
  392. term.setCursorPos(w/2 - #go/2, 10)
  393. if term.isColour() then term.setTextColour(colours.white) end
  394. term.write(go)
  395. term.setCursorPos(1,h)
  396. sleep(5)
  397. redstone.setOutput("back", false)
  398. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement