Page 1 of 1

Help with .draw?

Posted: Thu Apr 17, 2014 1:39 am
by kaupper3

Code: Select all

function love.load()
    player = {
        grid_x = 256,
        grid_y = 256,
        act_x = 200,
        act_y = 200,
        speed = 10
    }
    map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                           
}

    water = love.graphics.newImage("water.jpg")
    
end 









function love.update(dt)
    player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
    player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end












function love.draw()
   
love.graphics.draw (water, 0, 0, 0, 12, 12, 0, 0)

    love.graphics.setColor(189, 183, 107)
    love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
    for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.setColor(0,0,139)
                love.graphics.draw(water)
            end
        end
    end

 for y=2, #map do
        for x=2, #map[y] do
            if map[y][x] == 2 then
                love.graphics.setColor(92,51,23)
                love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
            end
        end
    end
end









function love.keypressed(key)
    if key == "up" then
        if testMap(0, -1) then
            player.grid_y = player.grid_y - 32
        end
    elseif key == "down" then
        if testMap(0, 1) then
            player.grid_y = player.grid_y + 32
        end
    elseif key == "left" then
        if testMap(-1, 0) then
            player.grid_x = player.grid_x - 32
        end
    elseif key == "right" then
        if testMap(1, 0) then
            player.grid_x = player.grid_x + 32
        end
    end
end














function testMap(x, y)
    if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
        return false
    end
    return true
end



So thats my code. I have quite a problems, so ill make a list here:

1. First off, i don't know how to scale everything up to the players monitor size.
2.On my "map" i want to print pictures for the 1's and 0's and so on. My problem is the love.graphics.draw draws it on the coordinates.
3. I have plenty more, but ill save them for later.

Re: Help with .draw?

Posted: Thu Apr 17, 2014 11:28 am
by HugoBDesigner
On the draw function, you must add coordinates for it to work. The game can't "discover" that you're using a grid. Bt, since you made the map based on x-y axis (always a good choice, I think), it shouldn't be hard:

Code: Select all

local s = 16 --the water tile size (I'm just guessing. You can change the value
local offset = {x = 50, y = 50} --just if you don't want it to draw in the top-left corner
love.graphics.draw(water, (x-1)*s+offset.x, (y-1)*s+offset.y)
As for the monitor, you can either set it fullscreen (you can use [wiki]love.window.getFullscreenModes[/wiki] if you're using LÖVE 0.9.0 or higher), set the window resizable (also for LÖVE 0.9.0 or higher) or just change (or let the player change) the window size ([wiki]love.graphics.setMode[/wiki] for LÖVE 0.8.0, [wiki]love.window.setMode[/wiki] for LÖVE 0.9.0 or higher).