SHARE
TWEET

Fly

a guest Aug 9th, 2014 288 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. --# Main
  3. -- my first game
  4.  
  5. function setup()
  6.    
  7. -- there you can see you score
  8. parameter.watch("score")
  9. --start position
  10. px = 10
  11. py = 72
  12.     -----------------------------------------------------
  13.     -----------------------------------------------------
  14.  -- randomise our start position
  15. pox = math.random(1,10)+px
  16. poy = math.random(98,500)+py
  17.     -----------------------------------------------------
  18.     -----------------------------------------------------
  19. -- i pick button class from other project , because my attempts to create smaller and simplier button failed
  20. button = Button("")
  21. button.action = function () scores() end
  22.     -----------------------------------------------------
  23.     -----------------------------------------------------
  24. score = 0
  25. end
  26.  
  27.  
  28. function scores()
  29.     -- counts you succsessful taps
  30. if button.action then score = score + 1
  31. -- and some sounds
  32. sound(SOUND_PICKUP, 11321)
  33. end
  34. end
  35.  
  36.  
  37.  
  38.  
  39. function draw()
  40.     -- create beautifull background
  41. background(76, 157, 144, 255)
  42.     -----------------------------------------------------
  43.     -----------------------------------------------------
  44.     -- this move our fly
  45.     pox = pox + math.random(0.1,1)+score
  46.     poy = poy + math.random(-2.008,1)+score
  47.  
  48.     parameter.watch("DeltaTime")
  49.    
  50.     -----------------------------------------------------
  51.     -----------------------------------------------------
  52.    
  53.  -- i dont find more elegant way to make animation. sorry
  54.    if math.random(1,2)/2 == 1 then
  55.    sprite("Platformer Art:Battor Flap 1",pox,poy)
  56.    else
  57.    sprite("Platformer Art:Battor Flap 2",pox,poy)
  58.     end
  59.    
  60.     -----------------------------------------------------
  61.     -----------------------------------------------------
  62.    
  63.     if poy>HEIGHT then poy = 10
  64.     elseif pox>WIDTH then pox = 10
  65.     end
  66.  -- draw our invisible button
  67.     drawButton()
  68.    
  69.     -----------------------------------------------------
  70.     -----------------------------------------------------
  71.  
  72.  
  73. end
  74.  
  75. function drawButton()
  76.     -- move button with fly
  77.     button.pos = vec2(pox,poy)
  78.     button:draw()
  79. end
  80.  
  81. function touched(touch)
  82. button:touched(touch)
  83. end
  84. --# RoundRect
  85. function roundRect(x,y,w,h,r)
  86.     pushStyle()
  87.    
  88.     insetPos = vec2(x+r,y+r)
  89.     insetSize = vec2(w-2*r,h-2*r)
  90.    
  91.     --Copy fill into stroke
  92.     local red,green,blue,a = fill()
  93.     stroke(red,green,blue,a)
  94.    
  95.     noSmooth()
  96.     rectMode(CORNER)
  97.     rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
  98.    
  99.     if r > 0 then
  100.         smooth()
  101.         lineCapMode(ROUND)
  102.         strokeWidth(r*2)
  103.  
  104.         line(insetPos.x, insetPos.y,
  105.              insetPos.x + insetSize.x, insetPos.y)
  106.         line(insetPos.x, insetPos.y,
  107.              insetPos.x, insetPos.y + insetSize.y)
  108.         line(insetPos.x, insetPos.y + insetSize.y,
  109.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)
  110.         line(insetPos.x + insetSize.x, insetPos.y,
  111.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)            
  112.     end
  113.     popStyle()
  114. end
  115. --# Button
  116. Button = class()
  117.  
  118. function Button:init(displayName)
  119.     -- you can accept and set parameters here
  120.     self.displayName = displayName
  121.    
  122.     self.pos = vec2(0,0)
  123.     self.size = vec2(0,0)
  124.     self.action = nil
  125.    
  126.     self.color = color(113, 65, 190, 0)
  127. end
  128.  
  129. function Button:draw()
  130.     -- Codea does not automatically call this method
  131.     pushStyle()
  132.     -- because you mustnt see our button
  133.     noFill()
  134.     fill(self.color)
  135.    
  136.     font("ArialRoundedMTBold")
  137.     fontSize(22)
  138.    
  139.     -- use longest sound name for size
  140.     local w,h = textSize(self.displayName)
  141.     w = w + 20
  142.     h = h + 30
  143.    
  144.     roundRect(self.pos.x - w/2,
  145.               self.pos.y - h/2,
  146.               w,h,30)
  147.            
  148.     self.size = vec2(w,h)
  149.            
  150.     textMode(CENTER)
  151.     fill(54, 65, 96, 255)
  152.     text(self.displayName,self.pos.x+2,self.pos.y-2)
  153.     fill(255, 255, 255, 255)
  154.     text(self.displayName,self.pos.x,self.pos.y)
  155.    
  156.     popStyle()
  157. end
  158.  
  159. function Button:hit(p)
  160.     local l = self.pos.x - self.size.x/2
  161.     local r = self.pos.x + self.size.x/2
  162.     local t = self.pos.y + self.size.y/2
  163.     local b = self.pos.y - self.size.y/2
  164.     if p.x > l and p.x < r and
  165.        p.y > b and p.y < t then
  166.         return true
  167.     end
  168.    
  169.     return false
  170. end
  171.  
  172. function Button:touched(touch)
  173.     -- Codea does not automatically call this method
  174.     if touch.state == ENDED and
  175.        self:hit(vec2(touch.x,touch.y)) then
  176.         if self.action then
  177.             self.action()
  178.         end
  179.     end
  180. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top