SHARE
TWEET

Bounce Codea w/ Highscore

a guest Feb 12th, 2017 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Bounce
  2.  
  3.  
  4.  
  5. Main = class()
  6.  
  7.  
  8.  
  9. function setup()
  10.  
  11.     displayMode(FULLSCREEN)
  12.  
  13.     supportedOrientations(LANDSCAPE_ANY)
  14.  
  15.    
  16.  
  17.     Setup:init()
  18.  
  19.     RoundRect()
  20.  
  21.    
  22.  
  23.     i = h50 --Object's Initial Height
  24.  
  25.     j = WIDTH --Tube's Initial Width
  26.  
  27.     n = WIDTH
  28.  
  29.     p2 = WIDTH
  30.  
  31.     print(score)
  32.  
  33.     tubeHeightBottom = HEIGHT * math.random(10,50) * .01
  34.  
  35.     tubeHeightTop = h60 - tubeHeightBottom
  36.  
  37.    
  38.  
  39.     --Game state
  40.  
  41.     menu = 1
  42.  
  43.     play = 2
  44.  
  45.     gameover = 3
  46.  
  47.     state = menu
  48.  
  49.     -- Change value for game speed
  50.  
  51.     speed = 12
  52.  
  53.     -- Game Score
  54.  
  55.     score = 0
  56.  
  57.     -- Highscore
  58.  
  59.     highScore = readLocalData("highscore", 0)
  60.    
  61.  
  62.     --Colors
  63.  
  64.     r1 = 30
  65.  
  66.     g1 = 60
  67.  
  68.     b1 = 110
  69.  
  70.     a1 = 255
  71.  
  72.    
  73.  
  74.     --Background particle
  75.  
  76.     particle = image(WIDTH*2,HEIGHT*2)
  77.  
  78.    
  79.  
  80.     setContext(particle)
  81.  
  82.  
  83.  
  84.  
  85.  
  86.     for p = 1, 400 do
  87.  
  88.         fill(math.random(100, 230), 20, 101, 255)
  89.  
  90.         roundRect(math.random(WIDTH * 2),math.random(HEIGHT * 2),5 , 5, 5)
  91.  
  92.     end
  93.  
  94.     setContext()
  95.  
  96. end
  97.  
  98.  
  99.  
  100. function draw()  
  101.  
  102.     background(21, 37, 43, 255)
  103.  
  104.  
  105.  
  106.     for p = 1,30 do
  107.  
  108.         sprite(particle,0,0)
  109.  
  110.     end
  111.  
  112.    
  113.  
  114.     resetMatrix()
  115.  
  116.    
  117.  
  118.     if r1 > 0 then
  119.  
  120.         r1 = i - 190
  121.  
  122.     end
  123.  
  124.    
  125.  
  126.     --Menu
  127.  
  128.     if state == menu then
  129.  
  130.         fill(r1, g1, b1, a1)
  131.  
  132.         rect(0, 0, WIDTH, h10 + 15)
  133.  
  134.         rect(0, h90, WIDTH, h10 )
  135.  
  136.        
  137.  
  138.         fill(255, 255, 255, 255)
  139.  
  140.         ellipse(w15, i, w15)
  141.  
  142.         text("Tap to play", w50, h50)
  143.  
  144.         fontSize(70)
  145.  
  146.         font("ArialRoundedMTBold")
  147.  
  148.        
  149.  
  150.         if CurrentTouch.state == BEGAN then
  151.  
  152.             state = play
  153.  
  154.         end
  155.  
  156.     end
  157.  
  158.    
  159.  
  160.    
  161.  
  162.     --Playing
  163.  
  164.     if state == play then
  165.  
  166.         fill(r1, g1, b1, a1)
  167.  
  168.         rect(0, 0, WIDTH, h10 + 15)
  169.  
  170.         rect(0, h90, WIDTH, h10 )
  171.  
  172.         --Tube Color
  173.  
  174.         fill(r1, g1, b1, a1)
  175.  
  176.         --Tube
  177.  
  178.         if j <= WIDTH and j >- w10 then
  179.  
  180.             rectMode(CORNER)
  181.  
  182.             --Tube: #Bottom
  183.  
  184.             --roundRect(x,y,w,h,r)
  185.  
  186.             --rect(x,y,w,h)
  187.  
  188.             roundRect(j, h10, w10, tubeHeightBottom, 15)
  189.  
  190.             --Tube: #Top
  191.  
  192.             roundRect(j, h10 + tubeHeightBottom + h30, w10,tubeHeightTop, 15)
  193.  
  194.             --Speed:
  195.  
  196.             j = j - speed
  197.  
  198.             if j <=- w10 then
  199.  
  200.                 j = WIDTH
  201.  
  202.                 score = score + 1
  203.                
  204.                 if(score == highScore + 1) then
  205.                     highScore = score
  206.                     saveLocalData("highscore", highScore)
  207.                 end
  208.  
  209.                 tubeHeightBottom = HEIGHT * math.random(10,50) * .01
  210.  
  211.                 tubeHeightTop = h60 - tubeHeightBottom
  212.  
  213.             end
  214.  
  215.         end
  216.  
  217.        
  218.  
  219.  
  220.  
  221.        
  222.  
  223.         if i > h10 then
  224.  
  225.             --Flight
  226.  
  227.             if CurrentTouch.state == BEGAN and i< HEIGHT then
  228.  
  229.                 i = i + 10
  230.  
  231.  
  232.  
  233.                 ellipse(w15, i, w20 - 10)
  234.  
  235.             end
  236.  
  237.            
  238.  
  239.             --Drawing
  240.  
  241.             fill(r1 + 20, g1 + 20, b1 + 20, a1)
  242.  
  243.             ellipse(w15, i, w15)
  244.  
  245.             --Gravity
  246.  
  247.             i = i - 5
  248.  
  249.         elseif i <= h20 then
  250.  
  251.             --Lands on ground
  252.  
  253.             fill(255, 255, 255, 255)
  254.  
  255.  
  256.  
  257.             ellipse(w15, i, w15)
  258.  
  259.             state = gameover
  260.  
  261.         end
  262.  
  263.    
  264.  
  265.    
  266.  
  267.         fill(255, 255, 255, 255)
  268.  
  269.         fontSize(50)
  270.  
  271.         font("ArialRoundedMTBold")
  272.  
  273.         text(score, w90, h90)
  274.  
  275.         if j <= w25 and j >= w10 then
  276.  
  277.             if i >= h10 and i <= tubeHeightBottom + w10 then
  278.  
  279.                 state = gameover
  280.  
  281.             elseif i >= HEIGHT - tubeHeightTop then
  282.  
  283.                 state = gameover
  284.  
  285.             end
  286.  
  287.         end
  288.  
  289.     end
  290.  
  291.    
  292.  
  293.     --Gameover
  294.  
  295.     if state==gameover then
  296.  
  297.         background(58, 112, 221, 255)
  298.  
  299.         fill(255, 255, 255, 255)
  300.  
  301.         fontSize(100)
  302.  
  303.         font("ArialRoundedMTBold")
  304.  
  305.         text("GAMEOVER", w50, h50)
  306.  
  307.         text("Score: "..score, w50, h30)
  308.         text("Highscore: "..math.floor(highScore), w50, h15)
  309.  
  310.     end
  311.  
  312. end
  313.  
  314.  
  315.  
  316. RoundRect = class()
  317.  
  318.  
  319.  
  320. function roundRect(x,y,w,h,r)
  321.  
  322.  
  323.  
  324. -- [[ 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.]]
  325.  
  326.  
  327.  
  328.     pushStyle()
  329.  
  330.  
  331.  
  332. -- [[ 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. ]]
  333.  
  334.    
  335.  
  336.     insetPos = vec2(x+r,y+r)
  337.  
  338.     insetSize = vec2(w-2*r,h-2*r)
  339.  
  340.    
  341.  
  342. -- Copy fill into stroke
  343.  
  344.  
  345.  
  346. -- [[ 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.]]
  347.  
  348.  
  349.  
  350.     local red,green,blue,a = fill()
  351.  
  352.     stroke(red,green,blue,a)
  353.  
  354.    
  355.  
  356. -- [[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. ]]
  357.  
  358.  
  359.  
  360.     noSmooth()
  361.  
  362.     rectMode(CORNER)
  363.  
  364.     rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
  365.  
  366.    
  367.  
  368.     if r > 0 then
  369.  
  370.  
  371.  
  372. -- [[ 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. ]]
  373.  
  374.  
  375.  
  376.         smooth()
  377.  
  378.         lineCapMode(ROUND)
  379.  
  380.         strokeWidth(r*2)
  381.  
  382.  
  383.  
  384.         line(insetPos.x, insetPos.y,
  385.  
  386.              insetPos.x + insetSize.x, insetPos.y)
  387.  
  388.         line(insetPos.x, insetPos.y,
  389.  
  390.              insetPos.x, insetPos.y + insetSize.y)
  391.  
  392.         line(insetPos.x, insetPos.y + insetSize.y,
  393.  
  394.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)
  395.  
  396.         line(insetPos.x + insetSize.x, insetPos.y,
  397.  
  398.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)            
  399.  
  400.     end
  401.  
  402.  
  403.  
  404.     popStyle()
  405.  
  406.  
  407.  
  408. end
  409.  
  410.  
  411.  
  412. Setup = class()
  413.  
  414.  
  415.  
  416. function Setup:init(x)
  417.  
  418.    
  419.  
  420.     -- Width Scale
  421.  
  422.    
  423.  
  424.     w95=WIDTH* .95
  425.  
  426.     w90=WIDTH* .9
  427.  
  428.     w85=WIDTH* .85
  429.  
  430.     w80=WIDTH* .8
  431.  
  432.     w75=WIDTH* .75
  433.  
  434.     w70=WIDTH* .7
  435.  
  436.     w65=WIDTH* .65
  437.  
  438.     w60=WIDTH* .6
  439.  
  440.     w55=WIDTH* .55
  441.  
  442.     w50=WIDTH* .5
  443.  
  444.     w45=WIDTH* .45
  445.  
  446.     w40=WIDTH* .4
  447.  
  448.     w35=WIDTH* .35
  449.  
  450.     w30=WIDTH* .3
  451.  
  452.     w25=WIDTH* .25
  453.  
  454.     w20=WIDTH* .2
  455.  
  456.     w15=WIDTH* .15    
  457.  
  458.     w10=WIDTH* .1
  459.  
  460.    
  461.  
  462.     -- Height Scale
  463.  
  464.    
  465.  
  466.     h95=HEIGHT* .95
  467.  
  468.     h90=HEIGHT* .9
  469.  
  470.     h85=HEIGHT* .85
  471.  
  472.     h80=HEIGHT* .8
  473.  
  474.     h75=HEIGHT* .75
  475.  
  476.     h70=HEIGHT* .7
  477.  
  478.     h65=HEIGHT* .65
  479.  
  480.     h60=HEIGHT* .6
  481.  
  482.     h55=HEIGHT* .55
  483.  
  484.     h50=HEIGHT* .5
  485.  
  486.     h45=HEIGHT* .45
  487.  
  488.     h40=HEIGHT* .4
  489.  
  490.     h35=HEIGHT* .35
  491.  
  492.     h30=HEIGHT* .3
  493.  
  494.     h25=HEIGHT* .25
  495.  
  496.     h20=HEIGHT* .2
  497.  
  498.     h15=HEIGHT* .15    
  499.  
  500.     h10=HEIGHT* .1
  501.  
  502. 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