Calculating a constant random speed within love.update

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Calculating a constant random speed within love.update

Post by redsled »

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?
Attachments
food eater.love
(1.84 KiB) Downloaded 86 times
Last edited by redsled on Mon Feb 16, 2015 12:31 am, edited 2 times in total.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Calculating a constant random speed within love.update

Post by undef »

You didn't upload a .love file, but I think you might just want to decrease max, right?
If max is 500 then your food would traverse 500 pixels in one second, which is a lot faster than min.

Edit: Oh sry you made that on purpose. So I don't really understand the problem I guess... :/
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: Calculating a constant random speed within love.update

Post by redsled »

undef wrote:You didn't upload a .love file, but I think you might just want to decrease max, right?
If max is 500 then your food would traverse 500 pixels in one second, which is a lot faster than min.

Edit: Oh sry you made that on purpose. So I don't really understand the problem I guess... :/
Oops! I guess I got so caught up in making the post I forgot to even include the .love file! Hope it's available now :oops:
User avatar
Evine
Citizen
Posts: 72
Joined: Wed May 28, 2014 11:46 pm

Re: Calculating a constant random speed within love.update

Post by Evine »

I've changed these 2 functions so that "rand = math.random(1, 5)" is only called when the food is removed by the player or by the floor.
Thinking about it. "local min" , "local max" and "local rand" should all just be in "food.spawn"

Code: Select all

function food.collision()
	for i,v in ipairs(food) do
		if v.x < player.x + player.w and
			v.x + food.w > player.x and 
			v.y < player.y + player.h and 
			v.y + food.h > player.y then
				table.remove(food, i)
				rand = math.random(1, 5)
				food.spawn(math.random(0,love.graphics.getWidth() - food.w),
					math.random(0,love.graphics.getHeight() - respawnX))
		end
	end
end
function food.physics(dt)
	for i,v in ipairs(food) do
		local min = 75
		local max = 500
		
		if v.y + food.h > love.graphics.getHeight() then
			table.remove(food, i)
			rand = math.random(1, 5)
			food.spawn(math.random(0,love.graphics.getWidth() - food.w),
				math.random(0,love.graphics.getHeight() - respawnX))
		end
		print(rand)
		if rand == 1 then
			v.y = v.y + max * dt
		else
			v.y = v.y + min * dt
		end
	end
end
Artal, A .PSD loader: https://github.com/EvineDev/Artal
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: Calculating a constant random speed within love.update

Post by redsled »

Ahh... Ok thanks a bunch! So basically, if I'm getting this right, each time there is a new food spawned, rand gets reset once instead of changing each frame. Thanks a lot, I guess I just didn't know where exactly to set the 'rand' variable.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests