/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Ball = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.gravity = 0.6; self.bounceX = 0.8; self.bounceY = 0.7; self.groundY = 2432; self.lastHitBy = null; self.hit = function (character, forceX, forceY) { self.velocityX = forceX; self.velocityY = forceY; self.lastHitBy = character; }; self.update = function () { // Apply gravity self.velocityY += self.gravity; // Update position self.x += self.velocityX; self.y += self.velocityY; // Ground bounce if (self.y >= self.groundY - 30) { self.y = self.groundY - 30; self.velocityY *= -self.bounceY; self.velocityX *= self.bounceX; LK.getSound('bounce').play(); } // Side boundaries if (self.x <= 30) { self.x = 30; self.velocityX *= -self.bounceX; } else if (self.x >= 2018) { self.x = 2018; self.velocityX *= -self.bounceX; } // Net collision if (self.x > 1014 && self.x < 1034 && self.y > 2132) { if (self.velocityX > 0) { self.x = 1014; } else { self.x = 1034; } self.velocityX *= -self.bounceX; } }; return self; }); var Crewmate = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('crewmate', { anchorX: 0.5, anchorY: 1.0 }); self.velocityY = 0; self.isGrounded = false; self.jumpPower = -18; self.gravity = 0.8; self.groundY = 2432; self.speed = 8; self.courtMinX = 50; self.courtMaxX = 974; self.jump = function () { if (self.isGrounded) { self.velocityY = self.jumpPower; self.isGrounded = false; } }; self.moveLeft = function () { if (self.x > self.courtMinX) { self.x -= self.speed; } }; self.moveRight = function () { if (self.x < self.courtMaxX) { self.x += self.speed; } }; self.update = function () { // Apply gravity self.velocityY += self.gravity; self.y += self.velocityY; // Ground collision if (self.y >= self.groundY) { self.y = self.groundY; self.velocityY = 0; self.isGrounded = true; } }; return self; }); var Opponent = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('opponent', { anchorX: 0.5, anchorY: 1.0 }); self.velocityY = 0; self.isGrounded = false; self.jumpPower = -18; self.gravity = 0.8; self.groundY = 2432; self.speed = 6; self.courtMinX = 1074; self.courtMaxX = 1998; self.lastJumpTime = 0; self.jump = function () { if (self.isGrounded && LK.ticks - self.lastJumpTime > 30) { self.velocityY = self.jumpPower; self.isGrounded = false; self.lastJumpTime = LK.ticks; } }; self.update = function () { // Apply gravity self.velocityY += self.gravity; self.y += self.velocityY; // Ground collision if (self.y >= self.groundY) { self.y = self.groundY; self.velocityY = 0; self.isGrounded = true; } // AI behavior if (ball) { var distanceToBall = Math.abs(self.x - ball.x); // Move towards ball if it's on opponent's side if (ball.x > 1024) { if (ball.x > self.x && self.x < self.courtMaxX) { self.x += self.speed; } else if (ball.x < self.x && self.x > self.courtMinX) { self.x -= self.speed; } // Jump if ball is close and low enough if (distanceToBall < 150 && ball.y > 2200 && ball.velocityY > 0) { self.jump(); } } } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ // Game variables var player; var opponent; var ball; var net; var ground; var playerScore = 0; var opponentScore = 0; var dragNode = null; var gameWinScore = 11; // Create ground ground = game.addChild(LK.getAsset('ground', { anchorX: 0, anchorY: 1, x: 0, y: 2482 })); // Create net net = game.addChild(LK.getAsset('net', { anchorX: 0.5, anchorY: 1, x: 1024, y: 2432 })); // Create player player = game.addChild(new Crewmate()); player.x = 512; player.y = 2432; // Create opponent opponent = game.addChild(new Opponent()); opponent.x = 1536; opponent.y = 2432; // Create ball ball = game.addChild(new Ball()); ball.x = 1024; ball.y = 2200; // Create score display var scoreText = new Text2('Player: 0 | Opponent: 0', { size: 80, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); function updateScore() { scoreText.setText('Player: ' + playerScore + ' | Opponent: ' + opponentScore); } function resetBall() { ball.x = 1024; ball.y = 2200; ball.velocityX = 0; ball.velocityY = 0; } function checkScore() { // Check if ball hits player's court if (ball.y >= 2400 && ball.x < 1024) { opponentScore++; LK.getSound('score').play(); updateScore(); resetBall(); if (opponentScore >= gameWinScore) { LK.showGameOver(); } } // Check if ball hits opponent's court else if (ball.y >= 2400 && ball.x > 1024) { playerScore++; LK.setScore(playerScore); LK.getSound('score').play(); updateScore(); resetBall(); if (playerScore >= gameWinScore) { LK.showYouWin(); } } } function handleMove(x, y, obj) { if (dragNode) { dragNode.x = x; // Keep player in bounds if (dragNode.x < dragNode.courtMinX) { dragNode.x = dragNode.courtMinX; } else if (dragNode.x > dragNode.courtMaxX) { dragNode.x = dragNode.courtMaxX; } } } game.move = handleMove; game.down = function (x, y, obj) { // Check if touching player's side for dragging if (x < 1024) { dragNode = player; handleMove(x, y, obj); } }; game.up = function (x, y, obj) { // Jump on touch release if (dragNode) { dragNode.jump(); } dragNode = null; }; game.update = function () { // Check ball collision with player if (player.intersects(ball) && ball.lastHitBy !== player) { var forceX = (ball.x - player.x) * 0.3 + 8; var forceY = -12; ball.hit(player, forceX, forceY); } // Check ball collision with opponent if (opponent.intersects(ball) && ball.lastHitBy !== opponent) { var forceX = (ball.x - opponent.x) * 0.3 - 8; var forceY = -12; ball.hit(opponent, forceX, forceY); } checkScore(); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.gravity = 0.6;
self.bounceX = 0.8;
self.bounceY = 0.7;
self.groundY = 2432;
self.lastHitBy = null;
self.hit = function (character, forceX, forceY) {
self.velocityX = forceX;
self.velocityY = forceY;
self.lastHitBy = character;
};
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
// Update position
self.x += self.velocityX;
self.y += self.velocityY;
// Ground bounce
if (self.y >= self.groundY - 30) {
self.y = self.groundY - 30;
self.velocityY *= -self.bounceY;
self.velocityX *= self.bounceX;
LK.getSound('bounce').play();
}
// Side boundaries
if (self.x <= 30) {
self.x = 30;
self.velocityX *= -self.bounceX;
} else if (self.x >= 2018) {
self.x = 2018;
self.velocityX *= -self.bounceX;
}
// Net collision
if (self.x > 1014 && self.x < 1034 && self.y > 2132) {
if (self.velocityX > 0) {
self.x = 1014;
} else {
self.x = 1034;
}
self.velocityX *= -self.bounceX;
}
};
return self;
});
var Crewmate = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('crewmate', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityY = 0;
self.isGrounded = false;
self.jumpPower = -18;
self.gravity = 0.8;
self.groundY = 2432;
self.speed = 8;
self.courtMinX = 50;
self.courtMaxX = 974;
self.jump = function () {
if (self.isGrounded) {
self.velocityY = self.jumpPower;
self.isGrounded = false;
}
};
self.moveLeft = function () {
if (self.x > self.courtMinX) {
self.x -= self.speed;
}
};
self.moveRight = function () {
if (self.x < self.courtMaxX) {
self.x += self.speed;
}
};
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
self.y += self.velocityY;
// Ground collision
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocityY = 0;
self.isGrounded = true;
}
};
return self;
});
var Opponent = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('opponent', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityY = 0;
self.isGrounded = false;
self.jumpPower = -18;
self.gravity = 0.8;
self.groundY = 2432;
self.speed = 6;
self.courtMinX = 1074;
self.courtMaxX = 1998;
self.lastJumpTime = 0;
self.jump = function () {
if (self.isGrounded && LK.ticks - self.lastJumpTime > 30) {
self.velocityY = self.jumpPower;
self.isGrounded = false;
self.lastJumpTime = LK.ticks;
}
};
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
self.y += self.velocityY;
// Ground collision
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocityY = 0;
self.isGrounded = true;
}
// AI behavior
if (ball) {
var distanceToBall = Math.abs(self.x - ball.x);
// Move towards ball if it's on opponent's side
if (ball.x > 1024) {
if (ball.x > self.x && self.x < self.courtMaxX) {
self.x += self.speed;
} else if (ball.x < self.x && self.x > self.courtMinX) {
self.x -= self.speed;
}
// Jump if ball is close and low enough
if (distanceToBall < 150 && ball.y > 2200 && ball.velocityY > 0) {
self.jump();
}
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var player;
var opponent;
var ball;
var net;
var ground;
var playerScore = 0;
var opponentScore = 0;
var dragNode = null;
var gameWinScore = 11;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 1,
x: 0,
y: 2482
}));
// Create net
net = game.addChild(LK.getAsset('net', {
anchorX: 0.5,
anchorY: 1,
x: 1024,
y: 2432
}));
// Create player
player = game.addChild(new Crewmate());
player.x = 512;
player.y = 2432;
// Create opponent
opponent = game.addChild(new Opponent());
opponent.x = 1536;
opponent.y = 2432;
// Create ball
ball = game.addChild(new Ball());
ball.x = 1024;
ball.y = 2200;
// Create score display
var scoreText = new Text2('Player: 0 | Opponent: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
function updateScore() {
scoreText.setText('Player: ' + playerScore + ' | Opponent: ' + opponentScore);
}
function resetBall() {
ball.x = 1024;
ball.y = 2200;
ball.velocityX = 0;
ball.velocityY = 0;
}
function checkScore() {
// Check if ball hits player's court
if (ball.y >= 2400 && ball.x < 1024) {
opponentScore++;
LK.getSound('score').play();
updateScore();
resetBall();
if (opponentScore >= gameWinScore) {
LK.showGameOver();
}
}
// Check if ball hits opponent's court
else if (ball.y >= 2400 && ball.x > 1024) {
playerScore++;
LK.setScore(playerScore);
LK.getSound('score').play();
updateScore();
resetBall();
if (playerScore >= gameWinScore) {
LK.showYouWin();
}
}
}
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
// Keep player in bounds
if (dragNode.x < dragNode.courtMinX) {
dragNode.x = dragNode.courtMinX;
} else if (dragNode.x > dragNode.courtMaxX) {
dragNode.x = dragNode.courtMaxX;
}
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
// Check if touching player's side for dragging
if (x < 1024) {
dragNode = player;
handleMove(x, y, obj);
}
};
game.up = function (x, y, obj) {
// Jump on touch release
if (dragNode) {
dragNode.jump();
}
dragNode = null;
};
game.update = function () {
// Check ball collision with player
if (player.intersects(ball) && ball.lastHitBy !== player) {
var forceX = (ball.x - player.x) * 0.3 + 8;
var forceY = -12;
ball.hit(player, forceX, forceY);
}
// Check ball collision with opponent
if (opponent.intersects(ball) && ball.lastHitBy !== opponent) {
var forceX = (ball.x - opponent.x) * 0.3 - 8;
var forceY = -12;
ball.hit(opponent, forceX, forceY);
}
checkScore();
};