SHARE
TWEET

Bounce Codea

a guest Feb 12th, 2017 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Bounce
  2.  
  3. Main = class()
  4.  
  5. function setup()
  6.     displayMode(FULLSCREEN)
  7.     supportedOrientations(LANDSCAPE_ANY)
  8.    
  9.     Setup:init()
  10.     RoundRect()
  11.    
  12.     i = h50 --Object's Initial Height
  13.     j = WIDTH --Tube's Initial Width
  14.     n = WIDTH
  15.     p2 = WIDTH
  16.     print(score)
  17.     tubeHeightBottom = HEIGHT * math.random(10,50) * .01
  18.     tubeHeightTop = h60 - tubeHeightBottom
  19.    
  20.     --Game state
  21.     menu = 1
  22.     play = 2
  23.     gameover = 3
  24.     state = menu
  25.     -- Change value for game speed
  26.     speed = 12
  27.     -- Game Score
  28.     score = 0
  29.     -- Highscore not implemented
  30.     --highScore = 0
  31.    
  32.     --Colors
  33.     r1 = 30
  34.     g1 = 60
  35.     b1 = 110
  36.     a1 = 255
  37.    
  38.     --Background particle
  39.     particle = image(WIDTH*2,HEIGHT*2)
  40.    
  41.     setContext(particle)
  42.  
  43.  
  44.     for p = 1, 400 do
  45.         fill(math.random(100, 230), 20, 101, 255)
  46.         roundRect(math.random(WIDTH * 2),math.random(HEIGHT * 2),5 , 5, 5)
  47.     end
  48.     setContext()
  49. end
  50.  
  51. function draw()  
  52.     background(21, 37, 43, 255)
  53.  
  54.     for p = 1,30 do
  55.         sprite(particle,0,0)
  56.     end
  57.    
  58.     resetMatrix()
  59.    
  60.     if r1 > 0 then
  61.         r1 = i - 190
  62.     end
  63.    
  64.     --Menu
  65.     if state == menu then
  66.         fill(r1, g1, b1, a1)
  67.         rect(0, 0, WIDTH, h10 + 15)
  68.         rect(0, h90, WIDTH, h10 )
  69.        
  70.         fill(255, 255, 255, 255)
  71.         ellipse(w15, i, w15)
  72.         text("Tap to play", w50, h50)
  73.         fontSize(70)
  74.         font("ArialRoundedMTBold")
  75.        
  76.         if CurrentTouch.state == BEGAN then
  77.             state = play
  78.         end
  79.     end
  80.    
  81.    
  82.     --Playing
  83.     if state == play then
  84.         fill(r1, g1, b1, a1)
  85.         rect(0, 0, WIDTH, h10 + 15)
  86.         rect(0, h90, WIDTH, h10 )
  87.         --Tube Color
  88.         fill(r1, g1, b1, a1)
  89.         --Tube
  90.         if j <= WIDTH and j >- w10 then
  91.             rectMode(CORNER)
  92.             --Tube: #Bottom
  93.             --roundRect(x,y,w,h,r)
  94.             --rect(x,y,w,h)
  95.             roundRect(j, h10, w10, tubeHeightBottom, 15)
  96.             --Tube: #Top
  97.             roundRect(j, h10 + tubeHeightBottom + h30, w10,tubeHeightTop, 15)
  98.             --Speed:
  99.             j = j - speed
  100.             if j <=- w10 then
  101.                 j = WIDTH
  102.                 score = score + 1
  103.                 tubeHeightBottom = HEIGHT * math.random(10,50) * .01
  104.                 tubeHeightTop = h60 - tubeHeightBottom
  105.             end
  106.         end
  107.        
  108.  
  109.        
  110.         if i > h10 then
  111.             --Flight
  112.             if CurrentTouch.state == BEGAN and i< HEIGHT then
  113.                 i = i + 10
  114.  
  115.                 ellipse(w15, i, w20 - 10)
  116.             end
  117.            
  118.             --Drawing
  119.             fill(r1 + 20, g1 + 20, b1 + 20, a1)
  120.             ellipse(w15, i, w15)
  121.             --Gravity
  122.             i = i - 5
  123.         elseif i <= h20 then
  124.             --Lands on ground
  125.             fill(255, 255, 255, 255)
  126.  
  127.             ellipse(w15, i, w15)
  128.             state = gameover
  129.         end
  130.    
  131.    
  132.         fill(255, 255, 255, 255)
  133.         fontSize(50)
  134.         font("ArialRoundedMTBold")
  135.         text(score, w90, h90)
  136.         if j <= w25 and j >= w10 then
  137.             if i >= h10 and i <= tubeHeightBottom + w10 then
  138.                 state = gameover
  139.             elseif i >= HEIGHT - tubeHeightTop then
  140.                 state = gameover
  141.             end
  142.         end
  143.     end
  144.    
  145.     --Gameover
  146.     if state==gameover then
  147.         background(58, 112, 221, 255)
  148.         fill(255, 255, 255, 255)
  149.         fontSize(100)
  150.         font("ArialRoundedMTBold")
  151.         text("GAMEOVER", w50, h50)
  152.         text(score, w50, h20)
  153.     end
  154. end
  155.  
  156. RoundRect = class()
  157.  
  158. function roundRect(x,y,w,h,r)
  159.  
  160. -- [[ pushStyle() saves the current graphic styles like stroke, width, etc. You can then do your thing and call popStyle at the end to return to this state.]]
  161.  
  162.     pushStyle()
  163.  
  164. -- [[ insetPos and insetSize contain the co-ordinates for the internal "fill" rectangle. InsetPos.x = x, insetPos.y = y, insetSize.x = w and insetSize.y = h. In effect this creates a rectangle that is smaller than a factor of "r" within the rectangle co-ordinates specified in roundRect. ]]
  165.    
  166.     insetPos = vec2(x+r,y+r)
  167.     insetSize = vec2(w-2*r,h-2*r)
  168.    
  169. -- Copy fill into stroke
  170.  
  171. -- [[ Since Codea 1.3 you can retrieve the style information from all style functions by calling them without arguments. This way you only have to set the fill style once as you would for the normal rectangle function. You can read all about how this rounded rectangle function evolved on the Codea Forums.]]
  172.  
  173.     local red,green,blue,a = fill()
  174.     stroke(red,green,blue,a)
  175.    
  176. -- [[noSmooth() will disable smooth (unaliased) line drawing. It is useful for drawing thin lines. This initial rectangle is used to fill in the centre of your rounded rectangle, it has the usual 90 degree corners. Four lines are then drawn around this to give the rounded corner look. You can see this yourself by commenting out the 4 lines drawn below. ]]
  177.  
  178.     noSmooth()
  179.     rectMode(CORNER)
  180.     rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
  181.    
  182.     if r > 0 then
  183.  
  184. -- [[ You have to use smooth() if you want to use the ROUND option for lineCapMode. Four lines are now drawn around the filler rectangle. One on each edge. Because the lines have rounded ends when you overlap them it makes the corners look rounded, albeit a bit like a ball if you get the proportions wrong. Each of the lines are twice the width of the corner radius. ]]
  185.  
  186.         smooth()
  187.         lineCapMode(ROUND)
  188.         strokeWidth(r*2)
  189.  
  190.         line(insetPos.x, insetPos.y,
  191.              insetPos.x + insetSize.x, insetPos.y)
  192.         line(insetPos.x, insetPos.y,
  193.              insetPos.x, insetPos.y + insetSize.y)
  194.         line(insetPos.x, insetPos.y + insetSize.y,
  195.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)
  196.         line(insetPos.x + insetSize.x, insetPos.y,
  197.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)            
  198.     end
  199.  
  200.     popStyle()
  201.  
  202. end
  203.  
  204. Setup = class()
  205.  
  206. function Setup:init(x)
  207.    
  208.     -- Width Scale
  209.    
  210.     w95=WIDTH* .95
  211.     w90=WIDTH* .9
  212.     w85=WIDTH* .85
  213.     w80=WIDTH* .8
  214.     w75=WIDTH* .75
  215.     w70=WIDTH* .7
  216.     w65=WIDTH* .65
  217.     w60=WIDTH* .6
  218.     w55=WIDTH* .55
  219.     w50=WIDTH* .5
  220.     w45=WIDTH* .45
  221.     w40=WIDTH* .4
  222.     w35=WIDTH* .35
  223.     w30=WIDTH* .3
  224.     w25=WIDTH* .25
  225.     w20=WIDTH* .2
  226.     w15=WIDTH* .15    
  227.     w10=WIDTH* .1
  228.    
  229.     -- Height Scale
  230.    
  231.     h95=HEIGHT* .95
  232.     h90=HEIGHT* .9
  233.     h85=HEIGHT* .85
  234.     h80=HEIGHT* .8
  235.     h75=HEIGHT* .75
  236.     h70=HEIGHT* .7
  237.     h65=HEIGHT* .65
  238.     h60=HEIGHT* .6
  239.     h55=HEIGHT* .55
  240.     h50=HEIGHT* .5
  241.     h45=HEIGHT* .45
  242.     h40=HEIGHT* .4
  243.     h35=HEIGHT* .35
  244.     h30=HEIGHT* .3
  245.     h25=HEIGHT* .25
  246.     h20=HEIGHT* .2
  247.     h15=HEIGHT* .15    
  248.     h10=HEIGHT* .1
  249. 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