Questions of a noob

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.
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Questions of a noob

Post by mrpoptart »

Hello all. I am trying to make a tank game. I have been reading tuts. on the site, and reading everything i can pretty much but i am having a really hard time lmao. Even though i do have past experience, though it is little, i still can not grasp how to do a few things.

1. How can i draw my player, the tank, on my map that i have made.
2. How can i get my tank to move in the direction that the key on the keyboard is pressed. (example, left is pressed, the tank turns left, right, it turns right etc.) I think i am in the right direction here, just doing it in the wrong spot.

I am going to include my whole folder of files here so it will be easier for you guys to see just what it is i am messing up, besides the whole thing lmao.

Thank you in advance for any help, and the help i have already got on here.
Attachments
tanktestgame.love
(48.1 KiB) Downloaded 161 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Questions of a noob

Post by Robin »

I cannot read that .love. Try making a ZIP instead of a RAR.
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Questions of a noob

Post by BlackBulletIV »

1. love.graphics.draw. If the tank is an image (most likely), you can use love.graphics.newImage to create an Image object. Like this:

Code: Select all

function love.load()
  tankImg = love.graphics.newImage('tank.png') -- or wherever you've put it
end

function love.draw()
  -- draw background, you must draw the tank after the background so that it will be on top of the background
  love.graphics.draw(tankImg)
end
2. You can capture key presses with love.keypressed. However, you'll probably be wanting to check every frame whether a key is currently down, by using love.keyboard.isDown.

Hope that helps!
Deif
Prole
Posts: 12
Joined: Sun Mar 06, 2011 10:01 pm

Re: Questions of a noob

Post by Deif »

Ah ok I see where you've gotten mixed up. You have a love.load, love.update, and love.draw in each of your files. You don't want to do that at all. If you want to make a really simple working version, then just whack everything from the map.lua file into the main.lua file, and everything from the player.lua file into the main.lua file as well (i.e. put the love.update(dt) function from the player file into the love.update(dt) function in the main.lua file.

Basically the main.lua file is the first file that is called and it runs the love.load function when it first loads up. Then it updates, draws, etc. but it doesn't go through the other files in the folder looking for these functions, it only does this in the main.lua file. So what's the point in the other files? Well you had the right idea with separating all the objects out so that it's easier to read. Really what you want to do is change the functions in the map.lua file to Map.load, Map:update(dt), Map:draw(), etc. and then reference that in the main.lua file simple something like the following:

Code: Select all

function love.load()
map = Map.load()
end

function love.update(dt)
map:update(dt) -- this is where the love engine goes hunting for a method called 'update(dt)' in the Map object.
end

function love.draw()
map:draw()
end
It might be a bit unclear, but if you download one of the demos that you get with the love engine, and look at the code behind no.love. You'll see what I mean. What they do (which is what I stole and am using at the moment) is for each object (map, player, etc.) they do a set of functions to allow you to manipulate the 'object' in any way you like. Note I say object here but lua doesn't have objects due to the way the language works, but I just like to pretend that it does, and you can kind of mimic the behaviour of them.

So, a little snippet of what you might like to put into your map.lua code is:

Code: Select all

Map = {}
Map.__index = Map

function Map.load()

local temp = {}
setmetatable(temp, Map)

-- Now comes all your fancy stuff, but load them into the temp variable
temp.TileW, temp.TileH = 32, 32
temp.Tileset = love.graphics.newImage('tiles.png')

-- Do the same with your other thingys, then end with
return temp
end
That's kinda what happens in the no.love demo. So this way you can make as many player or map 'objects' as you like by calling them in the love.load function as a separate variable. I hope that makes sense.... I'm not a very good teacher.
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

It actually makes great sense Deif. I had no idea i could do something like that. It should make things a great deal easier to understand and even to write. Thank you all for the help, and i hope i can take this info and make my game work.
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

I think i tried what you were telling me Deif. But i now get an error when i try to test my code. I get :
Error
main.lua:3: attempt to index global 'Map' (a nil value)
traceback
main.lua:3: in function 'load'
[C]: in function 'xpcall'

Here is the code i have worked on and got this error.
I have also tried to end the love.load function right after the player.load() and still get this error
main.lua

Code: Select all

function love.load()
map = Map.Load() 
player = Player.Load()


   function love.keypressed(key)
	if state.keypressed then state.keypressed(key) end
   end

    function love.keyreleased(key)
	if state.keyreleased then state.keyreleased(key) end
    end

    function love.update(dt)
	map:update(dt)
	player:update(dt)
    end

  function love.draw()	
	map:draw()
	player:draw()
   end
end
map.lua

Code: Select all

Map = {}
Map.__index = Map

function Map.load()

local temp = {}
setmetatable(temp, Map)

temp.TileW, temp.TileH = 32, 32
temp.Tileset = love.graphics.newImage('tiles.png')
  
  local TilesetW, TilesetH = temp.Tileset:getWidth() , temp.Tileset:getHeight()
  
  Quads = {
    love.graphics.newQuad(0, 0, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 1 = brown
    love.graphics.newQuad(32, 0, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 2 = grass
    love.graphics.newQuad(0, 32, temp.TileW, temp.TileH, temp.tilesetW, temp.tilesetH), -- 3 = halfbridge
    love.graphics.newQuad(32, 32, temp.TileW, temp.TileH, temp,tilesetW, temp.tilesetH) -- 4 = black
  }
  
    TileTable = {
  
     { 2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,2 },
     { 1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1 },
     { 1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,1 },
     { 2,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
     { 2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,2,2,2,2 } 
  }

  return temp
end

function Map.draw()
  for rowIndex=1, #TileTable do
    local row = TileTable[rowIndex]
    for columnIndex=1, #row do
      local number = row[columnIndex]
      local x = (columnIndex-1)*temp.TileW
      local y = (rowIndex-1)*temp.TileH
      love.graphics.drawq(temp.Tileset, Quads[number], x, y)
    end
  end
end

player.lua

Code: Select all

Player = {}
Player.__index = Player


function Player.load()
    --First we initialize all the state images that we need
    images = {
        up = love.graphics.newImage("tankup.png"),
		down = love.graphics.newImage("tankdown.png"),
        left = love.graphics.newImage("tankleft.png"),
		idle = love.graphics.newImage("tankidle.png"),
        right = love.graphics.newImage("tankright.png")
    }

    --And here's our player object, complete with a state variable and a variable that holds the current image.
    player = {
        state = "idle",
        image = images["idle"],
        x = 500,
        y = 300,
        speed = 100
		hp = 100
    }
end

function player.update(dt)
    --Now what we do is we check for a button to be pressed, and if it is we set the player state and move the player.
    if love.keyboard.isDown("left") then
        player.state = "left"
        player.x = player.x - player.speed*dt
    elseif love.keyboard.isDown("right") then
        player.state = "right"
        player.x = player.x + player.speed*dt
    elseif love.keyboard.isDown("up") then
        player.state = "up"
        player.x = player.x - player.speed*dt
    elseif love.keyboard.isDown("down") then
        player.state = "down"
        player.x = player.x + player.speed*dt
	else
	if love.keyboard.isDown(" ") then
	player.state ="idle"
    end
end
    --Now we update the player image with whatever image corresponds to the current state.
    player.image = images[player.state]
end

function Player.draw()
    --And finally we draw the player.
    love.graphics.draw(player.image, player.x, player.y)
end
Last edited by mrpoptart on Tue Apr 26, 2011 2:59 pm, edited 1 time in total.
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Questions of a noob

Post by BarnD »

Why was EVERYTHING in love.load()?
Should be like this.
mrpoptart wrote: main.lua

Code: Select all

function love.load()
    map = Map.Load() 
    player = Player.Load()
end

function love.keypressed(key)
    if state.keypressed then state.keypressed(key) end
end

function love.keyreleased(key)
    if state.keyreleased then state.keyreleased(key) end
end

function love.update(dt)
    map:update(dt)
    player:update(dt)
end

function love.draw()	
    map:draw()
    player:draw()
end
Last edited by BarnD on Tue Apr 26, 2011 3:03 pm, edited 1 time in total.
mrpoptart
Prole
Posts: 17
Joined: Mon Apr 25, 2011 5:31 am

Re: Questions of a noob

Post by mrpoptart »

I have tried it that way before BarnD, and i just tried it again just to make sure, and i get the same error.
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Questions of a noob

Post by BarnD »

mrpoptart wrote:I have tried it that way before BarnD, and i just tried it again just to make sure, and i get the same error.
Oh, have you included the other two .lua files in the main.lua?
With

Code: Select all

require("map.lua")
require("player.lua")
(I think)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Questions of a noob

Post by Robin »

main.lua:

Code: Select all

require("map")
require("player")

-- rest of your main.lua
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests