Page 1 of 1

Help with making a game

Posted: Tue Jan 08, 2013 5:26 pm
by antibiotic1
Ok i am making a simple tic tac toe game. I had maked the tables and now i don't know hot to draw x and 0 images on mouse click.
I have background 400x400 with 9 132x132 quads, and o.png for 0 and x.png for x image. 2 pixels are between every quad.
Here is my code for my table(sorry for bad english)

Code: Select all

guad = {}
   guad[0] = love.graphics.newQuad(0, 0, 132, 132, 132, 132)
   guad[1] = love.graphics.newQuad(134, 0, 132, 132, 132, 132)
   guad[2] = love.graphics.newQuad(268, 0, 132, 132, 132, 132)
   guad[3] = love.graphics.newQuad(0, 134, 132, 132, 132, 132)
   guad[4] = love.graphics.newQuad(134, 134, 132, 132, 132, 132)
   guad[5] = love.graphics.newQuad(268, 134, 132, 132, 132, 132)
   guad[6] = love.graphics.newQuad(0, 268, 132, 132, 132, 132)
   guad[7] = love.graphics.newQuad(134, 268, 132, 132, 132, 132)
   guad[7] = love.graphics.newQuad(268, 268, 132, 132, 132, 132)
 

Re: Help with making a game

Posted: Tue Jan 08, 2013 6:15 pm
by Saegor
i think you misunderstood what newQuad function is for
it's for image map, like animation, sprites in a row, you see ?

if you want to draw your two images, simply do something like

Code: Select all

imageO = "o.png"
imageX = "x.png"
after that you can, for example, store the grid coordinates in a table
(in Lua, a table start at 1 by default so take the habit)

Code: Select all

   grid = {}

   grid[1] = {0, 0}
   grid[2] = {134, 0}
   grid[3] = {268, 0}
   grid[4] = {0, 134}
   grid[5] = {134, 134}
   grid[6] = {268, 134}
   grid[7] = {0, 268}
   grid[8] = {134, 268}
   grid[9] = {268, 268}
after that you can draw an o or x like this

Code: Select all

love.graphics.draw(imageO, grid[1])
sorry too for my bad english

Re: Help with making a game

Posted: Tue Jan 08, 2013 6:29 pm
by antibiotic1
Ok i understand that but what about mousepressed and random drawing X and O pictures?

Re: Help with making a game

Posted: Tue Jan 08, 2013 6:41 pm
by Saegor
antibiotic1 wrote:Ok i understand that but what about mousepressed and random drawing X and O pictures?
hey man, you can read the wiki !

try something like

Code: Select all

function love.mousepressed( x, y, button )
if button == "l" then

for i, v in ipairs(grid) do

    if x > v[1] and x < v[1]+132 and y > v[2] and y < v[2]+132 then
        love.graphics.draw(image_O, grid[i])
    end
end

Re: Help with making a game

Posted: Tue Jan 08, 2013 6:42 pm
by Yell0w
You can use the buildt in function love.mousepressed like this:

Code: Select all

function love.mousepressed(x, y, button)
	if x <= 120 and y <= 120 then
       --     code for enabling a X or O 
       end

end

Re: Help with making a game

Posted: Wed Jan 09, 2013 2:52 am
by Jasoco
I would prefer to make the "grid" as an actual grid.

Code: Select all

grid = {}
for x = 1, 3 do
  grid[x] = {}
  for y = 1, 3 do
    grid[x][y] = 0 --Whatever your default "empty square" ID is.
  end
end
Then you can refer to grid[x][y] for whatever tile it contains or set it accordingly.

Also makes it much easier to check for winners.

Re: Help with making a game

Posted: Wed Jan 09, 2013 12:35 pm
by antibiotic1
How can i can refer to grid[x][y]? I have the same table like Saegor has posted... i don't understand how to post the random image x or o on mouse click with predeifined grid table ...sorry for bad English tnx

Re: Help with making a game

Posted: Wed Jan 09, 2013 3:26 pm
by Ubermann
antibiotic1 wrote:How can i can refer to grid[x][y]? I have the same table like Saegor has posted... i don't understand how to post the random image x or o on mouse click with predeifined grid table ...sorry for bad English tnx
Check out this:

Code: Select all

numOfBoardRows = 10
numOfBoardColumns = 10
cellWidth = 48
cellHeight = 48

function love.draw()
    local mouseX, mouseY = love.mouse.getX(), love.mouse.getY()
    local thisCellX, thisCellY = mouseX/cellWidth, mouseY/cellHeight
    
    for i = 1, numOfBoardRows do
	        love.graphics.line(i*cellWidth, 0, i*cellWidth, cellHeight*numOfBoardRows)
    end
    for i = 1, numOfBoardColumns do
	        love.graphics.line(0, i*cellHeight, cellWidth*numOfBoardColumns, i*cellHeight)
    end
    
    if thisCellX <= numOfBoardColumns and thisCellX > 0 and 
       thisCellY <= numOfBoardRows and thisCellY > 0 then

        love.graphics.print("Selected cell: " .. math.floor(1 + mouseX/cellWidth) .. ", " .. 
                             math.floor(1 + mouseY/cellHeight), 25, cellHeight*numOfBoardRows + 50)
    else
        love.graphics.print("Mouse cursor is out of board bounds", 25, cellHeight*numOfBoardRows + 50)
    end
end
You can execute that code and you will get the next:
roEp7.png
roEp7.png (59.57 KiB) Viewed 406 times