# Calculating dynamic light and shadows in tile based roguelike games – part 1: Bresenham’s line algorithm

I love roguelike games. From the simple ASCII games to more complex turn based games like Sproggiwood. I also made some blog posts about roguelike games. One of the features a good roguelike game should have is dynamic light and shadows, as well as a “fog of war” effect. Although it may seem complex, I’ll show you this feature in a way you will find it a lot easier than it seems. I will use a logic similar to the one used in the creation of a survival horror game, but this time we are dealing with tiles. In this first step, we’ll see the algorithm which will be the core of dynamic lights in tile based environments: Bresenham’s line algorithm. From Wikipedia: Bresenham’s line algorithm is an algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures. It is one of the earliest algorithms developed in the field of computer graphics. To create a demo with Bresenham algorithm I am going to use the JavaScript code found on RosettaCode together with the magic of Phaser. This is what we are going to create:
We have two draggable tiles, the white ones, a green straight line connecting the tiles, and the result of Bresenham algorithm made of grey tiles. Try to drag white tiles and see what happens. This will be our ray of light. The source code is very basic at the moment, let’s have a look:
```var game;
var gridWidth = 40;
var gridHeight = 40;
var tileSize = 16;

game = new Phaser.Game(640, 640, Phaser.AUTO, "");
game.state.start("PlayGame");
}

var playGame = function(game){};

playGame.prototype = {
},
create: function(){
var startCol = game.rnd.between(0, gridWidth - 1);
var startRow = game.rnd.between(0, gridHeight - 1);
this.startPoint = game.add.sprite(startCol * tileSize, startRow * tileSize, "tile");
this.startPoint.inputEnabled = true;
this.startPoint.input.enableDrag();
this.startPoint.input.boundsRect = new Phaser.Rectangle(0, 0, game.width, game.height);
this.startPoint.input.enableSnap(tileSize, tileSize, true, true);
do{
var endCol = game.rnd.between(0, gridWidth - 1);
var endRow = game.rnd.between(0, gridHeight - 1);
} while (startRow == endRow && startCol == endCol);
this.endPoint = game.add.sprite(endCol * tileSize, endRow * tileSize, "tile");
this.endPoint.inputEnabled = true;
this.endPoint.input.enableDrag();
this.endPoint.input.boundsRect = new Phaser.Rectangle(0, 0, game.width, game.height);
this.endPoint.input.enableSnap(tileSize, tileSize, true, true);
},
update: function(){
this.lineGroup.removeAll(true);
this.lineGraphics.clear();
this.lineGraphics.lineStyle(3, 0x00ff00);
this.lineGraphics.moveTo(this.startPoint.x + tileSize / 2, this.startPoint.y + tileSize / 2);
this.lineGraphics.lineTo(this.endPoint.x + tileSize / 2, this.endPoint.y + tileSize / 2);
var x0 = this.startPoint.x / tileSize;
var x1 = this.endPoint.x / tileSize;
var y0 = this.startPoint.y / tileSize;
var y1 = this.endPoint.y / tileSize;
var dx = Math.abs(x1 - x0);
var sx = -1;
if(x0 < x1){
var sx = 1
}
var dy = Math.abs(y1 - y0);
var sy = -1;
if(y0 < y1){
var sy = 1;
}
var err = -dy / 2;
if(dx > dy){
err = dx / 2;
}
do{
var tile = game.add.sprite(x0 * tileSize, y0 * tileSize, "tile");
tile.alpha = 0.5;
var e2 = err;
if(e2 > -dx){
err -= dy;
x0 += sx;
}
if(e2 < dy){
err += dx;
y0 += sy;
}
} while(x0 != x1 || y0 != y1)
}
}
```
It’s just the management of draggable sprites which snaps on a grid and the rendering of Bresenham algorithm itself, which runs from line 44 to line 75. The reason I am leaving this script uncommented is most of it will be discarded in the final version as it’s only a demo to show you how Bresenham algorithm works. Next time I am going to show you how to use this algorithm to create dynamic light and shadows, meanwhile if you already have some idea, you can download the source code and start from here.
215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 10000000
// 2 Cars
// 2048
// Avoider
// Ballz
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Breakout
// Bricks
// Columns
// CubesOut
// Dots
// DROP'd
// Dudeski
// Eskiv
// Filler
// Fling
// Globe
// HookPod
// Hundreds
// InkTd
// Iromeku
// Lumines
// Magick
// MagOrMin
// Maze
// Memdot
// Nano War
// Nodes
// o:anquan
// Ononmin
// Pacco
// Phyballs
// Platform
// Poker
// Pool
// Poux
// Pudi
// qomp
// Racing
// Renju
// SameGame
// Security
// Sling
// Slingy
// Sokoban
// Splitter
// Sproing
// Stack
// Stairs
// Stringy
// Sudoku
// Tetris
// Threes
// Toony
// Turn
// TwinSpin
// vvvvvv
// Wordle
// Worms
// Yanga
// Zhed
// zNumbers