0% found this document useful (0 votes)
3 views

Source Code

Uploaded by

fajarpebefajar12
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Source Code

Uploaded by

fajarpebefajar12
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

=========================================================

========[Global Variable]
=========================================================

//konfigurasi screen game


var X_POSITION;
var Y_POSITION;
var layoutSize = {
'w':1280, 'h':720
}

//konfigurasi Attribut Player


var MoveSpeed = 300;

//Level Game
var LevelGame = 1;

// ==[Create By #EL►Razz]==

//============ Assets Configuration ===================

//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);

//set Title game


var titleGame = Menu.add.image(X_POSITION.CENTER, Y_POSITION.TOP + 250,
TitleGame);
//set Button Play
var buttonPlay = Menu.add.image(X_POSITION.CENTER, Y_POSITION.CENTER + 100,
ButtonPlay);
buttonPlay.setScale(2);
buttonPlay.setInteractive();

//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]

//set animasi button Play


function repeatAnimBtnPlay(object){
Menu.tweens.add({
targets: object,
ease: "Linear",
duration: 500,
yoyo: true,
scaleX: 2.2,
scaleY: 2.2,
repeat: 1,
onComplete: function(){
repeatAnimBtnPlay(object);
}
});
}
repeatAnimBtnPlay(buttonPlay);

//set pindah Layout


Menu.input.on("gameobjectdown", function(pointer, gameobject){
if (gameobject == buttonPlay){
this.music_menu.pause()
Menu.scene.start("TAsceneBermain");
}
}, this);
}

=========================================================
========[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,

//inisialisasi asset game ================================================


//set background
this.background = this.add.image(X_POSITION.CENTER, Y_POSITION.CENTER, BgMenu);
this.background.setScale(3);

//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 Score Coin


this.score = this.add.text(X_POSITION.CENTER,Y_POSITION.TOP + 150,"0", {
fontFamily : 'Verdana, Arial',
fontSize: '18pt',
color: "#ffffff",
stroke: "#000000",
strokeThickness: 5,
}).setScrollFactor(0);

this.score.setText("Coin : " + CoinInGame);


this.score.setDepth(40);
this.score.setOrigin(0.5);
//setCamera
this.cameras.main.setZoom(1.5);
// this.cameras.main.setZoom(0.8);

//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 Player Particle Move


this.MoveParticle = this.add.particles(TrailParticle);
this.MoveParticleEmit = this.MoveParticle.createEmitter({
x:0,
y:0,
angle:{min:0, max:45},
scale: {start:1, end:0},
opacity: {start:1, end:0},
blendMode: 'NORMAL',
lifespan: 400,
// gravityY: 600,
speedX:{min: -50, max: 10},
speedY:{min: -50, max: 10},
on: true,
follow: this.player,
});
this.MoveParticleEmit.emitParticle(10);
this.MoveParticle.setVisible(true);

//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);

let checkTemp = this.add.image(0,0,Coin).setVisible(false);


let coinSize = {"width":checkTemp.width, "height": checkTemp.height}
let coins = this.physics.add.staticGroup();
coins.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)

V_Platform(650, 750, 1, 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);

H_Platform(350, 500, 2, false, Ground1);


H_Platform(350, 300, 2, false, Ground1);
H_Platform(350, 100, 1, true, Ground1);

//CheckPoint
coins.create(400, 100,Coin);

//Section2 - Middle
H_Platform(450, 100, 1, false, Ground1)
H_Platform(650, 100, 1, true, Ground1)

H_Platform(600, 200, 1, false, Ground1)


H_Platform(500, 200, 1, false, Ground1)
H_Spike(600, 215, 1, true, trap1);
H_Spike(500, 215, 1, true, trap1);

//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);

H_Platform(550, 300, 1, false, Ground1)

H_Platform(450, 400, 1, false, Ground1)


H_Platform(650, 400, 1, false, Ground1)

H_Platform(550, 500, 1, false, Ground1)


H_Spike(550, 515, 1, true, trap1);

H_Platform(600, 600, 2, true, Ground1)


H_Platform(450, 600, 2, true, Ground1)

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);

H_Platform(750, 750, 1, true, Ground1);


H_Platform(750, 800, 26, true, Ground1);

H_Platform(950, 650, 1, true, Wall1);


H_Platform(950, 750, 1, true, Wall1);

//Traps
H_Spike(1000, 815, 1, true, trap1);
H_Spike(1100, 815, 1, true, trap1);
H_Spike(1200, 815, 1, true, trap1);

H_Platform(1250, 700, 1, true, Wall1);


H_Platform(1250, 750, 1, true, Wall1);

H_Platform(1350, 750, 1, true, Ground2);


H_Platform(1550, 764, 1, true, Ground3);
H_Platform(1750, 750, 1, true, Ground2);
H_Spike(1300, 815, 11, true, trap1);

H_Platform(1850, 750, 1, true, Wall1);


H_Platform(1850, 650, 1, true, Wall1);
V_Platform(2000, 900, 6, false, Wall1);

gates.create(2000 - gateSize.width, 860 - gateSize.height, Gate);


}

level1();

//Game Logic ========================================

var Game = this;

//Check Collition with moveable Platform


var CheckPlatCol = function(player, mvPlatforms){
PlatMove = true;
}
var PlayerHurt = function (player, spikes){
Restart();
}
var Win = function(player, gates){
Wins();
}
function Restart(){
Game.music_game.pause()
Game.walk.pause()
PlatMove = false;
Game.scene.start("TAsceneGameOver");
}
function Wins(){
Game.music_game.pause()
Game.walk.pause()
PlatMove = false;
CoinLast += CoinInGame;
Game.scene.start("TAsceneFinish");
}

//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);

//MovePlayer dengan ditekan


this.cursors = this.input.keyboard.createCursorKeys();
//!!!code untuk menjalankannya ada di update script
//Restart Scene Game
this.input.keyboard.on('keydown-R',function(input){
Game.walk.pause()
Game.music_game.pause()
Game.scene.restart();
},this)

//Back to scene Menu


this.input.keyboard.on('keydown-ESC',function(input){
Game.walk.pause()
this.scene.start("TAsceneMenu");
},this)
//Refresh Page
this.input.keyboard.on('keydown-N',function(input){
window.location.reload();
},this)

//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;

//MovePlayer dengan tekan keyboard


if(this.cursors.right.isDown){
//Move ketika klik arrow kanan
this.player.setVelocityX(MoveSpeed);

//set Animasi Player Ketika Klik Arrow Kanan


this.player.anims.play('right', true);

//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));

//set Animasi Player Ketika Klik Arrow kiri


this.player.anims.play('left', true);

//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);

//set Animasi Player Ketika tidak klik apapun


this.player.anims.play('front', true);

this.walk.setVolume(0);
//Flip Player
// this.player.flipX = false;
//hide Particle
// this.MoveParticle.setVisible(false);
}

//MoveJump dengan keyboard


if(this.cursors.up.isDown && this.player.body.touching.down){
//MoveJump ketika klik arrow atas
this.player.setVelocityY(-450);

this.playerJump.play();
}

//Cek ketika player melayang


if (!this.player.body.touching.down){
//set Animasi ketika melayang
this.player.anims.play('jump', true);
//hide Particle
this.MoveParticle.setVisible(true);

this.walk.setVolume(0);
}

//Moving Platform
if (PlatMove){
this.mvPlatforms.setVelocityX(100);
}

if (this.mvPlatforms.body.x >= 625){


PlatMove = false;
this.mvPlatforms.setVelocityX(0);
}
}

=========================================================
========[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()

var Games = this;


//Background
this.bg = this.add.image(X_POSITION.CENTER, Y_POSITION.CENTER, BgMenu);
this.bg.setScale(2);
this.bg.setTint(" 0xff8c8c");

//set Text Kalah


this.titleWin = this.add.image(X_POSITION.CENTER, Y_POSITION.TOP + 200,
TitleLose)

//button Home
var buttonHome = Games.add.image(X_POSITION.CENTER, Y_POSITION.CENTER,
ButtonHome)
buttonHome.setInteractive();

Games.input.on("gameobjectdown", function(pointer, gameobject){


if (gameobject == buttonHome){
// console.log(Games)
CoinInGame = 0;
Games.scene.start("TAsceneMenu")
}
},this);

//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();

//set animasi button Play


function repeatAnimBtnPlay(object){
Games.tweens.add({
targets: object,
ease: "Linear",
duration: 500,
yoyo: true,
scaleX: 1.2,
scaleY: 1.2,
repeat: 1,
onComplete: function(){
repeatAnimBtnPlay(object);
}
});
}
repeatAnimBtnPlay(this.titleWin);
}

=========================================================
========[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()

var Games = this;


//Background
this.bg = this.add.image(X_POSITION.CENTER, Y_POSITION.CENTER, BgMenu);
this.bg.setScale(2);
this.bg.setTint("0x8cffad");

//set Text Kalah


this.titleWin = this.add.image(X_POSITION.CENTER, Y_POSITION.TOP + 200,
TitleWin)

//button Home
var buttonHome = Games.add.image(X_POSITION.CENTER, Y_POSITION.CENTER,
ButtonHome)
buttonHome.setInteractive();

Games.input.on("gameobjectdown", function(pointer, gameobject){


if (gameobject == buttonHome){
// console.log(Games)
CoinInGame = 0;
Games.scene.start("TAsceneMenu")
}
},this);

//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();

//set animasi button Play


function repeatAnimBtnPlay(object){
Games.tweens.add({
targets: object,
ease: "Linear",
duration: 500,
yoyo: true,
scaleX: 1.2,
scaleY: 1.2,
repeat: 1,
onComplete: function(){
repeatAnimBtnPlay(object);
}
});
}
repeatAnimBtnPlay(this.titleWin);
}

You might also like