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

代码下载,请点击

 

 

 

转载于:https://www.cnblogs.com/xdao/archive/2013/04/08/love2d-game01.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值