Dissapearing food in snake game!

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
SamPerson12345
Prole
Posts: 41
Joined: Sat Aug 30, 2008 5:35 pm

Dissapearing food in snake game!

Post by SamPerson12345 »

I'm working on a snake clone, but I have run into an odd problem. For some reason, after the snake eats a certain amount of food, it stops reappearing and I can't figure out why. Could someone help? Here is the code:

Code: Select all

function load()
	font = love.graphics.newFont(love.default_font, 12)
	love.graphics.setFont(font)
	message1 = "Snake"
	message2 = "Press Space To Start"
	peice = love.graphics.newImage("SnakePart.png")
	started = false
	direction = 4
	PeicesX = {6,5,4,3,2,1}
	PeicesY = {1,1,1,1,1,1}
	Peices = 6
	X=6
	Y=1
	Score = 0
	time = 0
	FoodX = math.random(1,25)
	FoodY = math.random(1,18)
end

function draw()
	if love.keyboard.isDown(love.key_space) then
		started = true
	end
	if started == false then
		love.graphics.draw(message1,400,200)
		love.graphics.draw(message2,350,300)
	else
		for v = 1, Peices, 1  do
			love.graphics.draw(peice,PeicesX[v]*32+16,PeicesY[v]*32+16)
		end
		love.graphics.draw(peice,FoodX*32+16,FoodY*32+16)
	end
end

function play()
	if direction == 1 then
		Y=Y-1
	end
	if direction == 2 then
		Y=Y+1
	end
	if direction == 3 then
		X=X-1
	end
	if direction == 4 then
		X=X+1
	end
	for v=Peices,1,-1 do
		if v+1 <= Peices then
			PeicesX[v+1]=PeicesX[v]
			PeicesY[v+1]=PeicesY[v]
		end
	end
	PeicesX[1]=X
	PeicesY[1]=Y
	if X==FoodX and Y==FoodY then
		Peices=Peices+1
		PeicesX[Peices]=PeicesX[Peices-1]
		PeicesY[Peices]=PeicesY[Peices-1]
		FoodX = math.random(1,25)
		FoodY = math.random(1,18)
	end
end

function reset()
	started = false
	direction = 4
	PeicesX = {6,5,4,3,2,1}
	PeicesY = {1,1,1,1,1,1}
	Peices = 6
	X=6
	Y=1
	Score = 0
	time = 0
	FoodX = math.random(1,25)
	FoodY = math.random(1,18)
end

function update(dt)
	time = dt + time
	if time > .1 then
		play()
		time = 0
	end
	if love.keyboard.isDown(love.key_up) then
		direction = 1
	end
	if love.keyboard.isDown(love.key_down) then
		direction = 2
	end
	if love.keyboard.isDown(love.key_left) then
		direction = 3
	end
	if love.keyboard.isDown(love.key_right) then
		direction = 4
	end
end
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Dissapearing food in snake game!

Post by surtic »

You draw the food at (FoodX*32+16,FoodY*32+16).
FoodX can be between 1 and 25 and FoodY can be between 1 and 18.
That means that you draw the food between (48,48) and (816,592). But the screen is 800x600, so if FoodX is 25 you don't see the food.

Because you are using math.random() without math.randomseed(), the sequence is always the same and the 11th piece of food is always at 25,9. So you don't see the food, but it's still possible to eat it (and continue playing).

To find the problem, I've added this line to the draw() function:

Code: Select all

love.graphics.draw(string.format("fx=%d, fy=%d", FoodX, FoodY), 10, 20)
User avatar
mike
Administrator
Posts: 364
Joined: Mon Feb 04, 2008 5:24 pm

Re: Dissapearing food in snake game!

Post by mike »

Here is an example of how to use randomseed, btw:

Code: Select all

math.randomseed(os.time())
This sets the randomization seed to the current operating system time (meaning that the seed will be different every time). This needs to be done because it's impossible to get a computer to give you a random number, its brain just doesn't work that way. What it does is have a sequence of code which presents a series of numbers based on the seed number that seems random, but only if the seed is different every time. Place this piece of code in your load somewhere and you're good to go.

I just wanted to be helpful too...
Now posting IN STEREO (where available)
User avatar
SamPerson12345
Prole
Posts: 41
Joined: Sat Aug 30, 2008 5:35 pm

Re: Dissapearing food in snake game!

Post by SamPerson12345 »

Thanks for both of your help! I'll be releasing this as soon as I can set up a way to check if the snake is hitting itself.
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Dissapearing food in snake game!

Post by surtic »

Just a tiny comment about math.randomseed() - you should probably throw away the first few random numbers.
I normally do it with

Code: Select all

math.randomseed(os.time())
math.random()
math.random()
math.random()
Here's the problem:

Code: Select all

> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
83
> =math.random(1,100)
90
> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
78
> =math.random(1,100)
80
> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
41
> =math.random(1,100)
16
> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
3
> =math.random(1,100)
52
> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
34
> =math.random(1,100)
71
> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
66
> =math.random(1,100)
89
> math.randomseed(os.time())
> =math.random(1,100)
70
> =math.random(1,100)
62
> =math.random(1,100)
16
>
You see that the first "random" number is always 70, right after math.randomseed(). It's something to do with Lua, not with the C library.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests