/****
* Classes
****/
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
// Update the ball's position based on its direction
self.update = function () {
self.x += Math.cos(ballDirection) * 10 * (1 + 0.1 * Level);
self.y += Math.sin(ballDirection) * 10 * (1 + 0.1 * Level);
// Check for collision with the paddles
if (self.intersects(bluePaddle)) {
ballDirection = Math.random() * Math.PI + Math.PI / 2;
Points++;
Aushilf1++;
LK.getSound('ping').play();
if (Aushilf1 >= 50) {
Aushilf1 = 0;
Level++;
Points += 100;
Lives++;
}
} else if (self.intersects(yellowPaddle)) {
ballDirection = Math.random() * Math.PI + 3 * Math.PI / 2;
Points++;
Aushilf1++;
LK.getSound('ping').play();
if (Aushilf1 >= 50) {
Aushilf1 = 0;
Level++;
Points += 100;
Lives++;
}
} else if (self.intersects(greenPaddle)) {
ballDirection = Math.random() * Math.PI;
Points++;
Aushilf1++;
LK.getSound('ping').play();
if (Aushilf1 >= 50) {
Aushilf1 = 0;
Level++;
Points += 100;
Lives++;
}
} else if (self.intersects(redPaddle)) {
ballDirection = Math.random() * Math.PI + Math.PI;
Points++;
Aushilf1++;
LK.getSound('ping').play();
if (Aushilf1 >= 50) {
Aushilf1 = 0;
Level++;
Points += 100;
Lives++;
}
}
// Check if the ball has reached the edge of the screen
if (self.x <= 0 || self.x >= 2048 || self.y <= 0 || self.y >= 2732) {
Lives--;
LK.getSound('Explosion').play();
// Reset the ball's position and direction if there are still lives left
if (Lives > 0) {
self.x = 2048 / 2;
self.y = 2732 / 2;
ballDirection = Math.random() * 2 * Math.PI;
} else {
// End the game if there are no lives left
LK.showGameOver();
}
}
// Update the Level, Lives, and Score texts
levelText.setText('Level: ' + Level);
livesText.setText('Lives: ' + Lives);
scoreText.setText('Score: ' + Points);
};
});
var BluePaddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('bluePaddle', {
anchorX: 0.5,
anchorY: 0.5
});
});
var GreenPaddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('greenPaddle', {
anchorX: 0.5,
anchorY: 0.5
});
});
var RedPaddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('redPaddle', {
anchorX: 0.5,
anchorY: 0.5
});
});
var YellowPaddle = Container.expand(function () {
var self = Container.call(this);
var paddleGraphics = self.attachAsset('yellowPaddle', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
//<Assets used in the game will automatically appear here>
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('Bild1', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(background);
var ball = game.addChild(new Ball());
ball.x = 2048 / 2;
ball.y = 2732 / 2;
// Create a variable to store the ball's direction
// The direction is a random angle between 0 and 2*PI
var ballDirection = Math.random() * 2 * Math.PI;
var yellowPaddle = game.addChild(new YellowPaddle());
yellowPaddle.x = 2048 / 2;
yellowPaddle.y = 80;
var redPaddle = game.addChild(new RedPaddle());
redPaddle.x = 80;
redPaddle.y = 2732 / 2;
var bluePaddle = game.addChild(new BluePaddle());
bluePaddle.x = 2048 / 2;
bluePaddle.y = 2732 - 80;
var greenPaddle = game.addChild(new GreenPaddle());
greenPaddle.x = 2048 - 80;
greenPaddle.y = 2732 / 2;
// Initialize game variables
var Level = 1;
var Lives = 10;
var Points = 0;
var Aushilf1 = 0;
// Create and display the Level text
var levelText = new Text2('Level: ' + Level, {
size: 70,
fill: "#ffffff"
});
levelText.anchor.set(0.5, 0);
levelText.y = LK.gui.center.y - 1200;
LK.gui.center.addChild(levelText);
// Create and display the Lives text
var livesText = new Text2('Lives: ' + Lives, {
size: 70,
fill: "#ffffff"
});
livesText.anchor.set(0.5, 0);
livesText.y = levelText.height + levelText.y;
LK.gui.center.addChild(livesText);
// Create and display the Score text
var scoreText = new Text2('Score: ' + Points, {
size: 70,
fill: "#ffffff"
});
scoreText.anchor.set(0.5, 0);
scoreText.y = levelText.height + livesText.height + levelText.y;
LK.gui.center.addChild(scoreText);
// Create and display the game title text
var titleText = new Text2('Pyper-Hong by Nemesus', {
size: 70,
fill: "#ffffff"
});
titleText.anchor.set(0.5, 0);
titleText.y = scoreText.height + scoreText.y + 650;
LK.gui.center.addChild(titleText);
game.move = function (x, y, obj) {
var lastPosition = this.lastPosition || {
x: 2048 / 2,
y: 2732 / 2
};
var dx = x - lastPosition.x;
var dy = y - lastPosition.y;
bluePaddle.x = Math.min(Math.max(bluePaddle.x + dx, 80 + bluePaddle.width / 2), 2048 - 80 - bluePaddle.width / 2);
yellowPaddle.x = Math.min(Math.max(yellowPaddle.x - dx, 80 + yellowPaddle.width / 2), 2048 - 80 - yellowPaddle.width / 2);
greenPaddle.y = Math.min(Math.max(greenPaddle.y + dy, 80 + greenPaddle.height / 2), 2732 - 80 - greenPaddle.height / 2);
redPaddle.y = Math.min(Math.max(redPaddle.y - dy, 80 + redPaddle.height / 2), 2732 - 80 - redPaddle.height / 2);
this.lastPosition = {
x: x,
y: y
};
};