前言
学习love2d游戏框架和lua语言记录
一、love2d介绍
love2d框架是一个开源的游戏框架,可以帮助我们快速的搭建一个完整的游戏
1、在配置好love2d的环境后,创建一个游戏项目需要:
(1)先创建一个项目的文件夹,在文件夹中创建一个main.lua文件
(2)在love2d框架中必须创建一个main.lua的文件,这个文件相当于程序的入口。
2、然后就可以在main.lua文件中编写游戏代码了
(1)love2d框架中有三个函数比较常用,function love.load() 、function love.update(dt) 、function love.draw() 。每次写love2d游戏都可以直接先把这三个函数在main.lua文件中先写出来。
(2)function love.load()这个函数是一个加载资源的函数,可以加载在游戏中用到的资源,如图片、音乐、字体、窗口大小、游戏中物体的初始位置坐标等内容。这个函数会在游戏开始时执行一次,加载资源。
(3)function love.update(dt) 这个函数是一个用于更新游戏状态的函数。它会在每一帧进行调用运行,更新物体或人物移动的坐标。
(4)function love.draw()是一个用于绘制游戏场景的函数。它会在每一帧进行调用,并且负责将游戏中的图形、文字等内容绘制到屏幕上。
3、下面用这三个函数实现一个方块在屏幕中左右来回弹的画面。
(1)先使用function love.load()函数,在函数中定义五个变量,方块的坐标x,y和方块的宽和高,和方块的移动速度。
function love.load()
x = 100
y = 100
w = 30
h = 30
speed = 100
end
(2)在使用function love.update(dt)函数来更新方块每一帧移动的情况。x为每过一帧后,x的位置,当x>500,速度取反,此时x便会减小,方块会反向移动。在x<0时在此取反,方块移动方向又取反,实现方块来回移动。
function love.update(dt)
x = x + speed * dt
if x > 500 then
speed = -speed
elseif x < 0 then
speed = -speed
end
end
(3) 最后使用function love.draw()函数把每一帧的结果绘制出来。先用love.graphics.setColor()函数来设置绘制的颜色和透明度,前三个参数是颜色rgb,最后一个参数是透明度,在用love.graphics.rectangle()函数来画一个矩形,fill表示填充,x,y表示矩形左上角的坐标,w,h是矩形的长和宽
function love.draw()
love.graphics.setColor(0,255,0,1)
love.graphics.rectangle("fill",x,y,w,h)
end
(3)运行上面三段代码,可实现绿色方块左右来回移动的画面。
二、用love2d框架实现贪吃蛇游戏
使用love2d框架实现贪吃蛇游戏
1,先在窗口画出网格,便于查看蛇和苹果位置和长度。每一个格的大小为20*20
在love.draw()函数中写下面代码,先用love.graphics.setColor(100,0,0,1)函数设置画线用的颜色,在用两个for循环分别画出横线和竖线,形成一个网格
love.graphics.setColor(100,0,0,1)
for x=0,lineY,20 do
love.graphics.line(0,x,800,x)
end
for x=0,lineX,20 do
love.graphics.line(x,0,x,600)
end
2,在创建一个存放蛇的表,和苹果的表。在love.load()函数中先加载蛇和苹果的初始位置。在for循环中将初始蛇的长度设置为3。每次循环中用snake表,表示的蛇身体的每一个方块的位置加入snakes表中组成一个完整的蛇。用apple.x和apple.y表示苹果的初始位置。并用apple.eat表示苹果是否被吃。
snakes={}
apple={}
direction=6
fpstotal=0
function love.load()
love.window.setMode(800, 600)-- {resizable=true}
for i=1,3 do
snake={}
snake.x=0+(i-1)*20
snake.y=400
snakes[i]=snake
end
apple.x=300
apple.y=300
apple.eat=true
end
3、用love.keypressed()函数来检测按键,控制蛇移动的方向。a表示左,d表示右,w表示上,s表示下,b表示重新开始。
function love.keypressed(key)
if key=='a' then
direction=4
elseif key=='d' then
direction=6
elseif key=='w' then
direction=8
elseif key=='s' then
direction=2
end
if key=='b' then
love.load()
end
end
4、下面是蛇移动的代码,当蛇身体长度大于0时执行,用getnext()函数获取蛇头的下一格出现的位置,如果和苹果的坐标一样,就说明蛇吃了这一个苹果,在蛇头前面加一个方块并将apple.eat设为false,如果蛇头下一个位置不是苹果,则依然将下一个方块加入蛇头位置,蛇尾则删去一个方块。
如果apple.eat为false时就更新苹果的位置。
function snakeupdate()
if #snakes>0 then
if (getnext().x==apple.x and getnext().y==apple.y) then
table.insert(snakes,1,getnext())
apple.eat=false
else
table.insert(snakes,1,getnext())
table.remove(snakes)
end
end
if apple.eat==false then
appleupdate()
end
end
function getnext()
snake={}
if direction==4 then
snake.x=snacks[1].x-20
snake.y=snacks[1].y
end
if direction==6 then
snake.x=snacks[1].x+20
snake.y=snacks[1].y
end
if direction==8 then
snake.x=snacks[1].x
snake.y=snacks[1].y-20
end
if direction==2 then
snake.x=snacks[1].x
snake.y=snacks[1].y+20
end
return snake
end
function appleupdate()
apple.x=love.math.random(39-0)*20
apple.y=love.math.random(29-0)*20
apple.eat=true
end
5、最后在love.draw()函数中将蛇和苹果的图形画出来。用love.graphics.rectangle("fill",apple.x,apple.y,20,20)画出苹果的图形。用for循环画出组成蛇的每一个方块的图形。
function love.draw()
love.graphics.setColor(100,0,0,1)
for x=0,lineY,20 do
love.graphics.line(0,x,800,x)
end
for x=0,lineX,20 do
love.graphics.line(x,0,x,600)
end
love.graphics.rectangle("fill",apple.x,apple.y,20,20)
for i=1,#snakes do
love.graphics.rectangle("fill",snakes[i].x,snakes[i].y,20,20)
end
end
游戏运行效果
FlappyBird-GameDevelopment-master
1.这段代码是一个简单的实现类和对象的 Lua 模块,可以用于创建和操作类实例。
首先,它定义了几个辅助函数,如 include_helper
、include
和 clone
,用于支持类和对象的定义和操作。
然后,它定义了 new
函数,该函数用于创建一个新的类。在创建类时,可以通过参数传入一个包含属性和方法的表。通过调用 setmetatable
设置类的元表,使得类对象可以像函数一样被调用,并返回一个对象实例。
接下来,它实现了一个用于提供跨类系统兼容性的接口,以便与其他类库进行整合。
最后,通过 setmetatable
返回一个表,该表包含 new
、include
和 clone
函数,通过将其设置为元表的 __call
元方法,可以直接将模块当作函数来调用,以便创建并返回一个新的类对象。
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
函数如何实现功能,和作用没看懂