Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Bounce
- -- a game about avoiding object's
- -- press screen to play, Enjoy!
- -- Put together by @warox
- -- Highscore added by @CodingIsLife
- displayMode(FULLSCREEN)
- supportedOrientations(LANDSCAPE_LEFT, LANDSCAPE_RIGHT)
- function setup()
- Setup:init()
- RoundRect()
- -- #Text #Color
- version = "1.2.0.0"
- name = "Bounce"
- textColor1 = color(255, 255, 255, 255)
- textColor2 = color(0, 0, 0, 255)
- -- #GAMESTATE
- gamestate = 2
- menu = 1
- play = 2
- gameover = 3
- -- #SETTINGS
- speed = 13
- score = 0
- -- Highscore not implemented
- highScore = readLocalData("highscore", 0)
- xw = w10
- yh = h50
- j = wmax
- r1 = 231
- g1 = 76
- b1 = 60
- a1 = 255
- -- #Objects
- defaultGravity = physics.gravity(0,0)
- physicsDraw = PhysicsDraw()
- player = createPlayer(xw, yh, -10, 10)
- wall = createWall(wmin,h95)
- wall2 = createWall(wmin, h05)
- ypos = HEIGHT*math.random(10,50) * .01
- tubetop = createTube(j, h85 - ypos, w05 ,hmax)
- tubebottom = createTube(j, h05 - h30 - ypos, w05 ,hmax)
- physicsDraw:addBody(player)
- physicsDraw:addBody(wall)
- physicsDraw:addBody(wall2)
- physicsDraw:addBody(tubetop)
- physicsDraw:addBody(tubebottom)
- end
- function info()
- fill(textColor2)
- fontSize(20)
- font("Vegur-Light")
- textMode(CORNER)
- text("Version: "..version, w05/2, h90)
- text("Project Name:"..name, w05/2, h85)
- end
- function createTube(x, y, w, h)
- local width = w or -10
- local height = h or 10
- local tube = physics.body(
- POLYGON,
- vec2(0, height),
- vec2(0, 0),
- vec2(width, 0),
- vec2(width, height)
- )
- tube.h = height
- tube.w = width
- tube.x = x
- tube.y = y
- tube.type = KINEMATIC
- tube.sleepingAllowed = false
- tube.info = "Tube"
- tube.interpolate = false
- return tube
- end
- function createWall(x, y)
- local wall = physics.body(
- POLYGON,
- vec2(0,20),
- vec2(0,0),
- vec2(WIDTH,0),
- vec2(WIDTH,20)
- )
- wall.x = x
- wall.y = y
- wall.type = STATIC
- return wall
- end
- function createPlayer(x, y, w, h)
- -- (x, y) are the centre co-ordinates of the player.
- -- (w, h) define the width and height of the box containing the player,
- -- if not specified they are -10 and 10 respectively.
- local width = w or -10
- local height = h or 10
- local playerBody = physics.body(
- POLYGON,
- vec2(width, height),
- vec2(width, width),
- vec2(height, width),
- vec2(height, height)
- )
- playerBody.x = x
- playerBody.y = y
- playerBody.type = DYNAMIC
- playerBody.sleepingAllowed = false
- playerBody.info = "player"
- playerBody.interpolate = false
- return playerBody
- end
- function draw()
- background(46, 46, 46, 255)
- -- #MENU
- if gamestate == menu then
- end
- -- #PLAY
- if gamestate == play then
- physicsDraw:draw()
- fill(255, 255, 255, 255)
- fontSize(50)
- text("Score: "..score, w50, h35)
- if player ~= nil then
- if CurrentTouch.state == BEGAN then
- player.y = player.y + 5
- else
- player.y = player.y - 5
- end
- elseif player == nil then
- return player
- end
- -- #Move Tube Top, Bottom in X
- if tubetop ~= nil then
- if tubetop.x <= WIDTH and tubetop.x >- w10 then
- tubetop.x = tubetop.x - speed
- print("Moving")
- else if tubetop.x <= w10 then
- tubetop.x = WIDTH
- ypos = HEIGHT*math.random(10,50) * .01
- tubetop.y = ypos + h10
- score = score + 1
- if(score == highScore + 1) then
- highScore = score
- saveLocalData("highscore", highScore)
- end
- end
- end
- if tubebottom.x <= WIDTH and tubebottom.x >- w10 then
- tubebottom.x = tubebottom.x - speed
- print("Moving")
- else if tubebottom.x <= w10 then
- tubebottom.x = WIDTH
- tubebottom.y = ypos - hmax
- end
- end
- elseif tubetop == nil then
- return tubetop, tubebottom
- end
- background(44, 62, 80,255)
- fill(r1, g1, b1, a1)
- roundRect(tubetop.x + 10, tubetop.y - 5, tubetop.w, HEIGHT, 20)
- roundRect(tubebottom.x + 10, tubebottom.y + 5, tubebottom.w, HEIGHT,20)
- rect(wall.x - 1, h95 - 10, WIDTH + 1, h10 - 10)
- rect(wall2.x - 1, hmin, WIDTH + 1, h10 - 10)
- ellipse(player.x,player.y, 40)
- fill(255,166,0,255)
- font("CourierNewPS-BoldMT")
- fontSize(20)
- text("Score: "..score, w90, h95)
- end
- -- #GAMEOVER
- if gamestate == gameover then
- background(r1, g1, b1, a1)
- fill(255, 255, 255, 255)
- textMode(CENTER)
- fontSize(100)
- font("CourierNewPSMT")
- text("GAMEOVER", w50, h70)
- fontSize(50)
- text("Score: "..score, w50, h35)
- text("Highscore: "..math.floor(highScore), w50, h20)
- fill(0, 0, 0, 255)
- fontSize(20)
- end
- info()
- end
- -- Collision Detection
- --
- -- This is simply a matter of checking whether one of the two
- -- colliding bodies are our ship.
- function collide(contact)
- if contact.bodyA == player or contact.bodyB == player then
- player.info = "destroyed"
- player = nil
- gamestate = gameover
- end
- end
- --# PhysicsDraw
- --# PhysicsDraw
- PhysicsDraw = class()
- -- Simplified Physics Debug Draw class from the Physics Lab
- -- example in Codea.
- function PhysicsDraw:init()
- self.bodies = {}
- self.contacts = {}
- end
- function PhysicsDraw:addBody(body)
- table.insert(self.bodies,body)
- end
- function PhysicsDraw:draw()
- pushStyle()
- smooth()
- noFill()
- for i,body in ipairs(self.bodies) do
- pushMatrix()
- translate(body.x, body.y)
- rotate(body.angle)
- if body.type == STATIC then
- stroke(255,255,255,255)
- elseif body.type == DYNAMIC and body.info ~= "debris" then
- stroke(150,255,150,255)
- else
- stroke(150,150,255,255)
- end
- if body.shapeType == POLYGON and body.info ~= "destroyed" then
- strokeWidth(3.0)
- local points = body.points
- for j = 1,#points do
- a = points[j]
- b = points[(j % #points)+1]
- line(a.x, a.y, b.x, b.y)
- end
- elseif body.shapeType == CHAIN or body.shapeType == EDGE then
- strokeWidth(3.0)
- local points = body.points
- for j = 1,#points-1 do
- a = points[j]
- b = points[j+1]
- line(a.x, a.y, b.x, b.y)
- end
- elseif body.shapeType == CIRCLE then
- strokeWidth(3.0)
- line(0,0,body.radius-3,0)
- ellipse(0,0,body.radius*2)
- end
- popMatrix()
- end
- stroke(255, 0, 0, 255)
- fill(255, 0, 0, 255)
- for k,v in pairs(self.contacts) do
- for m,n in ipairs(v.points) do
- ellipse(n.x, n.y, 10, 10)
- end
- end
- popStyle()
- end
- function PhysicsDraw:collide(contact)
- if contact.state == BEGAN then
- self.contacts[contact.id] = contact
- sound(SOUND_HIT, 2643)
- elseif contact.state == MOVING then
- self.contacts[contact.id] = contact
- elseif contact.state == ENDED then
- self.contacts[contact.id] = nil
- end
- end
- --# RoundRect
- RoundRect = class()
- function roundRect(x,y,w,h,r)
- -- [[ 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.]]
- pushStyle()
- -- [[ 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. ]]
- insetPos = vec2(x+r,y+r)
- insetSize = vec2(w-2*r,h-2*r)
- -- Copy fill into stroke
- -- [[ 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.]]
- local red,green,blue,a = fill()
- stroke(red,green,blue,a)
- -- [[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. ]]
- noSmooth()
- rectMode(CORNER)
- rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
- if r > 0 then
- -- [[ 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. ]]
- smooth()
- lineCapMode(ROUND)
- strokeWidth(r*2)
- line(insetPos.x, insetPos.y,
- insetPos.x + insetSize.x, insetPos.y)
- line(insetPos.x, insetPos.y,
- insetPos.x, insetPos.y + insetSize.y)
- line(insetPos.x, insetPos.y + insetSize.y,
- insetPos.x + insetSize.x, insetPos.y + insetSize.y)
- line(insetPos.x + insetSize.x, insetPos.y,
- insetPos.x + insetSize.x, insetPos.y + insetSize.y)
- end
- popStyle()
- end
- --# Setup
- Setup = class()
- -- Setup Class to define height and width!
- function Setup:init(x)
- -- Width Scale
- wmax = WIDTH * 1.0 -- Set's Max Width
- wmin = WIDTH * 0.0 -- Set's Min Width
- w95 = WIDTH * .95
- w90 = WIDTH * .90
- w85 = WIDTH * .85
- w80 = WIDTH * .80
- w75 = WIDTH * .75
- w70 = WIDTH * .70
- w65 = WIDTH * .65
- w60 = WIDTH * .60
- w55 = WIDTH * .55
- w50 = WIDTH * .50
- w45 = WIDTH * .45
- w40 = WIDTH * .40
- w35 = WIDTH * .35
- w30 = WIDTH * .30
- w25 = WIDTH * .25
- w20 = WIDTH * .20
- w15 = WIDTH * .15
- w10 = WIDTH * .10
- w05 = WIDTH * .05
- -- Height Scale
- hmax = HEIGHT * 1.0 -- Set's Max Height
- hmin = HEIGHT * 0.0 -- Set's Min Height
- h95 = HEIGHT * .95
- h90 = HEIGHT * .90
- h85 = HEIGHT * .85
- h80 = HEIGHT * .80
- h75 = HEIGHT * .75
- h70 = HEIGHT * .70
- h65 = HEIGHT * .65
- h60 = HEIGHT * .60
- h55 = HEIGHT * .55
- h50 = HEIGHT * .50
- h45 = HEIGHT * .45
- h40 = HEIGHT * .40
- h35 = HEIGHT * .35
- h30 = HEIGHT * .30
- h25 = HEIGHT * .25
- h20 = HEIGHT * .20
- h15 = HEIGHT * .15
- h10 = HEIGHT * .10
- h05 = HEIGHT * .05
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement