Love2D 中的面向对象编程(Lua)

Object-Oriented Programming in Love2D(Lua)

我对 Love2D 和 Lua 产生了兴趣,并决定尝试一下。

所以为了熟悉Lua和Love2D,我写了一个简单的例子:

项目结构:

demo
  |-ball.lua
  |-main.lua

ball.lua

Ball = {
    x = 0,
    y = 0,
    xSpeed = 0,
    ySpeed = 0,
    ballRadius = 0,
    r = 0,
    g = 0,
    b = 0
}

function Ball:new(x, y, xSpeed, ySpeed, ballRadius, r, g, b)
    t = {
        x = x,
        y = y,
        xSpeed = xSpeed,
        ySpeed = ySpeed,
        ballRadius = ballRadius, 
        r = r,
        g = g,
        b = b
    }
    setmetatable(t, self)
    self.__index = self
    return t
end

function Ball:move() 
    self.x = self.x + self.xSpeed
    self.y = self.y + self.ySpeed
end

function Ball:changeColor() 
    self.r = love.math.random(0, 255)
    self.g = love.math.random(0, 255)
    self.b = love.math.random(0, 255)
    print('color: ' .. self.r .. ' ' .. self.g .. ' ' .. self.b)
end

function Ball:checkEdges() 
    if self.x + self.ballRadius > love.graphics.getWidth() or self.x - self.ballRadius < 0 then 
        self.xSpeed = self.xSpeed * -1
        Ball:changeColor()
    end
    if self.y + self.ballRadius> love.graphics.getHeight() or self.y - self.ballRadius < 0 then 
        self.ySpeed = self.ySpeed * -1
        Ball:changeColor()
    end
end

function Ball:show() 
    love.graphics.setColor(self.r, self.g, self.b)
    love.graphics.ellipse('fill', self.x, self.y, self.ballRadius)
end

main.lua

require "ball"
local ball = nil
local x, y

function love.load()
    x = love.graphics.getWidth() / 2
    y = love.graphics.getHeight() / 2
    ball = Ball:new(x, y, 2, 3.5, 20, 255, 255, 255)
end

function love.update(dt) 
    Ball.move(ball)
    Ball.checkEdges(ball)
end

function love.keypressed(key)
    if key == 'escape' then 
        love.event.quit()
    end
end

function love.draw()
    love.graphics.setBackgroundColor(0, 0, 0)
    Ball.show(ball)
end

所以基本上它只是一个球,当它撞到边缘时会四处弹跳。
除了 function Ball:changeColor()
之外,一切似乎都很好 我希望球每次碰到边缘时都会改变颜色,但这不起作用。
function changeColor() 有问题吗?

这是演示的快照:

函数确实触发了并且 rgb 颜色值确实改变了,但是球本身没有改变颜色,感谢任何帮助!

最大的错误是颜色定义本身。
颜色范围从 0(零)到 1。 所以改变...

function Ball:changeColor() 
    self.r = love.math.random(0, 255)
    self.g = love.math.random(0, 255)
    self.b = love.math.random(0, 255)
    print('color: ' .. self.r .. ' ' .. self.g .. ' ' .. self.b)
end

...到...

function Ball:changeColor() 
    self.r = love.math.random()
    self.g = love.math.random()
    self.b = love.math.random()
    print('color: ' .. self.r .. ' ' .. self.g .. ' ' .. self.b)
end

它应该比打印出来...

color: 0.064632309279245 0.45080402957018 0.39887099192605

Object-Oriented编程

你用 Ball:new() 分配了一个球,但在进一步的代码中你没有使用 ball 分配的方法。
要将球作为参数提供给方法,使用 :
我给你一个更正的版本来说明我的意思。
我的彩球在这里命名为 cball ;-)

-- main.lua
require "ball"

function love.load()
    x = love.graphics.getWidth() / 2
    y = love.graphics.getHeight() / 2
    cball = Ball:new(x, y, 27, 27, 200, 0, 0, 0)
end

-- When object is assigned you reach the desired function with the :
-- So use it...
function love.update(dt) 
    cball:move() -- Use assigned method with cball as first argument
    cball:checkEdges() -- Same here
end

-- Checking if key is released avoids repeating keys ;-)
function love.keyreleased(key)
    if key == 'escape' or key == 'q' then 
        love.event.quit()
    end
    if key == 'r' then
        love.event.quit('restart')
    end
end

function love.draw()
    love.graphics.setBackgroundColor(.125, .125, .25)
    cball:show() -- Same here
end

最后一件事是 ball.lua 并不是真正设计为 Lua 的要求。
你的球Luareturns什么都没有。
因此第二个要求与第一个要求不一样。
好的设计示例...

-- ball.lua start with...
local Ball = {
    x = 0,
    y = 0,
    xSpeed = 0,
    ySpeed = 0,
    ballRadius = 0,
    r = 1,
    g = 0,
    b = 0
}
-- Than all function/method defining for Ball and last line do
return Ball

之后每次(首先从文件然后从 package.loaded.ball)正确加载。
此外,您可以直接进行设计...

-- Ball = require('ball')
function love.load()
    x = love.graphics.getWidth() / 2
    y = love.graphics.getHeight() / 2
    cball = require('ball'):new(x, y, 27, 27, 20, 0, 0, 0)
end

作为副作用,您在 new() 中提供的选项将全部生效。
另一个副作用是你可以在...

cball=require('ball'):new(...)

...一个...

newball=cball:new(...)

(三个点是选项的占位符)
...使另一个 (x, y, speedx, speedy, radius, color) 独立于 child child (ball) that require('ball') <--<< parent Object.