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('颜色: ' .. 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()有问题吗?

这是演示的快照:

1

函数确实触发了,rgb颜色值确实有所改变,但球本身没变颜色,任何帮助都是受欢迎的!

原文链接 https://stackoverflow.com/questions/70140672

点赞
stackoverflow用户11740758
stackoverflow用户11740758

最大的错误是颜色定义本身。

颜色的范围从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

面向对象编程

你用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

-- 当对象被分配时,你需要使用 : 调用所需的函数
-- 所以使用它...
function love.update(dt)
    cball:move() -- 使用分配的方法,cball作为第一个参数
    cball:checkEdges() -- 这里也一样
end

-- 检查是否释放了密钥避免重复按键 ;-)
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() -- 这里也一样
end

最后一件事是ball.lua不是真正设计为Lua的需求。

你的球Lua没有返回值。

因此第二个require不能像第一个require一样。

一个好的设计示例...

-- ball.lua 始于...
local Ball = {
    x = 0,
    y = 0,
    xSpeed = 0,
    ySpeed = 0,
    ballRadius = 0,
    r = 1,
    g = 0,
    b = 0
}
--然后是Ball的所有函数/方法定义并且最后一行做
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,半径,颜色)独立的子(球),它们从require('ball')<--<< 子对象的父对象中派生。

2021-11-28 09:16:14