Using sprites from a tileset

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.
Evrim
Prole
Posts: 20
Joined: Fri Apr 01, 2011 4:22 pm

Using sprites from a tileset

Post by Evrim »

Code: Select all

function love.load()
	font = love.graphics.newImageFont("font.png",
    " abcdefghijklmnopqrstuvwxyz" ..
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
    "123456789.,!?-+/():;%&`'*#=[]\"")
	love.graphics.setFont(font)
	
	tilesetImage = love.graphics.newImage( "dwarves.png" )
	tilesetImage:setFilter("nearest", "linear") -- this "linear filter" removes some artifacts if we were to scale the tiles
	tileSize = 24
	
	char = love.graphics.newImage("dwarves.png")
	
	x = 50
	y = 50
	speed = 50
end

function love.draw()
    love.graphics.print("Hello World", 400, 300)
	love.graphics.setBackgroundColor(255,255,255)
	
	love.graphics.draw(char, x, y)
end

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

   if love.keyboard.isDown("down") then
      y = y + (speed * dt)
   elseif love.keyboard.isDown("up") then
      y = y - (speed * dt)
   end
end
Here is my code and I got 3 questions

1) dwarves.png is a file that contains 30+ dwarf sprites each 24x24 pixels, I want to get one of them and use it as "char"

2) how can I resize the window to a size I want

3) how can I make sprites move on a 24x24 pixel grid and put borders for movement

Bonus) will I need to use multiple classes, if so - how?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Using sprites from a tileset

Post by bartbes »

Evrim wrote: 1) dwarves.png is a file that contains 30+ dwarf sprites each 24x24 pixels, I want to get one of them and use it as "char"
Using a Quad.
Evrim wrote: 2) how can I resize the window to a size I want
Check out Config Files.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Using sprites from a tileset

Post by Taehl »

Evrim wrote:3) how can I make sprites move on a 24x24 pixel grid and put borders for movement
I, personally, would scale them to 1/24th pixel size and love.graphics.scale them by 24. Then you just need to move them by "1", and everything works out.
Alternately, just move them 24 pixels at a time. x = x+24 or whatever.

I assume by "borders for movement" you mean you want to prevent things from moving past a certain point. That's easy enough: Just force their coordinates to be within the range of your playing field (maybe something like if x<0 then x=0 end).
Last edited by Taehl on Sat Apr 02, 2011 4:21 am, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Using sprites from a tileset

Post by Robin »

Taehl wrote:I, personally, would scale them to 1/24th pixel size and love.graphics.scale them by 24. Then you just need to move them by "1", and everything works out.
Alternately, just move them 24 pixels at a time. x = x+24 or whatever.
I would recommend the second method, especially for new people.
Help us help you: attach a .love.
Evrim
Prole
Posts: 20
Joined: Fri Apr 01, 2011 4:22 pm

Re: Using sprites from a tileset

Post by Evrim »

Code: Select all

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

   if love.keyboard.isDown("down") then
      y = y + (speed)
   elseif love.keyboard.isDown("up") then
      y = y - (speed)
   end
end
speed=24

and sprites go turbo...

EDIT: made a new code, but the thing is that I need to tap the keys to go in a direction

Code: Select all

function love.keypressed( key )
   if key == "right" then
      x = x + (speed)
   elseif key == "left" then
      x = x - (speed)
   end
   
   if key == "up" then
      y = y - (speed)
   elseif key == "down" then
      y = y + (speed)
   end
end
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Using sprites from a tileset

Post by nevon »

Evrim wrote:and sprites go turbo...

EDIT: made a new code, but the thing is that I need to tap the keys to go in a direction
The reason they go turbo is because they'll be moved every frame. One solution is to do what you did, but as you've seen, that would require you to tap the key each time you want to move. If you want to be able to hold down a button to move, say, two times per second, simply create a timer and do a check every frame. Like this:

Code: Select all

function love.load()
    timer = 0.5
end
function love.update(dt)
    if timer <= 0 then
        local hasMoved = false
        if love.keyboard.isDown("right") then
            x = x + (speed)
            hasMoved = true
        elseif love.keyboard.isDown("left") then
            x = x - (speed)
            hasMoved = true
        end
        
        if love.keyboard.isDown("down") then
            y = y + (speed)
            hasMoved = true
        elseif love.keyboard.isDown("up") then
            y = y - (speed)
            hasMoved = true
        end

        if hasMoved then
            timer = 0.5
        end
    else
        timer = timer - dt
    end
end
Evrim
Prole
Posts: 20
Joined: Fri Apr 01, 2011 4:22 pm

Re: Using sprites from a tileset

Post by Evrim »

Well, thanks for that!

Now it moves perfectly


The thing I need to know is how to make a main menu and in-game GUIs for such stuff as pause menu and inventories
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Using sprites from a tileset

Post by nevon »

Evrim wrote:The thing I need to know is how to make a main menu and in-game GUIs for such stuff as pause menu and inventories
Löve is not quite a game engine, even though it's marketed as one. As such, it has no concept of a game entity, and it has no idea what a GUI or a button is. Therefore, if you want to create buttons, you're going to have to make that yourself. How you do that is up to you. There are some libraries of various completion out there (a search for "Goo" might help), or you could use a custom solution. If you only need a few buttons for your game, you could hard code them into your views. In that case, just draw an image and check if the user is clicking within its hitbox.

As for the other question that you PM:ed me, to use other .lua files, simply run:

Code: Select all

require('otherluafile')
Where the string is the path to your lua file, without the file extension.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Using sprites from a tileset

Post by Robin »

Evrim wrote:The thing I need to know is how to make a main menu and in-game GUIs for such stuff as pause menu and inventories
What you might also want to try is to look around on this forum for games that have things like that and get your inspiration from them.
nevon wrote:As for the other question that you PM:ed me, to use other .lua files, simply run:

Code: Select all

require('otherluafile')
Where the string is the path to your lua file, without the file extension.
More specifically, if you have another Lua file you'd like to include in the directory crap, named stuff.lua (so the full path, relative to your main.lua, would be "crap/stuff.lua", you can include as:

Code: Select all

require("crap.stuff")
Help us help you: attach a .love.
Evrim
Prole
Posts: 20
Joined: Fri Apr 01, 2011 4:22 pm

Re: Using sprites from a tileset

Post by Evrim »

Thanks Robin for explaining that, will that dot trick work on images too because I started adding loads of sprites and the folder is getting filled by images


and a new problem, I found a tricky way for making a main - by re-moving (not remove) the images, I took them to very far away off the screen and screen gets cleared

The problem is not that but this

Code: Select all

function love.keypressed( key )
	local onMainMenu = true
	if onMainMenu == true then
		if key == "s" then
			onMainMenu = false
			x1 = -1000
			y1 = -1000
			x2 = -1000
			y2 = -1000
			x3 = -1000
			y3 = -1000
			x4 = -1000
			y4 = -1000
		end
	end
	if onMainMenu == true then
		if key == "q" then
			onMainMenu = false
			love.event.push('q')
		end
	end
end
S starts the game (removes main menu)
and Q quits it

But after I press S, screen clears supposedly but after I press Q, it quits the game even I set onMainMenu to false in S

I tried sperating the if-then to the current state (it used elseif, no luck on that) but didn't work :(
3lTNG.png
3lTNG.png (23.54 KiB) Viewed 282 times
(Will pixellate Start and Quit to 2x2 later)
Post Reply

Who is online

Users browsing this forum: No registered users and 79 guests