Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- my first game
- function setup()
- -- there you can see you score
- parameter.watch("score")
- --start position
- px = 10
- py = 72
- -----------------------------------------------------
- -----------------------------------------------------
- -- randomise our start position
- pox = math.random(1,10)+px
- poy = math.random(98,500)+py
- -----------------------------------------------------
- -----------------------------------------------------
- -- i pick button class from other project , because my attempts to create smaller and simplier button failed
- button = Button("")
- button.action = function () scores() end
- -----------------------------------------------------
- -----------------------------------------------------
- score = 0
- end
- function scores()
- -- counts you succsessful taps
- if button.action then score = score + 1
- -- and some sounds
- sound(SOUND_PICKUP, 11321)
- end
- end
- function draw()
- -- create beautifull background
- background(76, 157, 144, 255)
- -----------------------------------------------------
- -----------------------------------------------------
- -- this move our fly
- pox = pox + math.random(0.1,1)+score
- poy = poy + math.random(-2.008,1)+score
- parameter.watch("DeltaTime")
- -----------------------------------------------------
- -----------------------------------------------------
- -- i dont find more elegant way to make animation. sorry
- if math.random(1,2)/2 == 1 then
- sprite("Platformer Art:Battor Flap 1",pox,poy)
- else
- sprite("Platformer Art:Battor Flap 2",pox,poy)
- end
- -----------------------------------------------------
- -----------------------------------------------------
- if poy>HEIGHT then poy = 10
- elseif pox>WIDTH then pox = 10
- end
- -- draw our invisible button
- drawButton()
- -----------------------------------------------------
- -----------------------------------------------------
- end
- function drawButton()
- -- move button with fly
- button.pos = vec2(pox,poy)
- button:draw()
- end
- function touched(touch)
- button:touched(touch)
- end
- --# RoundRect
- function roundRect(x,y,w,h,r)
- pushStyle()
- insetPos = vec2(x+r,y+r)
- insetSize = vec2(w-2*r,h-2*r)
- --Copy fill into stroke
- local red,green,blue,a = fill()
- stroke(red,green,blue,a)
- noSmooth()
- rectMode(CORNER)
- rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)
- if r > 0 then
- 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
- --# Button
- Button = class()
- function Button:init(displayName)
- -- you can accept and set parameters here
- self.displayName = displayName
- self.pos = vec2(0,0)
- self.size = vec2(0,0)
- self.action = nil
- self.color = color(113, 65, 190, 0)
- end
- function Button:draw()
- -- Codea does not automatically call this method
- pushStyle()
- -- because you mustnt see our button
- noFill()
- fill(self.color)
- font("ArialRoundedMTBold")
- fontSize(22)
- -- use longest sound name for size
- local w,h = textSize(self.displayName)
- w = w + 20
- h = h + 30
- roundRect(self.pos.x - w/2,
- self.pos.y - h/2,
- w,h,30)
- self.size = vec2(w,h)
- textMode(CENTER)
- fill(54, 65, 96, 255)
- text(self.displayName,self.pos.x+2,self.pos.y-2)
- fill(255, 255, 255, 255)
- text(self.displayName,self.pos.x,self.pos.y)
- popStyle()
- end
- function Button:hit(p)
- local l = self.pos.x - self.size.x/2
- local r = self.pos.x + self.size.x/2
- local t = self.pos.y + self.size.y/2
- local b = self.pos.y - self.size.y/2
- if p.x > l and p.x < r and
- p.y > b and p.y < t then
- return true
- end
- return false
- end
- function Button:touched(touch)
- -- Codea does not automatically call this method
- if touch.state == ENDED and
- self:hit(vec2(touch.x,touch.y)) then
- if self.action then
- self.action()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement