write pixi.js game skeleton, with loading bar and splash screen
- User
write pixi.js game skeleton, with loading bar and splash screen
- Assistant
- User
you are using old pixi.js api, could you use pixi.js version 8 and typescript
- Assistant
write pixi.js game skeleton, with loading bar and splash screen
you are using old pixi.js api, could you use pixi.js version 8 and typescript
Below is an example of a simple Pixi.js game skeleton that includes a loading bar and a splash screen. To get started, make sure to include the Pixi.js library in your HTML file.
Here's the HTML file (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixi.js Game Skeleton</title>
<style>
body { margin: 0; padding: 0; overflow: hidden; }
#game-container { width: 100vw; height: 100vh; }
</style>
</head>
<body>
<div id="game-container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/6.5.2/browser/pixi.min.js"></script>
<script src="main.js"></script>
</body>
</html>
And here's the JavaScript file (main.js):
const app = new PIXI.Application({
view: document.getElementById('game-container'),
width: window.innerWidth,
height: window.innerHeight,
backgroundColor: 0x1099bb
});
// Define the splash screen properties
const splashScreenText = new PIXI.Text('Welcome to My Game', {
fontSize: 36,
fill: '#ffffff'
});
splashScreenText.anchor.set(0.5);
splashScreenText.x = app.screen.width / 2;
splashScreenText.y = app.screen.height / 2;
// Add splash screen text to the stage
app.stage.addChild(splashScreenText);
// Define the loading bar properties
const loadingBar = new PIXI.Graphics();
loadingBar.beginFill(0xffffff);
loadingBar.drawRect(0, 0, 300, 50);
loadingBar.endFill();
loadingBar.x = (app.screen.width - 300) / 2;
loadingBar.y = app.screen.height / 2 + 100;
const loadingBarProgress = new PIXI.Graphics();
loadingBarProgress.beginFill(0x00ff00);
loadingBarProgress.drawRect(0, 0, 0, 50);
loadingBarProgress.endFill();
loadingBarProgress.x = loadingBar.x;
loadingBarProgress.y = loadingBar.y;
// Add the loading bar to the stage
app.stage.addChild(loadingBar);
app.stage.addChild(loadingBarProgress);
// Load assets
PIXI.Loader.shared
.add('exampleAsset', 'path/to/your/asset.png') // Add your assets here
.on('progress', handleLoadingProgress)
.load(handleAssetsLoaded);
function handleLoadingProgress(loader) {
const progressWidth = (loader.progress / 100) * 300;
loadingBarProgress.width = progressWidth;
}
function handleAssetsLoaded() {
// Remove splash screen and loading bar
app.stage.removeChild(splashScreenText);
app.stage.removeChild(loadingBar);
app.stage.removeChild(loadingBarProgress);
// Start your game logic here
startGame();
}
function startGame() {
const exampleSprite = new PIXI.Sprite(PIXI.Loader.shared.resources['exampleAsset'].texture);
exampleSprite.x = app.screen.width / 2;
exampleSprite.y = app.screen.height / 2;
exampleSprite.anchor.set(0.5);
app.stage.addChild(exampleSprite);
}
// Handle window resize
window.addEventListener('resize', () => {
app.renderer.resize(window.innerWidth, window.innerHeight);
splashScreenText.x = app.screen.width / 2;
splashScreenText.y = app.screen.height / 2;
loadingBar.x = (app.screen.width - 300) / 2;
loadingBar.y = app.screen.height / 2 + 100;
loadingBarProgress.x = loadingBar.x;
loadingBarProgress.y = loadingBar.y;
});
In this example:
Make sure to replace 'path/to/your/asset.png' with the actual path to your asset. This example provides a clear structure for adding more game-related logic after the assets are loaded.
Certainly! Here's an example using Pixi.js version 8 with TypeScript. First, you'll need to set up a TypeScript project and install the required dependencies.
npm init -y
npm install pixi.js typescript ts-loader webpack webpack-cli --save-dev
tsconfig.json file:{
"compilerOptions": {
"target": "es5",
"module": "es6",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"sourceMap": true
},
"include": [
"src"
]
}
webpack.config.js file for bundling:const path = require('path');
module.exports = {
entry: './src/main.ts',
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
devtool: 'source-map',
};
/src folder and your TypeScript file inside it (src/main.ts):import * as PIXI from 'pixi.js';
const app = new PIXI.Application({
width: window.innerWidth,
height: window.innerHeight,
backgroundColor: 0x1099bb,
});
document.body.appendChild(app.view as HTMLCanvasElement);
// Define the splash screen properties
const splashScreenText = new PIXI.Text('Welcome to My Game', {
fontSize: 36,
fill: '#ffffff',
});
splashScreenText.anchor.set(0.5);
splashScreenText.x = app.screen.width / 2;
splashScreenText.y = app.screen.height / 2;
// Add splash screen text to the stage
app.stage.addChild(splashScreenText);
// Define the loading bar properties
const loadingBar = new PIXI.Graphics();
loadingBar.beginFill(0xffffff);
loadingBar.drawRect(0, 0, 300, 50);
loadingBar.endFill();
loadingBar.x = (app.screen.width - 300) / 2;
loadingBar.y = app.screen.height / 2 + 100;
const loadingBarProgress = new PIXI.Graphics();
loadingBarProgress.beginFill(0x00ff00);
loadingBarProgress.drawRect(0, 0, 0, 50);
loadingBarProgress.endFill();
loadingBarProgress.x = loadingBar.x;
loadingBarProgress.y = loadingBar.y;
// Add the loading bar to the stage
app.stage.addChild(loadingBar);
app.stage.addChild(loadingBarProgress);
PIXI.Assets.add('exampleAsset', 'path/to/your/asset.png'); // Add your assets here
// Load assets
PIXI.Assets.load('exampleAsset', (progress: number) => {
handleLoadingProgress(progress);
}).then(() => {
handleAssetsLoaded();
});
function handleLoadingProgress(progress: number) {
const progressWidth = (progress / 100) * 300;
loadingBarProgress.width = progressWidth;
}
function handleAssetsLoaded() {
// Remove splash screen and loading bar
app.stage.removeChild(splashScreenText);
app.stage.removeChild(loadingBar);
app.stage.removeChild(loadingBarProgress);
// Start your game logic here
startGame();
}
function startGame() {
const texture = PIXI.Assets.get('exampleAsset');
const exampleSprite = new PIXI.Sprite(texture);
exampleSprite.x = app.screen.width / 2;
exampleSprite.y = app.screen.height / 2;
exampleSprite.anchor.set(0.5);
app.stage.addChild(exampleSprite);
}
// Handle window resize
window.addEventListener('resize', () => {
app.renderer.resize(window.innerWidth, window.innerHeight);
splashScreenText.x = app.screen.width / 2;
splashScreenText.y = app.screen.height / 2;
loadingBar.x = (app.screen.width - 300) / 2;
loadingBar.y = app.screen.height / 2 + 100;
loadingBarProgress.x = loadingBar.x;
loadingBarProgress.y = loadingBar.y;
});
index.html):<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixi.js Game Skeleton</title>
<style>
body { margin: 0; padding: 0; overflow: hidden; }
#game-container { width: 100vw; height: 100vh; }
</style>
</head>
<body>
<div id="game-container"></div>
<script src="dist/bundle.js"></script>
</body>
</html>
To build and run your project, add the following scripts to your package.json:
"scripts": {
"build": "webpack --mode production",
"start": "webpack serve --open"
}
Finally, you can build your project with npm run build or start a development server with npm start.
This setup will create a basic Pixi.js game skeleton with a loading bar and splash screen using the latest Pixi.js version and TypeScript.