love2d游戏1--1942game(一)

这一路来我感觉对love2d已经基本了解,但对于如何做游戏还是不太熟悉,于是便有
看一些的love2d游戏代码的想法。1942game来自此处,版权归其作者我只是略作修

改,我将分两篇来讲解,此篇为游戏启动画面。

当我们玩游戏时,进入我们的第一个界面便是启动画面了,启动画面一来可以作为游

戏的宣传,二来可以用来预加载游戏资源。我们可以先建一个类,在里面放上启动画

面的代码,代码里放上状态变量和时间控制。然后在main.lua里检测状态变量的值,

并执行相应的操作。具体如下:

main.lua

require('splash')
function love.load()
  
  font = love.graphics.newFont("assets/font.ttf",14*scale)
  love.graphics.setFont(font)
  
  --初始化
  splash.load("splash","assets/title.gif","assets/music.mp3")
end

function love.draw()
  
  if splash.state == "splash" then
    splash.draw()
  end
  if splash.state=="game" then
    love.graphics.printf("Welcome to the game!",0,80*scale,love.graphics.getWidth(),"center")
  end
end

function love.update(dt)
 
  if splash.state == "splash" then
    splash.update(dt)
  end
  
end

function love.keypressed(key)
 
  if splash.state == "splash" then
    splash.keypressed(key)
  end

end

splash.lua

splash = {}
--初始化
function splash.load(state,img,music)
  splash.dt_temp = 0
  splash.state=state
  --加载资源
  splash.img=love.graphics.newImage(img)
  splash.music=love.audio.newSource(music, "stream" )
  splash.music:setLooping(true)
  love.audio.play(splash.music)
end

function splash.draw()
  love.graphics.draw(splash.img,0,(splash.dt_temp-1)*32*scale,0,scale,scale)
 
  -- 2.5s  后显示提示
  if splash.dt_temp == 2.5 then
    love.graphics.printf("Press Start",
      0,80*scale,love.graphics.getWidth(),"center")
  end
  
end

function splash.update(dt)
  
  splash.dt_temp = splash.dt_temp + dt
  -- 计时2.5s
  if splash.dt_temp > 2.5 then
    splash.dt_temp = 2.5
  end
  
    
    
end

function splash.keypressed(key)
  --改变游戏状态
  splash.state = "game"
  love.audio.stop(splash.music)
end

代码下载,请点击

 

 

 

posted @ 2013-04-08 22:27  半山th  阅读(1479)  评论(0编辑  收藏  举报