SciQuest_csp

Stars and bugs game

Jul 11th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. --------------------------------------
  2. -- Boom
  3. --------------------------------------
  4.  
  5. Boom = class()
  6.  
  7. function Boom:init(x,y)
  8.     self.x = x
  9.     self.y = y
  10.     
  11.     self.del = false
  12.     
  13.     self.t = 0
  14.     
  15.     table.insert(booms, self)
  16. end
  17.  
  18. function Boom:draw()
  19.     self.t = self.t + DeltaTime
  20.     if 1 < self.t then
  21.         self.del = true
  22.     end
  23.     
  24.     pushMatrix()
  25.     pushStyle()
  26.     
  27.     spriteMode(CENTER)
  28.     translate(self.x, self.y)
  29.     scale(1+self.t, 1+self.t)
  30.     tint(255, 255, 255, 255*(1-self.t))
  31.     sprite("Tyrian Remastered:Explosion Huge")
  32.     
  33.     popStyle()
  34.     popMatrix()
  35. end
  36.  
  37. function Boom:touched(touch)
  38.     -- Codea does not automatically call this method
  39. end
  40.  
  41. --------------------------------------
  42. -- Bug
  43. --------------------------------------
  44.  
  45. Bug = class()
  46.  
  47. function Bug:init()
  48.     self.x = -200
  49.     self.o = math.random(0, HEIGHT)
  50.     self.y = self.o
  51.     self.w = 0
  52.     
  53.     table.insert(bugs, self)
  54. end
  55.  
  56. function Bug:draw()
  57.     self.x = self.x + bugSpd * DeltaTime
  58.     
  59.     self.w = 50 * math.sin(score * 5)
  60.     self.y = self.o + self.w
  61.     sprite("SpaceCute:Beetle Ship", self.x, self.y)
  62. end
  63.  
  64. function Bug:touched(touch)
  65.     -- Codea does not automatically call this method
  66. end
  67.  
  68.  
  69. --------------------------------------
  70. -- Helper
  71. --------------------------------------
  72.  
  73.  
  74.  
  75. function intro()
  76.     fill(250, 255, 0, 255)
  77.     font("Inconsolata")
  78.     fontSize(50)
  79.     text("Remember, losing is fun!", WIDTH/2, HEIGHT*3/4)
  80.     spriteMode(CENTER)
  81.     sprite("SpaceCute:Star", WIDTH/3, HEIGHT/2)
  82.     text("good!", WIDTH/3, HEIGHT/3)
  83.     sprite("SpaceCute:Beetle Ship", WIDTH*2/3, HEIGHT/2, 150, 150)
  84.     text("bad!", WIDTH*2/3, HEIGHT/3)
  85. end
  86.  
  87. function game()
  88.     score = score + DeltaTime
  89.     
  90.     nrg(-decay * DeltaTime)
  91.     starDelay = 1 / sps
  92.     starTimer = starTimer + DeltaTime
  93.     if starDelay < starTimer then
  94.         Star()
  95.         starTimer = 0
  96.     end
  97.     
  98.     for k,v in pairs(booms) do
  99.         v:draw()
  100.         if v.del == true then
  101.             table.remove(booms, k)
  102.         end
  103.     end
  104.     
  105.     for k,v in pairs(stars) do
  106.         v:draw()
  107.         if v.del == true then
  108.             table.remove(stars, k)
  109.             nrg(-5)
  110.         end
  111.     end
  112.     
  113.     for k,bug in pairs(bugs) do
  114.         bug:draw()
  115.     end
  116.     
  117.     spriteMode(CENTER)
  118.     translate(WIDTH/2, 18)
  119.     scale(energy*WIDTH/600, 3)
  120.     sprite("Tyrian Remastered:Bullet Fire D")
  121. end
  122.  
  123. function fin()
  124.     text("You scored:", WIDTH/2, HEIGHT*2/3)
  125.     fontSize(100)
  126.     text( math.ceil(10*score), WIDTH/2, HEIGHT/2)
  127. end
  128.  
  129.  
  130.  
  131. --------------------------------------
  132. -- Main
  133. --------------------------------------
  134.  
  135. function setup()
  136.     parameter("bugSpd", 0, 200, 210)
  137.     parameter("radius", 0, 300, 200)
  138.     parameter("acc", 50, 200, 100)
  139.     parameter("sps", 0, 10, 2) -- stars per sec
  140.     starTimer = 0
  141.     parameter("energy", 0, 100, 100)
  142.     parameter("decay", 0, 5, 1)
  143.     score = 0
  144.     watch("score")
  145.     
  146.     INTRO = 1
  147.     GAME = 2
  148.     FIN = 3
  149.     gameState = INTRO
  150.     
  151.     stars = {}
  152.     Star()
  153.     
  154.     bugs = {}
  155.     Bug()
  156.     
  157.     booms = {}
  158. end
  159.  
  160. function draw()
  161.     spriteMode(CORNER)
  162.     sprite("SpaceCute:Background", 0, 0, WIDTH, HEIGHT)
  163.     
  164.     if gameState == INTRO then
  165.         intro()
  166.     elseif gameState == GAME then
  167.         game()
  168.     elseif gameState == FIN then
  169.         fin()
  170.     end
  171. end
  172.  
  173. function pyth(a, b)
  174.     dx = a.x - b.x
  175.     dy = a.y - b.y
  176.     return math.sqrt(dx*dx + dy*dy)
  177. end
  178.  
  179. function touched(t)
  180.     if gameState == INTRO then
  181.         gameState = GAME
  182.     end
  183.     
  184.     nrg(99)
  185.     
  186.     if t.state == BEGAN then
  187.         canTouch = true
  188.         for k,bug in pairs(bugs) do
  189.             if canTouch and pyth(t, bug) < radius then
  190.                 print(score)
  191.                 Boom(bug.x, bug.y)
  192.                 table.remove(bugs, k)
  193.             end
  194.         end
  195.         
  196.         for k,v in pairs(stars) do
  197.             if canTouch and pyth(t, v) < radius then
  198.                 nrg(7)
  199.                 table.remove(stars, k)
  200.                 canTouch = false
  201.             end
  202.         end
  203.         if canTouch == true then 
  204.             nrg(-3)
  205.             Bug()
  206.         end
  207.     end
  208. end
  209.  
  210. function nrg(e)
  211.     energy = energy + e
  212.     if 100 < energy then
  213.         energy = 100
  214.     elseif energy < 0 then
  215.         energy = 0
  216.         gameState = FIN
  217.     end
  218. end
  219.  
  220.  
  221. --------------------------------------
  222. -- Star
  223. --------------------------------------
  224.  
  225. Star = class()
  226.  
  227. function Star:init()
  228.     self.x = math.random(0, WIDTH)
  229.     self.y = HEIGHT + 150
  230.     
  231.     self.vel = 0
  232.     
  233.     self.del = false
  234.     
  235.     table.insert(stars, self)
  236. end
  237.  
  238. function Star:draw()
  239.     spriteMode(CENTER)
  240.     sprite("SpaceCute:Star", self.x, self.y)
  241.     self.vel = self.vel + acc * DeltaTime
  242.     self.y = self.y - self.vel * DeltaTime
  243.     
  244.     if self.y < -60 then 
  245.         self.del = true
  246.     end
  247. end
  248.  
  249. function Star:touched(touch)
  250. end
Advertisement
Add Comment
Please, Sign In to add comment