/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var AIStick = Container.expand(function () { var self = Container.call(this); var stickGraphics = self.attachAsset('aiStick', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 0.08; // Will be adjusted based on difficulty self.update = function () { // AI behavior based on puck position var targetX = puck.x; var targetY = game.rink.y - game.rink.height / 4; // Adjust AI difficulty if (aiDifficulty === 'easy') { self.speed = 0.05; targetX += (Math.random() - 0.5) * 100; // Add some randomness } else if (aiDifficulty === 'medium') { self.speed = 0.08; targetX += (Math.random() - 0.5) * 50; } else if (aiDifficulty === 'hard') { self.speed = 0.12; // Hard AI tries to intercept puck path if (puck.velocityY < 0) { targetX = puck.x + puck.velocityX * 10; } } // Move toward target var dx = targetX - self.x; var dy = targetY - self.y; self.x += dx * self.speed; self.y += dy * self.speed; // Constrain to AI half var rinkTop = game.rink.y - game.rink.height / 2; var rinkLeft = game.rink.x - game.rink.width / 2; var rinkRight = game.rink.x + game.rink.width / 2; self.x = Math.max(rinkLeft + 40, Math.min(rinkRight - 40, self.x)); self.y = Math.max(rinkTop + 40, Math.min(game.rink.y + 50, self.y)); // Check for puck collision puck.hit(self); }; return self; }); var PlayerStick = Container.expand(function () { var self = Container.call(this); var stickGraphics = self.attachAsset('playerStick', { anchorX: 0.5, anchorY: 0.5 }); self.targetX = 0; self.targetY = 0; self.update = function () { // Smooth movement toward target var dx = self.targetX - self.x; var dy = self.targetY - self.y; self.x += dx * 0.15; self.y += dy * 0.15; // Constrain to player half var rinkBottom = game.rink.y + game.rink.height / 2; var rinkLeft = game.rink.x - game.rink.width / 2; var rinkRight = game.rink.x + game.rink.width / 2; self.x = Math.max(rinkLeft + 40, Math.min(rinkRight - 40, self.x)); self.y = Math.max(game.rink.y - 50, Math.min(rinkBottom - 40, self.y)); // Check for puck collision puck.hit(self); }; return self; }); var Puck = Container.expand(function () { var self = Container.call(this); var puckGraphics = self.attachAsset('puck', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.friction = 0.98; self.bounceReduction = 0.7; self.update = function () { // Apply movement self.x += self.velocityX; self.y += self.velocityY; // Apply friction self.velocityX *= self.friction; self.velocityY *= self.friction; // Maintain minimum velocity to prevent stopping var currentSpeed = Math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY); var minSpeed = 0.5; if (currentSpeed > 0 && currentSpeed < minSpeed) { var speedRatio = minSpeed / currentSpeed; self.velocityX *= speedRatio; self.velocityY *= speedRatio; } // Boundary collision var rinkLeft = game.rink.x - game.rink.width / 2; var rinkRight = game.rink.x + game.rink.width / 2; var rinkTop = game.rink.y - game.rink.height / 2; var rinkBottom = game.rink.y + game.rink.height / 2; if (self.x < rinkLeft + 20 || self.x > rinkRight - 20) { self.velocityX *= -self.bounceReduction; self.x = Math.max(rinkLeft + 20, Math.min(rinkRight - 20, self.x)); } if (self.y < rinkTop + 20 || self.y > rinkBottom - 20) { self.velocityY *= -self.bounceReduction; self.y = Math.max(rinkTop + 20, Math.min(rinkBottom - 20, self.y)); } // Check for goals if (self.y < rinkTop + 100 && self.x > game.rink.x - 150 && self.x < game.rink.x + 150) { // Top goal (player scores) playerScore++; self.resetPosition(); LK.getSound('goal').play(); LK.setScore(playerScore); updateScore(); if (playerScore >= winScore) { LK.showYouWin(); } } else if (self.y > rinkBottom - 100 && self.x > game.rink.x - 150 && self.x < game.rink.x + 150) { // Bottom goal (AI scores) aiScore++; self.resetPosition(); LK.getSound('goal').play(); updateScore(); if (aiScore >= winScore) { LK.showGameOver(); } } }; self.resetPosition = function () { self.x = game.rink.x; self.y = game.rink.y; self.velocityX = 0; self.velocityY = 0; }; self.hit = function (stick) { var dx = self.x - stick.x; var dy = self.y - stick.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 60) { var force = 15; self.velocityX = dx / distance * force; self.velocityY = dy / distance * force; LK.getSound('hit').play(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x228B22 }); /**** * Game Code ****/ // Game variables var playerScore = 0; var aiScore = 0; var winScore = 3; var aiDifficulty = 'medium'; // 'easy', 'medium', 'hard' var gameStarted = false; // Create rink game.rink = game.addChild(LK.getAsset('rink', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Create center line var centerLine = game.addChild(LK.getAsset('centerLine', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Create goals var topGoal = game.addChild(LK.getAsset('goal', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: game.rink.y - game.rink.height / 2 + 50 })); var bottomGoal = game.addChild(LK.getAsset('goal', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: game.rink.y + game.rink.height / 2 - 50 })); // Create goal posts var topLeftPost = game.addChild(LK.getAsset('goalPost', { anchorX: 0.5, anchorY: 0.5, x: topGoal.x - 150, y: topGoal.y - 75 })); var topRightPost = game.addChild(LK.getAsset('goalPost', { anchorX: 0.5, anchorY: 0.5, x: topGoal.x + 150, y: topGoal.y - 75 })); var bottomLeftPost = game.addChild(LK.getAsset('goalPost', { anchorX: 0.5, anchorY: 0.5, x: bottomGoal.x - 150, y: bottomGoal.y + 75 })); var bottomRightPost = game.addChild(LK.getAsset('goalPost', { anchorX: 0.5, anchorY: 0.5, x: bottomGoal.x + 150, y: bottomGoal.y + 75 })); // Create game objects var puck = game.addChild(new Puck()); puck.resetPosition(); var playerStick = game.addChild(new PlayerStick()); playerStick.x = game.rink.x; playerStick.y = game.rink.y + game.rink.height / 4; var aiStick = game.addChild(new AIStick()); aiStick.x = game.rink.x; aiStick.y = game.rink.y - game.rink.height / 4; // UI Elements var scoreText = new Text2('Player: 0 - AI: 0', { size: 80, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); var difficultyText = new Text2('Difficulty: Medium', { size: 60, fill: 0xFFFFFF }); difficultyText.anchor.set(0.5, 0); difficultyText.y = 100; LK.gui.top.addChild(difficultyText); var instructionText = new Text2('Drag your stick to hit the puck!', { size: 50, fill: 0xFFFFFF }); instructionText.anchor.set(0.5, 1); LK.gui.bottom.addChild(instructionText); // Difficulty selector var easyButton = new Text2('EASY', { size: 60, fill: 0x00FF00 }); easyButton.anchor.set(0, 0.5); easyButton.x = 50; LK.gui.left.addChild(easyButton); var mediumButton = new Text2('MEDIUM', { size: 60, fill: 0xFFFF00 }); mediumButton.anchor.set(0, 0.5); mediumButton.x = 50; mediumButton.y = 100; LK.gui.left.addChild(mediumButton); var hardButton = new Text2('HARD', { size: 60, fill: 0xFF0000 }); hardButton.anchor.set(0, 0.5); hardButton.x = 50; hardButton.y = 200; LK.gui.left.addChild(hardButton); function updateScore() { scoreText.setText('Player: ' + playerScore + ' - AI: ' + aiScore); LK.setScore(playerScore); } function resetGame() { playerScore = 0; aiScore = 0; updateScore(); puck.resetPosition(); playerStick.x = game.rink.x; playerStick.y = game.rink.y + game.rink.height / 4; aiStick.x = game.rink.x; aiStick.y = game.rink.y - game.rink.height / 4; } // Event handlers easyButton.down = function () { aiDifficulty = 'easy'; difficultyText.setText('Difficulty: Easy'); resetGame(); }; mediumButton.down = function () { aiDifficulty = 'medium'; difficultyText.setText('Difficulty: Medium'); resetGame(); }; hardButton.down = function () { aiDifficulty = 'hard'; difficultyText.setText('Difficulty: Hard'); resetGame(); }; var isDragging = false; game.down = function (x, y, obj) { // Check if touch is in player area if (y > game.rink.y - 50) { isDragging = true; playerStick.targetX = x; playerStick.targetY = y; } }; game.move = function (x, y, obj) { if (isDragging && y > game.rink.y - 50) { playerStick.targetX = x; playerStick.targetY = y; } }; game.up = function (x, y, obj) { isDragging = false; }; game.update = function () { // Update all game objects puck.update(); playerStick.update(); aiStick.update(); }; // Initialize game updateScore();
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var AIStick = Container.expand(function () {
var self = Container.call(this);
var stickGraphics = self.attachAsset('aiStick', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0.08; // Will be adjusted based on difficulty
self.update = function () {
// AI behavior based on puck position
var targetX = puck.x;
var targetY = game.rink.y - game.rink.height / 4;
// Adjust AI difficulty
if (aiDifficulty === 'easy') {
self.speed = 0.05;
targetX += (Math.random() - 0.5) * 100; // Add some randomness
} else if (aiDifficulty === 'medium') {
self.speed = 0.08;
targetX += (Math.random() - 0.5) * 50;
} else if (aiDifficulty === 'hard') {
self.speed = 0.12;
// Hard AI tries to intercept puck path
if (puck.velocityY < 0) {
targetX = puck.x + puck.velocityX * 10;
}
}
// Move toward target
var dx = targetX - self.x;
var dy = targetY - self.y;
self.x += dx * self.speed;
self.y += dy * self.speed;
// Constrain to AI half
var rinkTop = game.rink.y - game.rink.height / 2;
var rinkLeft = game.rink.x - game.rink.width / 2;
var rinkRight = game.rink.x + game.rink.width / 2;
self.x = Math.max(rinkLeft + 40, Math.min(rinkRight - 40, self.x));
self.y = Math.max(rinkTop + 40, Math.min(game.rink.y + 50, self.y));
// Check for puck collision
puck.hit(self);
};
return self;
});
var PlayerStick = Container.expand(function () {
var self = Container.call(this);
var stickGraphics = self.attachAsset('playerStick', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.update = function () {
// Smooth movement toward target
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
self.x += dx * 0.15;
self.y += dy * 0.15;
// Constrain to player half
var rinkBottom = game.rink.y + game.rink.height / 2;
var rinkLeft = game.rink.x - game.rink.width / 2;
var rinkRight = game.rink.x + game.rink.width / 2;
self.x = Math.max(rinkLeft + 40, Math.min(rinkRight - 40, self.x));
self.y = Math.max(game.rink.y - 50, Math.min(rinkBottom - 40, self.y));
// Check for puck collision
puck.hit(self);
};
return self;
});
var Puck = Container.expand(function () {
var self = Container.call(this);
var puckGraphics = self.attachAsset('puck', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.friction = 0.98;
self.bounceReduction = 0.7;
self.update = function () {
// Apply movement
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Maintain minimum velocity to prevent stopping
var currentSpeed = Math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY);
var minSpeed = 0.5;
if (currentSpeed > 0 && currentSpeed < minSpeed) {
var speedRatio = minSpeed / currentSpeed;
self.velocityX *= speedRatio;
self.velocityY *= speedRatio;
}
// Boundary collision
var rinkLeft = game.rink.x - game.rink.width / 2;
var rinkRight = game.rink.x + game.rink.width / 2;
var rinkTop = game.rink.y - game.rink.height / 2;
var rinkBottom = game.rink.y + game.rink.height / 2;
if (self.x < rinkLeft + 20 || self.x > rinkRight - 20) {
self.velocityX *= -self.bounceReduction;
self.x = Math.max(rinkLeft + 20, Math.min(rinkRight - 20, self.x));
}
if (self.y < rinkTop + 20 || self.y > rinkBottom - 20) {
self.velocityY *= -self.bounceReduction;
self.y = Math.max(rinkTop + 20, Math.min(rinkBottom - 20, self.y));
}
// Check for goals
if (self.y < rinkTop + 100 && self.x > game.rink.x - 150 && self.x < game.rink.x + 150) {
// Top goal (player scores)
playerScore++;
self.resetPosition();
LK.getSound('goal').play();
LK.setScore(playerScore);
updateScore();
if (playerScore >= winScore) {
LK.showYouWin();
}
} else if (self.y > rinkBottom - 100 && self.x > game.rink.x - 150 && self.x < game.rink.x + 150) {
// Bottom goal (AI scores)
aiScore++;
self.resetPosition();
LK.getSound('goal').play();
updateScore();
if (aiScore >= winScore) {
LK.showGameOver();
}
}
};
self.resetPosition = function () {
self.x = game.rink.x;
self.y = game.rink.y;
self.velocityX = 0;
self.velocityY = 0;
};
self.hit = function (stick) {
var dx = self.x - stick.x;
var dy = self.y - stick.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 60) {
var force = 15;
self.velocityX = dx / distance * force;
self.velocityY = dy / distance * force;
LK.getSound('hit').play();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x228B22
});
/****
* Game Code
****/
// Game variables
var playerScore = 0;
var aiScore = 0;
var winScore = 3;
var aiDifficulty = 'medium'; // 'easy', 'medium', 'hard'
var gameStarted = false;
// Create rink
game.rink = game.addChild(LK.getAsset('rink', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
}));
// Create center line
var centerLine = game.addChild(LK.getAsset('centerLine', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
}));
// Create goals
var topGoal = game.addChild(LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: game.rink.y - game.rink.height / 2 + 50
}));
var bottomGoal = game.addChild(LK.getAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: game.rink.y + game.rink.height / 2 - 50
}));
// Create goal posts
var topLeftPost = game.addChild(LK.getAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: topGoal.x - 150,
y: topGoal.y - 75
}));
var topRightPost = game.addChild(LK.getAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: topGoal.x + 150,
y: topGoal.y - 75
}));
var bottomLeftPost = game.addChild(LK.getAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: bottomGoal.x - 150,
y: bottomGoal.y + 75
}));
var bottomRightPost = game.addChild(LK.getAsset('goalPost', {
anchorX: 0.5,
anchorY: 0.5,
x: bottomGoal.x + 150,
y: bottomGoal.y + 75
}));
// Create game objects
var puck = game.addChild(new Puck());
puck.resetPosition();
var playerStick = game.addChild(new PlayerStick());
playerStick.x = game.rink.x;
playerStick.y = game.rink.y + game.rink.height / 4;
var aiStick = game.addChild(new AIStick());
aiStick.x = game.rink.x;
aiStick.y = game.rink.y - game.rink.height / 4;
// UI Elements
var scoreText = new Text2('Player: 0 - AI: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var difficultyText = new Text2('Difficulty: Medium', {
size: 60,
fill: 0xFFFFFF
});
difficultyText.anchor.set(0.5, 0);
difficultyText.y = 100;
LK.gui.top.addChild(difficultyText);
var instructionText = new Text2('Drag your stick to hit the puck!', {
size: 50,
fill: 0xFFFFFF
});
instructionText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(instructionText);
// Difficulty selector
var easyButton = new Text2('EASY', {
size: 60,
fill: 0x00FF00
});
easyButton.anchor.set(0, 0.5);
easyButton.x = 50;
LK.gui.left.addChild(easyButton);
var mediumButton = new Text2('MEDIUM', {
size: 60,
fill: 0xFFFF00
});
mediumButton.anchor.set(0, 0.5);
mediumButton.x = 50;
mediumButton.y = 100;
LK.gui.left.addChild(mediumButton);
var hardButton = new Text2('HARD', {
size: 60,
fill: 0xFF0000
});
hardButton.anchor.set(0, 0.5);
hardButton.x = 50;
hardButton.y = 200;
LK.gui.left.addChild(hardButton);
function updateScore() {
scoreText.setText('Player: ' + playerScore + ' - AI: ' + aiScore);
LK.setScore(playerScore);
}
function resetGame() {
playerScore = 0;
aiScore = 0;
updateScore();
puck.resetPosition();
playerStick.x = game.rink.x;
playerStick.y = game.rink.y + game.rink.height / 4;
aiStick.x = game.rink.x;
aiStick.y = game.rink.y - game.rink.height / 4;
}
// Event handlers
easyButton.down = function () {
aiDifficulty = 'easy';
difficultyText.setText('Difficulty: Easy');
resetGame();
};
mediumButton.down = function () {
aiDifficulty = 'medium';
difficultyText.setText('Difficulty: Medium');
resetGame();
};
hardButton.down = function () {
aiDifficulty = 'hard';
difficultyText.setText('Difficulty: Hard');
resetGame();
};
var isDragging = false;
game.down = function (x, y, obj) {
// Check if touch is in player area
if (y > game.rink.y - 50) {
isDragging = true;
playerStick.targetX = x;
playerStick.targetY = y;
}
};
game.move = function (x, y, obj) {
if (isDragging && y > game.rink.y - 50) {
playerStick.targetX = x;
playerStick.targetY = y;
}
};
game.up = function (x, y, obj) {
isDragging = false;
};
game.update = function () {
// Update all game objects
puck.update();
playerStick.update();
aiStick.update();
};
// Initialize game
updateScore();