Help with making a 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
antibiotic1
Prole
Posts: 3
Joined: Mon Dec 10, 2012 5:16 pm

Help with making a game

Post 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)
 
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Help with making a game

Post 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
Current work : Isömap
antibiotic1
Prole
Posts: 3
Joined: Mon Dec 10, 2012 5:16 pm

Re: Help with making a game

Post by antibiotic1 »

Ok i understand that but what about mousepressed and random drawing X and O pictures?
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: Help with making a game

Post 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
Last edited by Saegor on Tue Jan 08, 2013 6:43 pm, edited 1 time in total.
Current work : Isömap
User avatar
Yell0w
Prole
Posts: 28
Joined: Wed Nov 21, 2012 7:40 am
Location: Norway
Contact:

Re: Help with making a game

Post 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
You can learn anything, but you cannot learn everything!
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Help with making a game

Post 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.
antibiotic1
Prole
Posts: 3
Joined: Mon Dec 10, 2012 5:16 pm

Re: Help with making a game

Post 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
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Help with making a game

Post 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 222 times
Post Reply

Who is online

Users browsing this forum: Yolwoocle and 222 guests