/****
* Classes
****/
// Ball class representing the soccer ball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
// Add friction to slow down the ball
self.speedX *= 0.98;
self.speedY *= 0.98;
// Prevent the ball from going out of the field
if (self.x < 0) {
self.x = 0;
self.speedX *= -1;
}
if (self.x > 2048) {
self.x = 2048;
self.speedX *= -1;
}
if (self.y < 0) {
self.y = 0;
self.speedY *= -1;
}
if (self.y > 2732) {
self.y = 2732;
self.speedY *= -1;
}
};
});
// Fox class representing the goalkeeper
var Fox = Container.expand(function () {
var self = Container.call(this);
var foxGraphics = self.attachAsset('fox', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.direction = 1;
self.update = function () {
// The goalkeeper moves randomly within the goal boundaries
self.x += self.direction * self.speed;
if (self.x < goalpostLeft.x + foxGraphics.width / 2 + 50) {
self.x = goalpostLeft.x + foxGraphics.width / 2 + 50;
self.direction *= -1;
}
if (self.x > goalpostRight.x - foxGraphics.width / 2 - 50) {
self.x = goalpostRight.x - foxGraphics.width / 2 - 50;
self.direction *= -1;
}
};
});
var Goal = Container.expand(function () {
var self = Container.call(this);
var goalGraphics = self.attachAsset('goal', {
anchorX: 0.5,
anchorY: 0.5,
width: 800
});
});
var Goalpost = Container.expand(function () {
var self = Container.call(this);
var goalpostGraphics = self.attachAsset('goalpost', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Opponent class representing the opponent players
var Opponent = Container.expand(function () {
var self = Container.call(this);
var opponentGraphics = self.attachAsset('opponent', {
anchorX: 0.5,
anchorY: 0.5
});
self.direction = Math.random() < 0.5 ? -1 : 1; // Random initial direction
self.speed = Math.random() * 5 + 3; // Random speed between 3 and 8
self.distance = Math.random() * 500 + 500; // Random distance between 500 and 1000
self.originX = self.x; // Store the original x position
self.update = function () {
// The opponent players move horizontally across the field width
self.x += self.direction * self.speed;
if (self.x < opponentGraphics.width / 2) {
self.x = opponentGraphics.width / 2;
self.direction *= -1;
}
if (self.x > 2048 - opponentGraphics.width / 2) {
self.x = 2048 - opponentGraphics.width / 2;
self.direction *= -1;
}
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Rabbit class representing the player character
var Rabbit = Container.expand(function () {
var self = Container.call(this);
var rabbitGraphics = self.attachAsset('rabbit', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
if (self.kicking) {
self.x += self.speed;
if (self.x > 2048) {
self.x = 2048;
self.kicking = false;
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 // Init game with green background
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0,
anchorY: 0
});
background.x = 0;
background.y = 0;
// Initialize game elements
var rabbit = game.addChild(new Rabbit());
rabbit.x = 1024;
rabbit.y = 2400;
var ball = game.addChild(new Ball());
ball.x = rabbit.x;
ball.y = rabbit.y - ball.height;
var fox = game.addChild(new Fox());
fox.x = 1024;
fox.y = 200;
var goal = game.addChild(new Goal());
goal.x = 1024;
goal.y = 0;
var goalpostLeft = game.addChild(new Goalpost());
goalpostLeft.x = goal.x - goal.width / 2;
goalpostLeft.y = 0;
var goalpostRight = game.addChild(new Goalpost());
goalpostRight.x = goal.x + goal.width / 2;
goalpostRight.y = 0;
// Initialize opponent players
var opponents = [];
var opponentSpacing = (rabbit.y - fox.y) / 6; // Calculate the spacing between the opponents
for (var i = 0; i < 5; i++) {
var opponent = game.addChild(new Opponent());
opponent.x = 1024; // Center the opponents in the x-axis
opponent.y = fox.y + opponentSpacing * (i + 1); // Distribute the opponents evenly between the rabbit and the goal
opponents.push(opponent);
}
// Initialize move counter
var moveCount = 0;
// Initialize shot counter
var shotCount = 10;
// Create a text object to display the number of shots left and the score
var scoreText = new Text2('Shots: ' + shotCount + ' Score: ' + moveCount, {
size: 50,
fill: 0xFFFFFF,
fontWeight: 'bold',
backgroundColor: 0x000000
});
scoreText.anchor.set(0.5, 1);
LK.gui.bottom.addChild(scoreText);
// Handle game updates
game.update = function () {
rabbit.update();
ball.update();
fox.update();
// Check for goal
if (ball.y < 50 && ball.x > goalpostLeft.x && ball.x < goalpostRight.x) {
if (!ball.goalScored) {
ball.goalScored = true;
LK.getSound('goal').play();
moveCount += 1;
shotCount += 2;
scoreText.setText('Shots: ' + shotCount + ' Score: ' + moveCount);
// Reset the ball position after a goal is scored
ball.x = rabbit.x;
ball.y = rabbit.y + rabbit.height / 2 + ball.height / 2;
ball.speedX = 0;
ball.speedY = 0;
}
} else {
ball.goalScored = false;
}
// Check if the player has run out of shots
if (shotCount <= 0) {
LK.showGameOver("Out of shots! Final score: " + moveCount);
}
// Update opponents
for (var i = 0; i < opponents.length; i++) {
var opponent = opponents[i];
opponent.update();
// Check for collision with ball
if (ball.intersects(opponent)) {
// Calculate the direction from the opponent to the ball
var dx = ball.x - opponent.x;
var dy = ball.y - opponent.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Reflect the ball's speed in the direction of the collision
ball.speedX = dx / distance * 20;
ball.speedY = dy / distance * 20;
// Play kick sound effect
LK.getSound('kick').play();
}
}
// Check for collision with fox
if (ball.intersects(fox)) {
// Calculate the direction from the fox to the ball
var dx = ball.x - fox.x;
var dy = ball.y - fox.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Reflect the ball's speed in the direction of the collision
ball.speedX = dx / distance * 20;
ball.speedY = dy / distance * 20;
// Play kick sound effect
LK.getSound('kick').play();
}
};
// Handle player input
game.down = function (x, y, obj) {
// Move rabbit towards mouse
rabbit.x = x;
rabbit.y = y;
};
game.up = function (x, y, obj) {
// Kick the ball towards the goal
var dx = x - rabbit.x;
var dy = y - rabbit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
ball.speedX = dx / distance * 20;
ball.speedY = dy / distance * 20;
// Make the rabbit move in the opposite direction
rabbit.kicking = true;
// Play kick sound effect
LK.getSound('kick').play();
// Decrement shot counter
shotCount--;
// Update the score text
scoreText.setText('Shots: ' + shotCount + ' Score: ' + moveCount);
// Make the rabbit move with the ball
rabbit.x = ball.x;
rabbit.y = ball.y;
};
pikachu. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
squirtle. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
glumanda. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
pokemon ball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.