Step 1 - Start Your Engines
First in main.ts we can import all of Excalibur as ex, this makes it clear in this example what types are coming from Excalibur.
typescript// main.tsimport * as ex from 'excalibur'
typescript// main.tsimport * as ex from 'excalibur'
You can certainly import individual types like this if you prefer, but for this tutorial we'll be doing the "barrel" import * as ex.
typescriptimport { Engine } from 'excalibur'
typescriptimport { Engine } from 'excalibur'
We can start by creating our ex.Engine, which will be the container for our game and drives the whole thing.
- We can configure the width and height in pixels
- We can configure the background color
#54C0CA - There are different
ex.DisplayMode's, If you want to fit the width and height to the screen and keep aspect ratio, useex.DisplayMode.FitScreen - If you want to avoid the letter boxing you can use
ex.DisplayMode.FitScreenAndFill, only the configured width and height are safe to draw in but outside of that is not guaranteed. - In this sample we are using pixel art, so setting
pixelArt: truewill provide the best defaults for that. pixelRatio: 2will "scale up` the canvas to give us more crisp look at lower resolutions.- Call
.start()to start the game
typescript// main.tsimport * as ex from 'excalibur';const game = new ex.Engine({width: 400,height: 500,backgroundColor: ex.Color.fromHex("#54C0CA"),pixelArt: true,pixelRatio: 2,displayMode: ex.DisplayMode.FitScreen});game.start();
typescript// main.tsimport * as ex from 'excalibur';const game = new ex.Engine({width: 400,height: 500,backgroundColor: ex.Color.fromHex("#54C0CA"),pixelArt: true,pixelRatio: 2,displayMode: ex.DisplayMode.FitScreen});game.start();
At this point the screen will just be a solid color we picked as our backgroundColor.
We can add some css to our game to center it on the screen
csshtml,body {background-color: black;margin: 0;padding: 0;height: 100%;}body {display: flex;justify-content: center;align-items: center;}
csshtml,body {background-color: black;margin: 0;padding: 0;height: 100%;}body {display: flex;justify-content: center;align-items: center;}
Then include this in your index.html in the <head> section
html<link rel="stylesheet" href="./src/style.css">
html<link rel="stylesheet" href="./src/style.css">