Source Code
Source Code
========[Global Variable]
=========================================================
//Level Game
var LevelGame = 1;
// ==[Create By #EL►Razz]==
//Sound
var MusicMenu = "music_menu"
var MusicGame = "music_play"
var PlayerWalk = "snd_walk"
var PlayerJumps = "snd_jump"
var CollectCoin = "snd_coin"
var GameOver = "snd_gameover"
var Win = "snd_win"
//SceneMenu
var TitleGame = "title_game"
var BgMenu = "bg_menu"
var ButtonPlay = "button_play"
var TitleLose = "title_lose"
var TitleWin = "title_win"
var ButtonHome = "button_home"
//Game
var Player = "dino"
var PlayerJump = "dinoJump"
var xPlayer = 350;
var yPlayer = 700;
var xPlayer_First = 350;
var yPlayer_First = 700;
//Debug
// var xPlayer_First = 1900;
// var yPlayer_First = 700;
//Particle
var ParticleLose = "particle_lose"
var ParticleWin = "particle_win"
var TrailParticle = "particle";
//Obstacle
var Ground1 = "platform"
var Wall1 = "separator"
var MovePlat = "mv_platform"
var PlatMove;
var trap1 = "spike"
var Ground2 = "platform2"
var Ground3 = "platform3"
var Coin = "coin"
//Coin Couinter
var CoinInGame = 0;
var CoinLast = 0;
// FInish
var Gate = "gate"
=========================================================
========[Scene Menu - Create]
=========================================================
function create(){
//setup scene
// melakukan inisialisai pada variabel pembantu ==========================
X_POSITION =
{
'LEFT': 0,
'CENTER': game.canvas.width / 2,
'RIGHT': game.canvas.width,
};
Y_POSITION =
{
'TOP': 0,
'CENTER': game.canvas.height / 2,
'BOTTOM': game.canvas.height,
}
// menampung scene yang sedang
// aktif ke dalam variabel 'sceneMenu'
var Menu = this;
// == Menambahkan assets ==
//Sound
//backsound Menu
this.music_menu = this.sound.add(MusicMenu);
this.music_menu.loop = true;
this.music_menu.play();
//set background
Menu.background = Menu.add.image(X_POSITION.CENTER, Y_POSITION.CENTER, BgMenu);
Menu.background.setScale(1.5);
//AddText
var MyCoin = this.add.text(X_POSITION.CENTER, Y_POSITION.BOTTOM - 100, "0",{
fontFamily: "Verdana, Arial",
fontSize: '18pt',
color: "#ffffff",
stroke: "#000000",
strokeThickness: 5,
})
MyCoin.setOrigin(0.5,0);
MyCoin.setText("Koin Kamu : " + CoinLast);
//[Code by #FPB]
=========================================================
========[Scene Bermain - Create]
=========================================================
function create(){
//setup scene
//Setup Sound
//background game
this.music_game = this.sound.add(MusicGame);
this.music_game.loop = true;
this.music_game.play();
//sound playerDino
this.walk = this.sound.add(PlayerWalk);
this.collectCoin = this.sound.add(CollectCoin);
this.playerJump = this.sound.add(PlayerJumps);
this.walk.play();
this.walk.loop = true;
// this.walk.setVolume(0);
var theCoin;
var hurt = false;
// melakukan inisialisai pada variabel pembantu ==========================
X_POSITION =
{
'LEFT': 0,
'CENTER': game.canvas.width / 2,
'RIGHT': game.canvas.width,
};
Y_POSITION =
{
'TOP': 0,
'CENTER': game.canvas.height / 2,
'BOTTOM': game.canvas.height,
//Tutorial text
this.Tutorial = this.add.text(X_POSITION.CENTER, Y_POSITION.CENTER + 150,"0", {
fontFamily : 'Verdana, Arial',
fontSize: '12pt',
color: "#ffffff",
stroke: "#000000",
strokeThickness: 5,
}).setScrollFactor(0);
this.Tutorial.setText("Tekan ← atau → untuk bergerak \n Tekan ↑ untuk
lompat");
this.Tutorial.setOrigin(0.5);
this.Tutorial.setDepth(50)
//set playerDino
this.player = this.physics.add.sprite(xPlayer_First, yPlayer_First, Player);
this.player.setGravity(0, 500);
this.player.setDepth(30);
this.player.setScale(2);
this.player.setSize(8,16);
// this.player.setBounce(0.2);
// this.player.setCollideWorldBounds(true);\
//Coin Particle
this.coinParticle = this.add.particles(Coin);
this.emitCoin = this.coinParticle.createEmitter({
speed: {min:150, max: 250},
scale: {start:1, end:0},
opacity: {min:100, max:0},
gravity:{x:0, y:150},
quantity: {min:5, max:15},
lifespan: {min:200, max:800}
})
this.emitCoin.setPosition(-100, -100);
this.emitCoin.explode();
//set Platform
//Static Plaforms
let groundTemp = this.add.image(0,0,Ground1).setVisible(false);
let groundSize = {'width':groundTemp.width, 'height':groundTemp.height}
var platforms = this.physics.add.staticGroup();
platforms.setDepth(20);
//Dynamic platforms
this.mvPlatforms = this.physics.add.sprite(550, 750, MovePlat);
this.mvPlatforms.body.allowGravity = false;
this.mvPlatforms.body.immovable = true;
//Traps platforms
let spikeTemp = this.add.image(450,700,trap1).setVisible(false);
let spikeSize = {"width":spikeTemp.width, "height": spikeTemp.height}
let spikes = this.physics.add.staticGroup();
spikes.setDepth(20);
//Gates
let gateTemp = this.add.image(1900,700,Gate).setVisible(false);
let gateSize = {"width":gateTemp.width, "height": gateTemp.height}
let gates = this.physics.add.staticGroup();
gates.setDepth(10);
//set platform
//Static Platform
//Fungsi membuat platform horizontal
function H_Platform(x,y, value, forward, assets){
for(let i = 0; i < value; i++){
if (forward){
platforms.create(x + groundSize.width * i, y + groundSize.height,
assets);
} else {
platforms.create(x - groundSize.width * i, y + groundSize.height,
assets);
}
}
}
//Fungsi membuat platform Vertical
function V_Platform(x,y, value, forward, assets){
for(let i = 0; i < value; i++){
if(forward){
platforms.create(x + groundSize.width, y + groundSize.height* i,
assets);
} else {
platforms.create(x + groundSize.width, y - groundSize.height* i,
assets);
}
}
}
//Fungsi membuat platform horizontal
function H_Spike(x,y, value, forward, assets){
for(let i = 0; i < value; i++){
if (forward){
spikes.create(x + spikeSize.width * i, y + spikeSize.height,
assets);
} else {
spikes.create(x - spikeSize.width * i, y + spikeSize.height,
assets);
}
}
}
//Fungsi membuat platform Vertical
function V_Spike(x,y, value, forward, assets){
for(let i = 0; i < value; i++){
if(forward){
spikes.create(x + spikeSize.width, y + spikeSize.height* i,
assets);
} else {
spikes.create(x + spikeSize.width, y - spikeSize.height* i,
assets);
}
}
}
//Setting Level
function level1(){
//Separator_Square
H_Platform(50, -50, 14, true, Wall1)
H_Platform(0, 750, 15, true, Wall1)
//Separator
V_Platform(-50, 0, 16, true, Wall1)
//Separator
V_Platform(350, 150, 13, true, Wall1)
//Separator
V_Platform(650, 0, 14, true, Wall1)
//Section1 - left
H_Platform(50, 700, 7, true, Ground1);
H_Platform(50, 650, 3, true, Ground1);
H_Platform(50, 600, 2, true, Ground1);
H_Platform(50, 400, 2, true, Ground1);
H_Platform(50, 200, 2, true, Ground1);
coins.create(150, 650,Coin);
coins.create(50, 400,Coin);
coins.create(50, 200,Coin);
//CheckPoint
coins.create(400, 100,Coin);
//Section2 - Middle
H_Platform(450, 100, 1, false, Ground1)
H_Platform(650, 100, 1, true, Ground1)
//coins
coins.create(650, 100,Coin);
coins.create(550, 300,Coin);
coins.create(550, 300,Coin);
coins.create(450, 400,Coin);
coins.create(650, 400,Coin);
coins.create(550, 450,Coin);
coins.create(600, 725,Coin);
//Trap
H_Spike(450, 765, 5, true, trap1);
//Section3 Lorong
H_Platform(750, 600, 26, true, Wall1);
V_Platform(650, 850, 2, true, Wall1);
H_Platform(750, 850, 26, true, Wall1);
//Coin
coins.create(1050, 800,Coin);
coins.create(1150, 800,Coin);
coins.create(1250, 700,Coin);
coins.create(1350, 750,Coin);
coins.create(1550, 765,Coin);
coins.create(1750, 750,Coin);
//Traps
H_Spike(1000, 815, 1, true, trap1);
H_Spike(1100, 815, 1, true, trap1);
H_Spike(1200, 815, 1, true, trap1);
level1();
//Collition Check
//set Collider Player dengan Platform
this.physics.add.collider(this.player, platforms, null, null);
this.physics.add.collider(this.player, this.mvPlatforms, CheckPlatCol, null);
this.physics.add.collider(this.player, spikes, PlayerHurt, null);
this.physics.add.collider(this.player, coins, function(player, coin){
coin.destroy();
CoinInGame +=1;
Game.collectCoin.play()
Game.emitCoin.setPosition(player.body.x, player.body.y);
Game.emitCoin.explode();
Game.Tutorial.setVisible(false);
}, null);
this.physics.add.collider(this.player, gates, Win, null);
//Player Animation========================================
//Animasi Player Ketika Klik arrow Kanan
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers(Player, {start: 1 , end: 6 }),
frameRate: 12,
repeat: -1
});
//Animasi Player Ketika Klik arrow Kiri
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers(Player, {start: 1 , end: 6 }),
frameRate: 12,
repeat: -1
});
//Animasi Player Ketika Klik arrow Kiri
this.anims.create({
key: 'front',
frames: [{key: Player, frames:0}],
frameRate: 12,
});
//Animasi Player Ketika Klik arrow Kiri
this.anims.create({
key: 'jump',
frames: [{key: PlayerJump, frames:0}],
frameRate: 12,
});
=========================================================
========[Scene Bermain - Update]
=========================================================
function update(){
//For debugger body player
xPlayer = this.player.body.x;
yPlayer = this.player.body.y;
//FollowPlayer
this.cameras.main.pan(xPlayer, yPlayer, 1, 'Linear');
//set Coin
this.score.setText("Coin : " + CoinInGame);
//MovementPlatform
let moveRight = false;
//Flip Player
this.player.flipX = false;
//debugMovement
console.clear();
console.log({x: xPlayer, y: yPlayer});
this.MoveParticle.setVisible(true);
this.walk.setVolume(1);
} else if(this.cursors.left.isDown){
//Move ketika klik arrow kiri
this.player.setVelocityX(-Math.abs(MoveSpeed));
//Flip Player
this.player.flipX = true;
//debugMovement
console.clear();
console.log({x: xPlayer, y: yPlayer});
this.MoveParticle.setVisible(true);
this.walk.setVolume(1);
}else {
//Ketika tidak klik apapun
this.player.setVelocityX(0);
this.walk.setVolume(0);
//Flip Player
// this.player.flipX = false;
//hide Particle
// this.MoveParticle.setVisible(false);
}
this.playerJump.play();
}
this.walk.setVolume(0);
}
//Moving Platform
if (PlatMove){
this.mvPlatforms.setVelocityX(100);
}
=========================================================
========[Scene GameOver - Create]
=========================================================
function create(){
X_POSITION =
{
'LEFT': 0,
'CENTER': game.canvas.width / 2,
'RIGHT': game.canvas.width,
};
Y_POSITION =
{
'TOP': 0,
'CENTER': game.canvas.height / 2,
'BOTTOM': game.canvas.height,
this.snd_gameover = this.sound.add(GameOver)
this.snd_gameover.play()
//button Home
var buttonHome = Games.add.image(X_POSITION.CENTER, Y_POSITION.CENTER,
ButtonHome)
buttonHome.setInteractive();
//Lose Particle
this.loseParticle = this.add.particles(ParticleLose);
this.emitLose = this.loseParticle.createEmitter({
speed: {min:150, max: 2000},
scale: {start:0.1, end:0.2},
gravityY: 500,
quantity: 50,
lifespan: 5000
})
this.emitLose.setPosition(X_POSITION.CENTER, Y_POSITION.CENTER);
this.emitLose.explode();
=========================================================
========[Scene Finish]
=========================================================
function create(){
X_POSITION =
{
'LEFT': 0,
'CENTER': game.canvas.width / 2,
'RIGHT': game.canvas.width,
};
Y_POSITION =
{
'TOP': 0,
'CENTER': game.canvas.height / 2,
'BOTTOM': game.canvas.height,
this.snd_Win = this.sound.add(Win)
this.snd_Win.play()
//button Home
var buttonHome = Games.add.image(X_POSITION.CENTER, Y_POSITION.CENTER,
ButtonHome)
buttonHome.setInteractive();
//Win Particle
this.winParticle = this.add.particles(ParticleWin);
this.emitWin = this.winParticle.createEmitter({
speed: {min:150, max: 2000},
scale: {start:0.1, end:1.3},
gravityY: 500,
quantity: 50,
lifespan: 5000
})
this.emitWin.setPosition(X_POSITION.CENTER, Y_POSITION.CENTER);
this.emitWin.explode();