Featured image of post 電腦遊戲設計導論 Lua 範例 20200507

電腦遊戲設計導論 Lua 範例 20200507

  • 背景移動

  • 動畫效果

  • 聲音效果

  • 文字顏色

display.setStatusBar( display.HiddenStatusBar )


local physics = require( "physics" )
local movieclip = require("movieclip")

physics.start()
--physics.setDrawMode( "hybrid" )
--physics.setDrawMode( "debug" )

physics.setGravity( 0, 10 )
math.randomseed(os.time())

local backgroundmusic=audio.loadStream("images/s1.wav")

score1=0

local bg1 = display.newImageRect("images/bg.jpg" ,320,480 )
local bg2 = display.newImageRect("images/bg.jpg" ,320,480 )
local bg3 = display.newImageRect("images/bg.jpg" ,320,480 )

bg1.x = 160 
bg1.y = 240

bg2.x = 160
bg2.y = -240

bg3.x = 160
bg3.y = -720

bg1.speed = 5
bg2.speed = 5
bg3.speed = 5

function bgmove(self,event)
    self.y = self.y + self.speed
    if self.y == 820 then
      self.y = -620
    end
end

bg1.enterFrame = bgmove  
Runtime:addEventListener("enterFrame", bg1)
bg2.enterFrame = bgmove
Runtime:addEventListener("enterFrame", bg2)
bg3.enterFrame = bgmove
Runtime:addEventListener("enterFrame", bg3)

--分數
local score = display.newText("Score : ",150,22,system.nativeFont,20)
local score5 = display.newText(score1,200,22,system.nativeFont,20)
score:setFillColor( 1, 0, 1)
score5:setFillColor( 1, 0, 1) 

---------------------------
-- Shapes
---------------------------
local shape_1 = { 1,-33, 27,-19, 31,14, 10,32, -17,29, -31,9, -28,-15, -13,-27 }
shape_1.density = 1; shape_1.friction = 0.3; shape_1.bounce = 0.2; 


local dispObj_2 = display.newImageRect( "images/ground.png", 400, 60 )
dispObj_2.x = 167
dispObj_2.y = 495
physics.addBody( dispObj_2, "static", { density=1, friction=0.3, bounce=0.2 } )



      local function produceBall(event)
            local dispObj_4 = display.newImageRect( "images/ball.png", 77, 77 )
            dispObj_4.x = 163 + math.random( 100 )
            dispObj_4.y = 252 - math.random( 100 )
            physics.addBody( dispObj_4, 
                {density=shape_1.density, friction=shape_1.friction, bounce=shape_1.bounce, shape=shape_1}
            )
            
            local onTouch = function(event)
                  audio.play(backgroundmusic,{channel=1,loops=1})
                  audio.setVolume(0.2,{channel=1})
                  
                  score1 = score1 + 5
                  score5.text =score1
                   
                  Forest = movieclip.newAnim({"images/explode1.png","images/explode2.png","images/explode3.png","images/explode4.png","images/explode5.png","images/explode6.png","images/explode7.png","images/explode8.png","images/explode9.png"})
                  Forest:play({startFrame=1,endFrame=9,loop=1,remove=true})
                  Forest.x,Forest.y = dispObj_4.x,dispObj_4.y
                  Forest.width,Forest.height=100,100
                  Forest:setDrag{drag=false}
  
                  dispObj_4:removeSelf()
                  dispObj_4 = nil
            end 
            dispObj_4:addEventListener("touch", onTouch)              


            --local function produceBall2(event)
            local dispObj_2 = display.newImageRect( "images/crate.png", 70, 70 )
            dispObj_2.x = 167
            dispObj_2.y = 260
            physics.addBody( dispObj_2, { density=1, friction=0.3, bounce=0.2 } )      
            
            local onTouch = function(event) 
                            score1 = score1 + 10
                            score5.text =score1

                            dispObj_2:removeSelf()
                            dispObj_2 = nil  
                            
            end 
            dispObj_2:addEventListener("touch", onTouch)                
            --end

            
      end



timer.performWithDelay( 1000, produceBall,-1)
--      for i=1,50 do
--      produceBall()
--      produceBall2()
--      end

 

Licensed under CC BY-NC-SA 4.0