Created
August 30, 2012 15:19
-
-
Save rikchilvers/3530653 to your computer and use it in GitHub Desktop.
Tile-based scrolling in LÖVE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function love.conf( t ) | |
| t.screen.width = 100 | |
| t.screen.height = 100 | |
| end | |
| function love.load( ) | |
| tiles = {} | |
| for i = 0, 15 do -- we're going to have a 4x4 grid (16 tiles, counting from 0) | |
| tiles[i] = i -- we'll just have each tile as a number to make it easy see what's going on | |
| end | |
| mapWidth = 4 -- we'll use these two for checking boundaries when moving by keyboard... | |
| mapHeight = 4 -- they should match the map table's dimension (look below) | |
| mapDisplayWidth = 2 -- the area to show the player... | |
| mapDisplayHeight = 2 -- like a square within a square | |
| positionX = 1 -- these are the coordinates of where to start drawing the inner square (the viewport, if you like)... | |
| positionY = 1 -- they originate at 0,0 in the top left corner | |
| tileWidth = 25 -- reasonably self-explanatory (in pixels, by the way)... | |
| tileHeight = 20 -- randomly picked to ensure the numbers didn't overlap | |
| map = { -- our map | |
| { 0, 1, 2, 3}, -- in this case we print out the actual numbers... | |
| { 4, 5, 6, 7}, -- but in a game they would refer to types of tile... | |
| { 8, 9, 10, 11}, -- so you'd have a table full of 0s, 1s, 2s, and 3s... | |
| { 12, 13, 14, 15}, -- (if you had 4 types of tile) | |
| } | |
| end | |
| function love.draw( ) | |
| drawMap() -- call the function below | |
| end | |
| function drawMap( ) | |
| for y = 1, mapDisplayHeight do -- draws 1 up to your viewport's height in tiles vertically | |
| for x = 1, mapDisplayWidth do -- the same horizontally (remember it counts down and right from the top left) | |
| love.graphics.print( | |
| tiles[map[y + positionY][x + positionX]], -- see below | |
| (x * tileWidth), -- the x position in pixels of where to do the printing | |
| (y * tileHeight) -- the y version of the above | |
| ) | |
| --[[ | |
| Let's break down line 37 | |
| The tiles table (line 3) contains 16 things that look like 1 = 1, 2 = 2 etc | |
| In a game it would be 1 = where/to/find/tile1.png, 2 = where/to/find/tile2.png etc | |
| But we just want numbers, so its 0 = 0 up to 15 = 15 | |
| So we're asking the tiles table for something ( tiles[... ) | |
| And that something is what is at a certain position (x,y) in our map table | |
| We find that postion using map[y + positionY][x + positionX] | |
| You can think of this as what is at y,x in map? It's weird it's y then x, but ignore that | |
| We could say map[y][x] but then we couldn't adjust what to show when the player moves | |
| So we modify it with positionY and positionX | |
| Clever, huh? | |
| --]] | |
| end | |
| end | |
| end | |
| -- phew! on to moving the blasted thing around | |
| -- we move the map around the player, rather than the player through the map | |
| -- implementation for the left and right keypresses will be more complicated (but more concise) | |
| -- then up and down, but identical in effect | |
| function love.keypressed( key, unicode ) | |
| if key == 'up' then -- easy peasy | |
| positionY = positionY - 1 -- if you push up, move the map down (think about how you move your mouse if you want to move north in Google Maps) | |
| if positionY < 0 then -- check to see if we're going to far up | |
| positionY = 0 -- if we are, only move the map to the top most row | |
| end | |
| end | |
| -- the next part stops the bottom of our viewport going out of the bottom of the map | |
| -- remember our viewport is a small square within a square, the boundaries for which are drawn from its top left corner | |
| if key == 'down' then | |
| positionY = positionY + 1 | |
| if positionY > mapHeight - mapDisplayHeight then -- e.g. if positionY is greater than 4 - 2 then | |
| positionY = mapHeight - mapDisplayHeight -- make positionY = 4 - 2 | |
| end | |
| end | |
| if key == 'left' then | |
| positionX = math.max(positionX - 1, 0) -- select the largest of either positionX - 1 or 0 | |
| end | |
| if key == 'right' then | |
| positionX = math.min(positionX + 1, mapWidth - mapDisplayWidth) -- select the smallest of positionX + 1 or 4 - 2 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment