Odd bug in 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

Odd bug in game.

Post by SamPerson12345 »

I was making a Conway's game of life game, but there is a weird bug. After the first iteration, cells no longer die. Also, cells sometimes die in the first iteration when they shouldn't. Can someone help?

Code: Select all

function load()
	Cell = love.graphics.newImage("Cell.PNG")
	WorldX = {}
	for a=1,160,1 do
		WorldX[a] = {}
		WorldX[a].WorldY = {}
		for b=1,120,1 do
			WorldX[a].WorldY[b] = false
		end
	end
	ChangeX = {}
	for a=1,160,1 do
		ChangeX[a] = {}
		ChangeX[a].ChangeY = {}
		for b=1,120,1 do
			ChangeX[a].ChangeY[b] = false
		end
	end
	Playing = false
	Neighbors = 0
	time = 0
end

function draw()
	for a=1,160,1 do
		for b=1,120,1 do
			if WorldX[a].WorldY[b] then
				love.graphics.draw(Cell,a*5-2.5,b*5-2.5)
			end
		end
	end
end

function update(dt)
	if Playing then
		time = dt+time
		if time > .1 then
			time = time-.1
			life()
		end
	end
end

function keypressed(key)
	if key == love.key_right and Playing == false then
		life()
	end
	if key == love.key_space then
		if Playing then
			Playing = false
		else
			Playing = true
		end
	end
end

function mousepressed(x,y,button)
	if button == love.mouse_left and Playing == false then
		if WorldX[math.floor((x+5)/5)].WorldY[math.floor((y+5)/5)] == false then
			WorldX[math.floor((x+5)/5)].WorldY[math.floor((y+5)/5)] = true
		else
			WorldX[math.floor((x+5)/5)].WorldY[math.floor((y+5)/5)] = false
		end
	end
end

function life()
	for a=1,160,1 do
		for b=1,120,1 do
			Neighbors = 0
			if a>1 then
				if WorldX[a-1].WorldY[b] then
					Neighbors = Neighbors+1
				end
			end
			if a<160 then
				if WorldX[a+1].WorldY[b] then
					Neighbors = Neighbors+1
				end
			end
			if b>1 then
				if WorldX[a].WorldY[b-1] then
					Neighbors = Neighbors+1
				end
			end
			if b<120 then
				if WorldX[a].WorldY[b+1] then
					Neighbors = Neighbors+1
				end
			end
			if a>1 and b>1 then
				if WorldX[a-1].WorldY[b-1] then
					Neighbors = Neighbors+1
				end
			end
			if a<160 and b<120 then
				if WorldX[a+1].WorldY[b+1] then
					Neighbors = Neighbors+1
				end
			end
			if b>1 and a<160 then
				if WorldX[a+1].WorldY[b-1] then
					Neighbors = Neighbors+1
				end
			end
			if b<120 and a>1 then
				if WorldX[a-1].WorldY[b+1] then
					Neighbors = Neighbors+1
				end
			end
			if WorldX[a].WorldY[a] then
				if Neighbors<2 or Neighbors>3 then
					ChangeX[a].ChangeY[b] = false
				else
					ChangeX[a].ChangeY[b] = true
				end
			else
				if Neighbors == 3 then
					ChangeX[a].ChangeY[b] = true
				end
			end
		end
	end
	for a=1,160,1 do
		for b=1,120,1 do
			WorldX[a].WorldY[b] = ChangeX[a].ChangeY[b]
		end
	end
end
Thanks for your help.
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Odd bug in game.

Post by surtic »

Code: Select all

      if WorldX[a].WorldY[a] then
I'm sure you meant

Code: Select all

      if WorldX[a].WorldY[b] then
User avatar
SamPerson12345
Prole
Posts: 41
Joined: Sat Aug 30, 2008 5:35 pm

Re: Odd bug in game.

Post by SamPerson12345 »

Oh wow, I feel really stupid now :P Thanks for your help! :D
Edit:
It works now! Thanks again! :mrgreen:
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 3 guests