level 6
function useDefaultFont(text,x,y,size)--默认字体 size=12
love.graphics.setColor(255,0,0)
love.graphics.setBlendMode("alpha")
love.graphics.setColorMode("combine")
local font = love.graphics.newFont( size )
love.graphics.setFont(font)
love.graphics.print(text,x,y+size)
end
function useTTFFont(text,x,y,size)--自选字体
local font=love.graphics.newFont("assets/msyh.ttf",size)
love.graphics.setFont(font)
--love.graphics.print(text,x,y)
end
------------------------------------------------------------------
function love.load() --资源加载回调函数,仅初始化时调用一次
--print(love.getVersion()) --获取版本
--love.window.setFullscreen(true, "desktop") --桌面全屏
--print(love.math.random(100, 300))
------------------------------------------------------------窗体
wolrd = {
width = love.graphics.getWidth(),
height = love.graphics.getHeight(),
title = love.window.getTitle(),
}
------------------------------------------------------------游戏
game = {
i = 1, --文本
j = 1, --背景
k = 1, --选项
n = 0, --累计
goon = true;
}
------------------------------------------------------------打字机特效
-- typewriter = {
-- x = wolrd.width/3.5, --屏幕中央
-- y = wolrd.height/4,
-- text = "dear:\nyou are my wolrd",
-- _text = "",
-- i = 0,
-- j = 0,
-- fps = 10
-- }
------------------------------------------------------------对话框
dialog = {
width = wolrd.width,
height = wolrd.height / 3,
x = 0,
y = wolrd.height - (wolrd.height / 3),
title = {
text = '文字冒险游戏简易版引擎',
x = 21,
y = wolrd.height - (wolrd.height) / 3 + 3
},
text = {
_text = '',
i = 0,
j = 0,--中文3字节
fps = 1,
x = 3*7,
y = wolrd.height - (wolrd.height) / 3 + 36
}
}
--------------------------------------------------------命令按钮
btnNext = {
x = wolrd.width - 50 - 30,
y = wolrd.height - 50 - 30,
image = love.graphics.newImage("assets/arrow.png")
}
--游戏剧情
text = {
'欢迎来到我的世界', --1
'这是我学习love2d的第二天', --2
'这是我学习love2d的第三天', --2
3,--4
'这是我学习love2d的第四天', --5
2,--6
'这是我学习love2d的第五天',
'这是我学习love2d的第六天',
3,
'这是我学习love2d的第七天',
'这是我学习love2d的第八天',
}
--剧情背景
imageBg = {
love.graphics.newImage("assets/0.jpg"), --1
'',--2
'',--3
'',--4
love.graphics.newImage("assets/1.jpg"), --5
'',--6
love.graphics.newImage("assets/2.jpg"), --7
'','',
love.graphics.newImage("assets/3.jpg"),
love.graphics.newImage("assets/4.jpg"),
love.graphics.newImage("assets/5.jpg"),
love.graphics.newImage("assets/6.jpg"),
love.graphics.newImage("assets/7.jpg"),
}
--{}
imagePerson = {
}
optionTextArray = { --剧情分支数据 i
{'1','第二个的第二个','哦哦'},
{'2','第二个的第二个','咳咳咳'},
{'3','第一个的第二个','第一个的第三个'},
}
--print(optionTextArray[2][2]) --英文
option = {
alive = false,
text = {
{
value = optionTextArray[game.k][1], --剧情分支数据 i
x = (wolrd.width - 224)/2 + 7,
y = wolrd.height/9 + 7,
},
{
value = optionTextArray[game.k][2],
x = (wolrd.width - 224)/2 + 7,
y = wolrd.height/3.3 + 7,
},
{
value = optionTextArray[game.k][3],
x = (wolrd.width - 224)/2 + 7,
y = wolrd.height /2 + 7,
},
},
image = {
{
width = 224,
height = 36,
x = (wolrd.width - 224)/2,
y = wolrd.height/9,
hint = love.graphics.newImage("assets/hint.dds"),
},
{
width = 224,
height = 36,
x = (wolrd.width - 224)/2,
y = wolrd.height/3.3,
hint = love.graphics.newImage("assets/hint.dds"),
},
{
width = 224,
height = 36,
x = (wolrd.width - 224)/2,
y = wolrd.height /2,
hint = love.graphics.newImage("assets/hint.dds"),
},
}
}
text.length = table.maxn(text)
imageBg.length = table.maxn(imageBg)
optionTextArray.length = table.maxn(optionTextArray)
useTTFFont("中文",0,0,16) --使用中文字体
love.mouse.setCursor(love.mouse.newCursor("assets/cur.png",21,21)) --更换鼠标光标
print("中文")
end
function love.update(dt) --更新回调函数,每周期调用
if type(text[game.i])=="number" then --判断是否为数值
game.n = game.n +1
if game.n == 1 then
game.goon = false
option.alive = true
end
end
local str = '' --typewrite
dialog.text.i = dialog.text.i + 1
if dialog.text.i % dialog.text.fps == 0 then
dialog.text.j = dialog.text.j + 1
if dialog.text.j % 3 == 0 then
str = string.sub(text[game.i],0,dialog.text.j)
dialog.text._text = str
end
end
end
function love.draw() --绘图回调函数,每周期调用
love.graphics.draw(imageBg[game.j] , 0, 0)
-- local str = ''
-- love.graphics.print(typewriter._text,typewriter.x - 3 * string.len(typewriter.text),typewriter.y)
-- typewriter.i = typewriter.i + 1
-- if typewriter.i % typewriter.fps == 0 then
-- typewriter.j = typewriter.j + 1
-- str = string.sub(typewriter.text,0,typewriter.j)
-- typewriter._text = str
-- end
-- if typewriter.j == string.len(typewriter.text) then
-- typewriter.j = 0
-- end
--dialog
love.graphics.rectangle("line", dialog.x, dialog.y, dialog.width, dialog.height)
love.graphics.rectangle("line", dialog.x, dialog.y, dialog.width, 30)
love.graphics.print(dialog.title.text, dialog.title.x, dialog.title.y)
love.graphics.print(dialog.text._text, dialog.text.x, dialog.text.y)
--command
love.graphics.draw(btnNext.image, btnNext.x, btnNext.y)
if game.goon == false then
if option.alive == true then
for i=1,text[game.i] do --选项坐标
love.graphics.draw(option.image[i].hint, option.image[i].x, option.image[i].y)
love.graphics.print(option.text[i].value,option.text[i].x,option.text[i].y)
end
end
end
end
function love.keypressed(key) --键盘检测回调函数,当键盘事件触发是调用
if key == "escape" then love.window.close() end --esc按键 退出
end
function love.mousepressed(x,y,key) --回调函数释放鼠标按钮时触发。
if game.goon == true then
if x > btnNext.x and y > btnNext.y and x < btnNext.x + 60 and y < btnNext.y + 60 then --按钮坐标
if key == 1 then --鼠标左键
if text.length - game.i >= 1 then --数组长度
game.i = game.i + 1
dialog.text.j = 0
end
-- if imageBg.length - game.j >= 1 then --数组长度
-- game.j = game.j + 1
-- end
end
end
else
if option.alive == true then
for i=1,text[game.i] do --选项选择
if x > option.image[i].x and y > option.image[i].y and x < option.image[i].x + option.image[i].width and y < option.image[i].y + option.image[i].height then --选项坐标
if i==1 then
print(i)
elseif i==2 then
print(i)
else
print(i)
end
game.goon = true
if game.k < optionTextArray.length then
game.k = game.k + 1
option.text[1].value = optionTextArray[game.k][1]
option.text[2].value = optionTextArray[game.k][2]
option.text[3].value = optionTextArray[game.k][3]
end
game.i = game.i + 1
game.j = game.i
game.n = 0
option.alive = false
end
end
end
end
end
2017年12月01日 04点12分
