function love.load()
Object=require "classic"
end
function love.update(dt)
end
function love.draw()
end
--! file: rectangle.lua
-- Pass Object as first argument.
Rectangle = Object.extend(Object)
function Rectangle.new(self)
self.test = math.random(1, 1000)
end
--! file: main.lua
function love.load()
Object = require "classic"
--Don't forget to load the file
require "rectangle"
r1 = Rectangle()
r2 = Rectangle()
print(r1.test, r2.test)
end
function love.update(dt)
end
function love.draw()
end
--! file: rectangle.lua
Rectangle = Object.extend(Object)
function Rectangle.new(self)
self.x = 100
self.y = 100
self.width = 200
self.height = 150
self.speed = 100
end
function Rectangle.update(self, dt)
self.x = self.x + self.speed * dt
end
function Rectangle.draw(self)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
--! file: main.lua
function love.load()
Object = require "classic"
--Don't forget to load the file
require "rectangle"
r1 = Rectangle()
r2 = Rectangle()
print(r1.test, r2.test)
end
function love.update(dt)
r1.update(r1,dt)
end
function love.draw()
--love.graphics.print(r1.test,100,100)
--love.graphics.print(r2.test,100,200)
r1.draw(r1,100,200)
end
--! file: main.lua
function love.load()
Object = require "classic"
--Don't forget to load the file
require "rectangle"
r1 = Rectangle()
r2 = Rectangle()
print(r1.test, r2.test)
end
--[[
function love.update(dt)
r1.update(r1,dt)
end
--]]
function love.update(dt)
r1:update(dt)
end
function love.draw()
--love.graphics.print(r1.test,100,100)
--love.graphics.print(r2.test,100,200)
--r1.draw(r1,100,200)
r1:draw()
end
--! file: rectangle.lua
--Lua turns this into: Object.extend(Object)
Rectangle = Object:extend()
--Lua turns this into: Rectangle.new(self)
function Rectangle:new()
self.x = 100
self.y = 100
self.width = 200
self.height = 150
self.speed = 100
end
--Lua turns this into: Rectangle.update(self, dt)
function Rectangle:update(dt)
self.x = self.x + self.speed * dt
end
--Lua turns this into: Rectangle.draw(self)
function Rectangle:draw()
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
--Lua turns this into: Object.extend(Object)
Rectangle = Object:extend()
--! file: rectangle.lua
function Rectangle:new(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.speed = 100
end
--Lua turns this into: Rectangle.update(self, dt)
function Rectangle:update(dt)
self.x = self.x + self.speed * dt
end
--Lua turns this into: Rectangle.draw(self)
function Rectangle:draw()
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
--! file: main.lua
function love.load()
Object = require "classic"
require "rectangle"
r1 = Rectangle(100, 100, 200, 50)
r2 = Rectangle(350, 80, 25, 140)
end
function love.update(dt)
r1:update(dt)
r2:update(dt)
end
function love.draw()
r1:draw()
r2:draw()
end
--! file: shape.lua
Shape = Object:extend()
function Shape:new(x, y)
self.x = x
self.y = y
self.speed = 100
end
function Shape:update(dt)
self.x = self.x + self.speed * dt
end
--! file: main.lua
function love.load()
Object = require "classic"
require "shape"
require "rectangle"
r1 = Rectangle()
r2 = Rectangle()
end
--! file: rectangle.lua
Rectangle = Shape:extend()
function Rectangle:new(x, y, width, height)
Rectangle.super.new(self, x, y)
self.width = width
self.height = height
end
function Rectangle:draw()
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
--! file: circle.lua
Circle = Shape:extend()
function Circle:new(x, y, radius)
Circle.super.new(self, x, y)
--A circle doesn't have a width or height. It has a radius.
self.radius = radius
end
function Circle:draw()
love.graphics.circle("line", self.x, self.y, self.radius)
end
--! file: main.lua
function love.load()
Object = require "classic"
--Don't forget to load the file
require "shape"
require "rectangle"
--Don't forget to load the file
require "circle"
r1 = Rectangle(100, 100, 200, 50)
--We make r2 a Circle instead of a Rectangle
r2 = Circle(350, 80, 40)
end
function love.update(dt)
r1:update(dt)
r2:update(dt)
end
function love.draw()
r1:draw()
r2:draw()
end
以上就是今天要讲的内容,本文仅仅简单介绍了Love2d之类和类的继承(Classes And Inheritance),介绍了love2d类和类的继承的使用,与博主的lua语言文章结合更好的理解love2d的编码,如果你是一名独立游戏开发者,或者一位对游戏开发有着深厚兴趣,但是又对于unity3d,ue4等这些对于新手而言不太友好的引擎而头疼的开发者;那么现在,你可以试试Love2D。Love2D是一款基于Lua编写的轻量级游戏框架,尽管官方称呼其为引擎,但实际上它只能称得上是一个框架,因为他并没有一套全面完整的解决方案。不过,这款框架上手及其容易,是学习游戏开发的初学者入门的一个良好选择。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_44918090/article/details/124552259
内容来源于网络,如有侵权,请联系作者删除!