If you view the .love file I provided, then you can see that the food's speed is switching between speeds rapidly. I know this is because it's inside the game loop. However, I'm trying to figure it out so that the food speed is at a constant min 4 out of 5 times the food respawns, and at a constant max 1 out of 5 times the food spawns. I know the max speed is too fast, but I made it high enough to show the distinction.
This is the bit of code for the physics:
Code: Select all
function food.physics(dt)
for i,v in ipairs(food) do
local min = 75
local max = 500
local rand = math.random(1, 5)
if rand == 1 then
v.y = v.y + max * dt
else
v.y = v.y + min * dt
end
if v.y + food.h > love.graphics.getHeight() then
table.remove(food, i)
food.spawn(math.random(0,love.graphics.getWidth() - food.w),
math.random(0,love.graphics.getHeight() - respawnX))
end
end
end
And this is the bit of code for the spawning of the food:
Code: Select all
function food.spawn(x, y)
table.insert(food, {x = x, y = y})
end
I'm guessing I have to create a speed variable within "food.spawn", but whenever I do that and call it, the program doesn't recognize it. If I haven't explained my problem clearly please let me know. Any guesses as how to fix the problem at hand?