[Code] Help requested.. again...

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
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

[Code] Help requested.. again...

Post by Sosolol261 »

I am feel SO BAD to ask you guys for help again.
I usually don't like to rely on others but this is impossible for me to do alone :(

I am making a Tick Tack Toe game on Love2D and came across this problem.
The O/X images won't be drawn on the screen. It seems like I am missing something big here, since the whole function seems to be frozen.

The code is too long to print so I will give you the .love file.
It is not commented yet, but if you would like some comments, tell me right away.

And also: Is there a way to find errors quickly? Not programs I mean tricks I can use to develop my skills a bit.

Thank you in advance :)
Joe Black
Prole
Posts: 39
Joined: Wed Jan 21, 2015 1:57 pm

Re: [Code] Help requested.. again...

Post by Joe Black »

hi sosolol,

some tricks to debug :
1- create a text like debug="" or more than one
draw it each frame
and if something don't work write debug="theprogrammisherenow\n"..debug at some points the program must go through
2- you can also wait a second in love.update to see better

in your code the program doesn't go through the if or the elsif of OX_draw it seems you forget the / in front of mltplay - in the update too.

then the tile1 is drawn but there are other problems you'll see. take it step by step.

pleased to help you.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: [Code] Help requested.. again...

Post by Positive07 »

Okey I made several changes:

As said above, you have "mltplay1" in some places and "/mltplay1" in others, I changed everything to "/mltplay1" and "/mltplay2" so that it fits better

You had this in love.update:

Code: Select all

	if gamestate == '/mltplay1' or gamestate == '/mltplay2' then
		take_turns()
	end
Which means that in every frame you changed from player 1 to player 2, and that is not what you want, you need to wait until the user presses something to change the state, luckily you got that right and in the OX_click you had this for every tile:

Code: Select all

if mousePosX > tile.pos1X and mousePosX < tile.pos1X + 100 and mousePosY > tile.pos1Y and mousePosY < tile.pos1Y + 100 then
	tile1 = true
	take_turns()
end
But here is your second error, you put tile1 to true, and then in your OX_draw you did this

Code: Select all

if gamestate == "/mltplay1" then
	if tile1 == true then
		love.graphics.draw(O, tile.pos1X, pos1Y)
	elseif tile2 == true then
		love.graphics.draw(O, tile.pos2X, pos1Y)
	...
end
This means that you draw just 1 tile every frame, because of elseif, you dont get to check each tile. The other error was that each tile was drawn an O if it was the turn of player 1, and each tile was drawn an X if it was the turn of player X
So I changed the OX_click to this:

Code: Select all

if mousePosX > tile.pos1X and mousePosX < tile.pos1X + 100 and mousePosY > tile.pos1Y and mousePosY < tile.pos1Y + 100 then
	if not tile1 then
		tile1 = gamestate
		take_turns()
	end
end
Which now sets tile 1 to the current game state
I changed OX_draw to this:

Code: Select all

if tile1 == '/mltplay1' then
	love.graphics.draw(O, tile.pos1X, tile.pos1Y)
end
if tile2 == '/mltplay1' then
	love.graphics.draw(O, tile.pos2X, tile.pos1Y)
end
...
if tile1 == '/mltplay2' then
	love.graphics.draw(X, tile.pos1X, tile.pos1Y)
end
if tile2 == '/mltplay2' then
	love.graphics.draw(X, tile.pos2X, tile.pos1Y)
end
Here you may notice that im checking if I should draw O or X, also other thing I changed here was that, before, you had pos1Y, pos2Y and so on for the Y position, but it should have been tile.pos1Y, tile.pos2Y and so on

The other problem was that you were drawing the main_UI after OX_draw, which means that after you placed all the O's and X's you drew the tiles.png image, which doesnt have a transparent background, so that means that it was on top and you couldnt see the O's and X's at all... To change this I changed this lines in love.draw

Code: Select all

if gamestate == '/mltplay1' or gamestate == '/mltplay2' then
	main_UI()
	OX_draw() --OX_draw is now drawn after main_UI
end
So here we had the O's and X's, but they were drawn with a color background, and in the turn of player 1 the background was black, and in player2 turn it was red, that was because you never changed back the color. Adding love.graphics.setColor(255,255,255) as the first line in OX_draw solved this.

Right now your game was completely fine, the only problem being that the backgrounds of the X's and O's covered the tiles.png image, to solve this I replaced your assets but that is because I wanted to do an other neat trick ;)

Now make the logic so that I can play your game!
Attachments
TickTackToe.love
(22.07 KiB) Downloaded 49 times
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

Re: [Code] Help requested.. again...

Post by Sosolol261 »

Positive07 wrote:Okey I made several changes:

As said above, you have "mltplay1" in some places and "/mltplay1" in others, I changed everything to "/mltplay1" and "/mltplay2" so that it fits better

You had this in love.update:

Code: Select all

	if gamestate == '/mltplay1' or gamestate == '/mltplay2' then
		take_turns()
	end
Which means that in every frame you changed from player 1 to player 2, and that is not what you want, you need to wait until the user presses something to change the state, luckily you got that right and in the OX_click you had this for every tile:

Code: Select all

if mousePosX > tile.pos1X and mousePosX < tile.pos1X + 100 and mousePosY > tile.pos1Y and mousePosY < tile.pos1Y + 100 then
	tile1 = true
	take_turns()
end
But here is your second error, you put tile1 to true, and then in your OX_draw you did this

Code: Select all

if gamestate == "/mltplay1" then
	if tile1 == true then
		love.graphics.draw(O, tile.pos1X, pos1Y)
	elseif tile2 == true then
		love.graphics.draw(O, tile.pos2X, pos1Y)
	...
end
This means that you draw just 1 tile every frame, because of elseif, you dont get to check each tile. The other error was that each tile was drawn an O if it was the turn of player 1, and each tile was drawn an X if it was the turn of player X
So I changed the OX_click to this:

Code: Select all

if mousePosX > tile.pos1X and mousePosX < tile.pos1X + 100 and mousePosY > tile.pos1Y and mousePosY < tile.pos1Y + 100 then
	if not tile1 then
		tile1 = gamestate
		take_turns()
	end
end
Which now sets tile 1 to the current game state
I changed OX_draw to this:

Code: Select all

if tile1 == '/mltplay1' then
	love.graphics.draw(O, tile.pos1X, tile.pos1Y)
end
if tile2 == '/mltplay1' then
	love.graphics.draw(O, tile.pos2X, tile.pos1Y)
end
...
if tile1 == '/mltplay2' then
	love.graphics.draw(X, tile.pos1X, tile.pos1Y)
end
if tile2 == '/mltplay2' then
	love.graphics.draw(X, tile.pos2X, tile.pos1Y)
end
Here you may notice that im checking if I should draw O or X, also other thing I changed here was that, before, you had pos1Y, pos2Y and so on for the Y position, but it should have been tile.pos1Y, tile.pos2Y and so on

The other problem was that you were drawing the main_UI after OX_draw, which means that after you placed all the O's and X's you drew the tiles.png image, which doesnt have a transparent background, so that means that it was on top and you couldnt see the O's and X's at all... To change this I changed this lines in love.draw

Code: Select all

if gamestate == '/mltplay1' or gamestate == '/mltplay2' then
	main_UI()
	OX_draw() --OX_draw is now drawn after main_UI
end
So here we had the O's and X's, but they were drawn with a color background, and in the turn of player 1 the background was black, and in player2 turn it was red, that was because you never changed back the color. Adding love.graphics.setColor(255,255,255) as the first line in OX_draw solved this.

Right now your game was completely fine, the only problem being that the backgrounds of the X's and O's covered the tiles.png image, to solve this I replaced your assets but that is because I wanted to do an other neat trick ;)

Now make the logic so that I can play your game!
Oh my.. so many obvious errors. I feel dumb now..
But well thanks for taking your time explaining some stuff :D

And about the tile psition: You just have to add 1 px to the tile positions.
like this
pos1X = 249, pos1Y = 249,
pos2X = 350, pos2Y = 350,
pos3X = 451, pos3Y = 451,
Last edited by Sosolol261 on Sun Jan 25, 2015 2:36 am, edited 2 times in total.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: [Code] Help requested.. again...

Post by Positive07 »

No problem my friend! We are here to learn and help!
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests