Advertisement
Guest User

Bounce

a guest
Feb 20th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.46 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Bounce
  4. -- a game about avoiding object's
  5. -- press screen to play, Enjoy!
  6.  
  7. -- Put together by @warox
  8. -- Highscore added by @CodingIsLife
  9.  
  10. displayMode(FULLSCREEN)
  11. supportedOrientations(LANDSCAPE_LEFT, LANDSCAPE_RIGHT)
  12.  
  13. function setup()
  14.     Setup:init()
  15.     RoundRect()
  16.    
  17.     -- #Text #Color
  18.     version = "1.2.0.0"
  19.     name = "Bounce"
  20.     textColor1 = color(255, 255, 255, 255)
  21.     textColor2 = color(0, 0, 0, 255)
  22.    
  23.     -- #GAMESTATE
  24.     gamestate = 2
  25.     menu = 1
  26.     play = 2
  27.     gameover = 3
  28.    
  29.     -- #SETTINGS
  30.     speed = 13
  31.     score = 0
  32.     -- Highscore not implemented
  33.     highScore = readLocalData("highscore", 0)
  34.     xw = w10
  35.     yh = h50
  36.    
  37.     j = wmax
  38.    
  39.     r1 = 231
  40.     g1 = 76
  41.     b1 = 60
  42.     a1 = 255
  43.     -- #Objects
  44.    
  45.     defaultGravity = physics.gravity(0,0)
  46.     physicsDraw = PhysicsDraw()
  47.    
  48.     player = createPlayer(xw, yh, -10, 10)
  49.     wall = createWall(wmin,h95)
  50.     wall2 = createWall(wmin, h05)
  51.    
  52.     ypos = HEIGHT*math.random(10,50) * .01
  53.    
  54.     tubetop = createTube(j, h85 - ypos, w05 ,hmax)
  55.     tubebottom = createTube(j, h05 - h30 - ypos, w05 ,hmax)
  56.    
  57.    
  58.     physicsDraw:addBody(player)
  59.     physicsDraw:addBody(wall)
  60.     physicsDraw:addBody(wall2)
  61.     physicsDraw:addBody(tubetop)
  62.     physicsDraw:addBody(tubebottom)    
  63. end
  64.  
  65. function info()
  66.     fill(textColor2)
  67.     fontSize(20)
  68.     font("Vegur-Light")
  69.     textMode(CORNER)
  70.     text("Version: "..version, w05/2, h90)
  71.     text("Project Name:"..name, w05/2, h85)
  72. end
  73.  
  74. function createTube(x, y, w, h)
  75.     local width = w or -10
  76.     local height = h or 10
  77.    
  78.     local tube = physics.body(
  79.     POLYGON,
  80.     vec2(0, height),
  81.     vec2(0, 0),
  82.     vec2(width, 0),
  83.     vec2(width, height)
  84.     )
  85.    
  86.     tube.h = height
  87.     tube.w = width
  88.     tube.x = x
  89.     tube.y = y
  90.     tube.type = KINEMATIC
  91.     tube.sleepingAllowed = false
  92.     tube.info = "Tube"
  93.     tube.interpolate = false
  94.     return tube
  95. end
  96.  
  97. function createWall(x, y)
  98.     local wall = physics.body(
  99.     POLYGON,
  100.     vec2(0,20),
  101.     vec2(0,0),
  102.     vec2(WIDTH,0),
  103.     vec2(WIDTH,20)
  104.     )
  105.    
  106.     wall.x = x
  107.     wall.y = y
  108.     wall.type = STATIC
  109.     return wall
  110. end
  111.  
  112. function createPlayer(x, y, w, h)
  113.  
  114.    -- (x, y) are the centre co-ordinates of the player.
  115.    -- (w, h) define the width and height of the box containing the player,
  116.    -- if not specified they are -10 and 10 respectively.
  117.     local width = w or -10
  118.     local height = h or 10
  119.    
  120.     local playerBody = physics.body(
  121.     POLYGON,    
  122.     vec2(width, height),
  123.     vec2(width, width),
  124.     vec2(height, width),
  125.     vec2(height, height)
  126.     )
  127.     playerBody.x = x
  128.     playerBody.y = y
  129.     playerBody.type = DYNAMIC
  130.     playerBody.sleepingAllowed = false
  131.     playerBody.info = "player"
  132.     playerBody.interpolate = false
  133.     return playerBody
  134. end
  135.  
  136.  
  137. function draw()
  138.     background(46, 46, 46, 255)
  139.  
  140.     -- #MENU
  141.     if gamestate == menu then
  142.  
  143.     end
  144.    
  145.     -- #PLAY
  146.     if gamestate == play then
  147.         physicsDraw:draw()
  148.         fill(255, 255, 255, 255)
  149.         fontSize(50)
  150.         text("Score: "..score, w50, h35)
  151.  
  152.         if player ~= nil then
  153.             if CurrentTouch.state == BEGAN then
  154.                 player.y = player.y + 5
  155.             else
  156.                 player.y = player.y - 5
  157.             end
  158.         elseif player == nil then
  159.             return player
  160.        
  161.         end
  162.        
  163.        
  164.         -- #Move Tube Top, Bottom in X
  165.         if tubetop ~= nil then
  166.             if tubetop.x <= WIDTH and tubetop.x >- w10 then
  167.                 tubetop.x = tubetop.x - speed
  168.                 print("Moving")
  169.                 else if tubetop.x <= w10 then
  170.                     tubetop.x = WIDTH
  171.                     ypos = HEIGHT*math.random(10,50) * .01
  172.                     tubetop.y = ypos + h10
  173.                    
  174.                     score = score + 1
  175.                     if(score == highScore + 1) then
  176.                         highScore = score
  177.                         saveLocalData("highscore", highScore)
  178.                     end
  179.                 end  
  180.             end
  181.             if tubebottom.x <= WIDTH and tubebottom.x >- w10 then
  182.                 tubebottom.x = tubebottom.x - speed
  183.                 print("Moving")
  184.                 else if tubebottom.x <= w10 then
  185.                     tubebottom.x = WIDTH
  186.                     tubebottom.y = ypos - hmax
  187.                 end
  188.             end
  189.         elseif tubetop == nil then
  190.             return tubetop, tubebottom
  191.         end
  192.        
  193.         background(44, 62, 80,255)
  194.         fill(r1, g1, b1, a1)
  195.        
  196.         roundRect(tubetop.x + 10, tubetop.y - 5, tubetop.w, HEIGHT, 20)
  197.         roundRect(tubebottom.x + 10, tubebottom.y + 5, tubebottom.w, HEIGHT,20)
  198.         rect(wall.x - 1, h95 - 10, WIDTH + 1, h10 - 10)
  199.         rect(wall2.x - 1, hmin, WIDTH + 1, h10 - 10)
  200.         ellipse(player.x,player.y, 40)
  201.        
  202.         fill(255,166,0,255)
  203.         font("CourierNewPS-BoldMT")
  204.         fontSize(20)
  205.         text("Score: "..score, w90, h95)
  206.  
  207.     end
  208.    
  209.     -- #GAMEOVER
  210.     if gamestate == gameover then
  211.         background(r1, g1, b1, a1)
  212.         fill(255, 255, 255, 255)
  213.        
  214.         textMode(CENTER)
  215.         fontSize(100)
  216.         font("CourierNewPSMT")
  217.         text("GAMEOVER", w50, h70)
  218.         fontSize(50)
  219.         text("Score: "..score, w50, h35)
  220.         text("Highscore: "..math.floor(highScore), w50, h20)
  221.         fill(0, 0, 0, 255)
  222.         fontSize(20)
  223.     end
  224.    
  225.     info()
  226. end
  227. -- Collision Detection
  228. --
  229. -- This is simply a matter of checking whether one of the two
  230. -- colliding bodies are our ship.
  231.  
  232. function collide(contact)
  233.  
  234.     if contact.bodyA == player or contact.bodyB == player then
  235.         player.info = "destroyed"
  236.         player = nil
  237.         gamestate = gameover
  238.     end
  239. end
  240.  
  241.    
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248. --# PhysicsDraw
  249. --# PhysicsDraw
  250. PhysicsDraw = class()
  251.  
  252. -- Simplified Physics Debug Draw class from the Physics Lab
  253. -- example in Codea.
  254.  
  255. function PhysicsDraw:init()
  256.    self.bodies = {}
  257.    self.contacts = {}
  258. end
  259.  
  260. function PhysicsDraw:addBody(body)
  261.    table.insert(self.bodies,body)
  262. end
  263.  
  264. function PhysicsDraw:draw()
  265.  
  266.    pushStyle()
  267.    smooth()
  268.    noFill()
  269.  
  270.    for i,body in ipairs(self.bodies) do
  271.  
  272.        pushMatrix()
  273.        translate(body.x, body.y)
  274.        rotate(body.angle)
  275.  
  276.        if body.type == STATIC then
  277.            stroke(255,255,255,255)
  278.        elseif body.type == DYNAMIC and body.info ~= "debris" then
  279.            stroke(150,255,150,255)
  280.        else
  281.            stroke(150,150,255,255)
  282.        end
  283.  
  284.        if body.shapeType == POLYGON and body.info ~= "destroyed" then
  285.            strokeWidth(3.0)
  286.            local points = body.points
  287.            for j = 1,#points do
  288.                a = points[j]
  289.                b = points[(j % #points)+1]
  290.                line(a.x, a.y, b.x, b.y)
  291.            end
  292.        elseif body.shapeType == CHAIN or body.shapeType == EDGE then
  293.            strokeWidth(3.0)
  294.            local points = body.points
  295.            for j = 1,#points-1 do
  296.                a = points[j]
  297.                b = points[j+1]
  298.                line(a.x, a.y, b.x, b.y)
  299.            end      
  300.        elseif body.shapeType == CIRCLE then
  301.            strokeWidth(3.0)
  302.            line(0,0,body.radius-3,0)            
  303.            ellipse(0,0,body.radius*2)
  304.        end
  305.  
  306.        popMatrix()
  307.    end
  308.  
  309.    stroke(255, 0, 0, 255)
  310.    fill(255, 0, 0, 255)
  311.  
  312.    for k,v in pairs(self.contacts) do
  313.        for m,n in ipairs(v.points) do
  314.            ellipse(n.x, n.y, 10, 10)
  315.        end
  316.    end
  317.  
  318.    popStyle()
  319. end
  320.  
  321. function PhysicsDraw:collide(contact)
  322.    if contact.state == BEGAN then
  323.        self.contacts[contact.id] = contact
  324.        sound(SOUND_HIT, 2643)
  325.    elseif contact.state == MOVING then
  326.        self.contacts[contact.id] = contact
  327.    elseif contact.state == ENDED then
  328.        self.contacts[contact.id] = nil
  329.    end
  330. end
  331.  
  332. --# RoundRect
  333. RoundRect = class()
  334.  
  335. function roundRect(x,y,w,h,r)
  336.  
  337. -- [[ 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.]]
  338.  
  339.     pushStyle()
  340.  
  341. -- [[ 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. ]]
  342.    
  343.     insetPos = vec2(x+r,y+r)
  344.     insetSize = vec2(w-2*r,h-2*r)
  345.    
  346. -- Copy fill into stroke
  347.  
  348. -- [[ 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.]]
  349.  
  350.     local red,green,blue,a = fill()
  351.     stroke(red,green,blue,a)
  352.    
  353. -- [[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. ]]
  354.  
  355.     noSmooth()
  356.     rectMode(CORNER)
  357.     rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
  358.    
  359.     if r > 0 then
  360.  
  361. -- [[ 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. ]]
  362.  
  363.         smooth()
  364.         lineCapMode(ROUND)
  365.         strokeWidth(r*2)
  366.  
  367.         line(insetPos.x, insetPos.y,
  368.              insetPos.x + insetSize.x, insetPos.y)
  369.         line(insetPos.x, insetPos.y,
  370.              insetPos.x, insetPos.y + insetSize.y)
  371.         line(insetPos.x, insetPos.y + insetSize.y,
  372.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)
  373.         line(insetPos.x + insetSize.x, insetPos.y,
  374.              insetPos.x + insetSize.x, insetPos.y + insetSize.y)            
  375.     end
  376.  
  377.     popStyle()
  378.  
  379. end
  380.  
  381.  
  382. --# Setup
  383. Setup = class()
  384. -- Setup Class to define height and width!
  385. function Setup:init(x)
  386.    
  387.     -- Width Scale
  388.     wmax = WIDTH * 1.0 -- Set's Max Width
  389.     wmin = WIDTH * 0.0 -- Set's Min Width
  390.    
  391.     w95 = WIDTH * .95
  392.     w90 = WIDTH * .90
  393.     w85 = WIDTH * .85
  394.     w80 = WIDTH * .80
  395.     w75 = WIDTH * .75
  396.     w70 = WIDTH * .70
  397.     w65 = WIDTH * .65
  398.     w60 = WIDTH * .60
  399.     w55 = WIDTH * .55
  400.     w50 = WIDTH * .50
  401.     w45 = WIDTH * .45
  402.     w40 = WIDTH * .40
  403.     w35 = WIDTH * .35
  404.     w30 = WIDTH * .30
  405.     w25 = WIDTH * .25
  406.     w20 = WIDTH * .20
  407.     w15 = WIDTH * .15    
  408.     w10 = WIDTH * .10
  409.     w05 = WIDTH * .05
  410.  
  411.     -- Height Scale
  412.     hmax = HEIGHT * 1.0 -- Set's Max Height
  413.     hmin = HEIGHT * 0.0 -- Set's Min Height
  414.    
  415.     h95 = HEIGHT * .95
  416.     h90 = HEIGHT * .90
  417.     h85 = HEIGHT * .85
  418.     h80 = HEIGHT * .80
  419.     h75 = HEIGHT * .75
  420.     h70 = HEIGHT * .70
  421.     h65 = HEIGHT * .65
  422.     h60 = HEIGHT * .60
  423.     h55 = HEIGHT * .55
  424.     h50 = HEIGHT * .50
  425.     h45 = HEIGHT * .45
  426.     h40 = HEIGHT * .40
  427.     h35 = HEIGHT * .35
  428.     h30 = HEIGHT * .30
  429.     h25 = HEIGHT * .25
  430.     h20 = HEIGHT * .20
  431.     h15 = HEIGHT * .15    
  432.     h10 = HEIGHT * .10
  433.     h05 = HEIGHT * .05
  434. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement