Page 1 of 3
Using sprites from a tileset
Posted: Fri Apr 01, 2011 7:35 pm
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?
Re: Using sprites from a tileset
Posted: Fri Apr 01, 2011 7:51 pm
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.
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 2:20 am
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).
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 3:30 am
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.
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 10:21 am
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
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 10:54 am
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
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 11:01 am
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
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 11:22 am
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:
Where the string is the path to your lua file, without the file extension.
Re: Using sprites from a tileset
Posted: Sat Apr 02, 2011 12:40 pm
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:
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:
Re: Using sprites from a tileset
Posted: Sun Apr 03, 2011 6:57 am
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 (23.54 KiB) Viewed 496 times
(Will pixellate Start and Quit to 2x2 later)