有没有办法在lua中使用math.random()以离散增量生成随机数?

时间:2017-06-20 05:50:46

标签: random lua numbers love2d

我正试图通过在LÖVE游戏开发环境中编写lua代码来测试游戏机制。我的目的是让敌方单位在五个车道之一中产生并穿过屏幕。目前,我能够生成所述单位并将它们约束到“车道”所在的边界。但是,我不确定如何在五个通道之一中明确生成它们。

现在,我正在使用math.random()来选择车道顶部和底部边界之间的数字。敌人单位精灵是32x32,因此五个独立的通道将覆盖总共160个像素的屏幕。现在,我将边界设置为240和400(屏幕高度的中间)。如何以32为增量生成一个随机数,以便敌人在五条可能的小道中产生?

编辑:所以,看起来边界实际上并没有设置在中间。将试图找出那个坐标,但如果有人可以给我一个免费赠品小费,我会很高兴! :d

我目前的工作代码如下:

-- Axis-based collision detection. Effective for rectangular units; does not work well with circles and non-quad shapes.

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end

debug = true


-- Timers declared here to reference later.
createEnemyTimerMax = 0.4
createEnemyTimer = createEnemyTimerMax

-- Delcares images; will actually be established in love.load and brought out in love.draw
enemyImg = nil

enemies = {}

function love.load(arg)
    enemyImg = love.graphics.newImage('assets/cat.png')
end


function love.update(dt)
    if love.keyboard.isDown('escape') then
        love.event.push('quit')
    end

    --Generates enemies.
    --Create 5 if/then conditions to account for each lane? 
    --Conditions dependent on createEnemyTimer being < 0 and # of enemies in lane < 5
    --First condition is met; how do we create the second?
    --Will each lane need its own timer?
    createEnemyTimer = createEnemyTimer - (.5 * dt)
    if createEnemyTimer < 0 then
        createEnemyTimer = createEnemyTimerMax

        -- Create an enemy
        randomNumber = math.random(240,400)
        newEnemy = { y = randomNumber, x = 650, img = enemyImg }
        table.insert(enemies, newEnemy)
    end

    --Updates position of enemies.
    for i, enemy in ipairs(enemies) do
        enemy.x = enemy.x - (200 * dt)

        if enemy.x < 0 then -- remove enemies when they pass off the screen
            table.remove(enemies, i)
        end
    end

    -- Collision detection.
    -- For MSJ, collision should prevent PC from moving onto enemy's square.

    --[[

    for i, enemy in ipairs(enemies) do
        for j, bullet in ipairs(bullets) do
            if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.img:getWidth(), bullet.img:getHeight()) then
                table.remove(bullets, j)
                table.remove(enemies, i)
                score = score + 1
            end
        end

        if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight())
        and isAlive then
            table.remove(enemies, i)
            isAlive = false
        end
    end

    ]]

end

function love.draw(dt)
    for i, enemy in ipairs(enemies) do
        love.graphics.draw(enemy.img, enemy.x, enemy.y)
    end
end

1 个答案:

答案 0 :(得分:1)

我认为你应该改变如何在一条车道上生成五个敌人的方法。如何生成标记每条车道相对位置的5个随机数?然后将此相对位置添加到每个通道的开头。例如:

local enemies = {}
local origin_y, height = 240, 32
for i=1,5 do
   local relative_y = math.random(32)
   local y = origin_y + height * i  + relative_y
   table.insert(enemies, { x = math.random(), y = y})
end