molni
molni

Reputation: 49

love2d simple draw exchange picture

I'm totally new to Lua and Love2D and probably do not understand the concepts at all. Well this is a Love2D tutorial, and I want to change it so when I press "a", for example, on the keyboard, the object will exchange (from hamster to car) and so on.

Can you help me out?

-- Tutorial 1: Hamster Ball
-- Add an image to the game and move it around using
-- the arrow keys.
-- compatible with löve 0.6.0 and up

function love.load()
   hamster = love.graphics.newImage("hamster.png")
   auto = love.graphics.newImage("auto.png")
   x = 50
   y = 50
   speed = 300
end

function love.update(dt)
   if love.keyboard.isDown("right") then
      x = x + (speed * dt)
   end
   if love.keyboard.isDown("left") then
      x = x - (speed * dt)
   end

   if love.keyboard.isDown("down") then
      y = y + (speed * dt)
   end
   if love.keyboard.isDown("up") then
      y = y - (speed * dt)
   end
   if love.keyboard.isDown("escape") then
      love.event.quit()
   end
   if love.keyboard.isDown("a") then
      love.draw(auto,x,y)
   end
end

function love.draw()
   love.graphics.draw(hamster, x, y)
end

Upvotes: 2

Views: 2058

Answers (3)

CarSeatHeadrest_Lover
CarSeatHeadrest_Lover

Reputation: 47

This is probably not the best way to solve this, but it works - create an object which has an X value, a Y value, a speed value, and an IMG value. Set the X and Y to 50 and 50, the speed to 300, and set the IMG to nil. In the load function, set the IMG value to whichever image you would prefer it to start as. In the update function, check for a key press and when the key is pressed change the IMG value to the new image.

An example script for clarification:


player = {X = 50, Y = 50, speed = 300, IMG = nil}

function love.load()

    hamster = love.graphics.newImage('hamster.png')
    auto = love.graphics.newImage('auto.png')

    player.IMG = hamster

end

function love.update(dt)

    if love.keyboard.isDown('d') then
        player.x = player.x + player.speed
    elseif love.keyboard.isDown('a') then
        player.x = player.x - player.speed
    end

    if love.keyboard.isDown('w') then
        player.y = player.y + player.speed
    elseif love.keyboard.isDown('s') then
        player.y = player.y = player.speed
    end

    if love.keyboard.isDown('space') then
        if player.IMG == hamster then
            player.IMG = auto
        else 
            player.IMG = hamster
        end

end

function love.draw()
    love.graphics.draw(player.IMG, player.X, player.Y)
end

Hopefully this helps!

Upvotes: 0

Corbin March
Corbin March

Reputation: 25734

I'd suggest using love.update to only update state. Don't draw in it. Then do all of your drawing in love.draw. A solution might be:

local state = {}

function love.load()
    hamster = love.graphics.newImage("hamster.png")
    auto = love.graphics.newImage("auto.png")
    state.activeImage = hamster
    state.activeImageName = "hamster"
    -- <snip> ...
end

function love.update(dt) 
    -- <snip> ... 
    if love.keyboard.isDown("a") then
        if state.activeImageName == "hamster" then
            state.activeImage = auto
            state.activeImageName = "auto"
        else
            state.activeImage = hamster
            state.activeImageName = "hamster"
        end
    end
end

function love.draw()
   love.graphics.draw(state.activeImage, x, y)
end

Upvotes: 0

molni
molni

Reputation: 49

Well thanks Corbin, i figured it out without "state" local variable. Your solution was inspiring to me. Now it's working.

  -- Tutorial 1: Hamster Ball
    -- Add an image to the game and move it around using
    -- the arrow keys.
    -- compatible with löve 0.6.0 and up


    function love.load()
       hamster = love.graphics.newImage("hamster.png")
       auto = love.graphics.newImage("auto.png")
       activeImage = hamster
       activeImageName = "hamster"
       x = 50
       y = 50
       speed = 300
    end

    function love.update(dt)
       if love.keyboard.isDown("right") then
          x = x + (speed * dt)
       end
       if love.keyboard.isDown("left") then
          x = x - (speed * dt)
       end

       if love.keyboard.isDown("down") then
          y = y + (speed * dt)
       end
       if love.keyboard.isDown("up") then
          y = y - (speed * dt)
       end
       if love.keyboard.isDown("escape") then
          love.event.quit()
       end
       if love.keyboard.isDown("a") then
          activeImage = auto
          activeImageName = "auto"
       end
       if love.keyboard.isDown("h") then
          activeImage = hamster
          activeImageName = "hamster"
       end

    end

    function love.draw()
       love.graphics.draw(activeImage, x, y)
    end

Upvotes: 1

Related Questions