loading...
上次更新: 2025-02-06
有興趣的話可以先試玩我製作的 Pixel Adventure,按照這系列文章,我們會做出一個類似這樣的簡易版遊戲。
v7,寫法會稍有不同,不過還是建議先看完那篇,來了解 PixiJS 的基本知識與運作,在實作時會更得心應手。npm create vite@latest my-game -- --template vite-ts
public 資料夾下載。專案/
├─ public/ 📌 遊戲需要用的json檔, 圖片等資源
│ ├─ character/
│ ├─ fruit/
│ ├─ background/
│ ├─ ...
├─ constants/ 📌 遊戲參數
│ ├─ config.ts
│ ├─ fruit.ts
│ ├─ ...
├─ src/
│ ├─ components/ 📌 元件
│ │ ├─ 元件1
│ │ ├─ 元件2
│ ├─ app.ts 📌 在這邊創建 pixi app
│ ├─ index.ts 📌 在這邊寫遊戲邏輯
├─ index.html
├─ package.json
#screen 裡#screen 的寬度為 608px, 高度為 368px,在之後遊戲舞台就會固定使用這寬高<html>
<head>
<title>Pixi Platformer Game</title>
<meta charset="UTF-8" />
<style>
.h1 {
color: #fff;
font-size: 24px;
letter-spacing: 0.25ch;
}
body {
margin: 0;
display: flex;
height: 100vh;
background-color: rgba(26, 26, 43, 1);
}
#app {
margin: auto;
display: flex;
flex-direction: column;
align-items: center;
}
#screen {
width: 608px;
height: 368px;
background-color: #fff;
}
</style>
</head>
<body>
<div id="app">
<h1 class="h1">Pixel Adventure</h1>
<div id="screen"></div>
</div>
<script src="src/index.ts"></script>
</body>
</html>在 app.ts 創建一個
import { Application } from 'pixi.js';
// 創建 pixi app
const app = new Application();
(async () => {
await app.init({
background: '#1099bb',
width: 608,
height: 368,
});
// 放到 DOM 裡面
document.querySelector('#screen')?.appendChild(app.canvas);
})();
export { app };在 index.ts 中引入
import './app';接著查看畫面,若沒出錯的話,畫面應會長這樣:
沒問題的話就可以前往下一篇了🎉
