/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10; // Increase the speed of the ball
self.update = function () {
self.y += self.speed;
};
});
// Bat class
var Bat = Container.expand(function () {
var self = Container.call(this);
var batGraphics = self.attachAsset('bat', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 //Init game with green background
});
/****
* Game Code
****/
// Create and position the orange wall as the background
var orangeWall = LK.getAsset('orangeWall', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 20,
// Scale to cover the entire background
scaleY: 20,
// Scale to cover the entire background
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(orangeWall);
// Initialize variables
var balls = [];
var bat = game.addChild(new Bat());
bat.x = 2048 / 2;
bat.y = 2500;
var score = 0;
var misses = 0;
var pitchInterval = 1000;
var ballThrown = false; // New variable to control if a ball has been thrown
// Score text
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Misses text
var missesTxt = new Text2('Misses: 0', {
size: 100,
fill: 0xFFFFFF
});
missesTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(missesTxt);
// Function to pitch a new ball
function pitchBall() {
if (!ballThrown) {
// Only throw a ball if one hasn't been thrown yet
var newBall = new Ball();
newBall.x = Math.random() * 2048;
newBall.y = 0;
balls.push(newBall);
game.addChild(newBall);
ballThrown = true; // Update the ballThrown variable to true
}
}
// Game update function
game.update = function () {
for (var i = balls.length - 1; i >= 0; i--) {
var ball = balls[i];
if (ball.y > 2732) {
ball.destroy();
balls.splice(i, 1);
misses++;
missesTxt.setText('Misses: ' + misses);
ballThrown = false; // Update the ballThrown variable to false
if (misses >= 3) {
LK.showGameOver();
}
} else if (ball.intersects(bat)) {
// Play hit sound
LK.getSound('Hit').play();
// Add hitting effect
LK.effects.flashObject(bat, 0xffff00, 500); // Flash bat yellow for 500ms
// Launch ball to top of screen
ball.speed = -10; // Set speed to negative to move upwards
ball.update = function () {
this.y += this.speed;
if (this.y < 0) {
this.destroy();
balls.splice(i, 1);
ballThrown = false; // Update the ballThrown variable to false
}
};
score += 4;
scoreTxt.setText('Score: ' + score);
pitchInterval = Math.max(500, 1000 - score * 10);
// Increase ball speed every 20 points
if (score % 20 === 0) {
balls.forEach(function (ball) {
ball.speed += 2; // Increase speed by 2
});
}
}
}
};
// Pitch balls at intervals
var pitchTimer = LK.setInterval(pitchBall, pitchInterval);
// Handle bat movement
game.down = function (x, y, obj) {
bat.down(x, y, obj);
}; /****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10; // Increase the speed of the ball
self.update = function () {
self.y += self.speed;
};
});
// Bat class
var Bat = Container.expand(function () {
var self = Container.call(this);
var batGraphics = self.attachAsset('bat', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 //Init game with green background
});
/****
* Game Code
****/
// Create and position the orange wall as the background
var orangeWall = LK.getAsset('orangeWall', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 20,
// Scale to cover the entire background
scaleY: 20,
// Scale to cover the entire background
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(orangeWall);
// Initialize variables
var balls = [];
var bat = game.addChild(new Bat());
bat.x = 2048 / 2;
bat.y = 2500;
var score = 0;
var misses = 0;
var pitchInterval = 1000;
var ballThrown = false; // New variable to control if a ball has been thrown
// Score text
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Misses text
var missesTxt = new Text2('Misses: 0', {
size: 100,
fill: 0xFFFFFF
});
missesTxt.anchor.set(0.5, 0);
LK.gui.topRight.addChild(missesTxt);
// Function to pitch a new ball
function pitchBall() {
if (!ballThrown) {
// Only throw a ball if one hasn't been thrown yet
var newBall = new Ball();
newBall.x = Math.random() * 2048;
newBall.y = 0;
balls.push(newBall);
game.addChild(newBall);
ballThrown = true; // Update the ballThrown variable to true
}
}
// Game update function
game.update = function () {
for (var i = balls.length - 1; i >= 0; i--) {
var ball = balls[i];
if (ball.y > 2732) {
ball.destroy();
balls.splice(i, 1);
misses++;
missesTxt.setText('Misses: ' + misses);
ballThrown = false; // Update the ballThrown variable to false
if (misses >= 3) {
LK.showGameOver();
}
} else if (ball.intersects(bat)) {
// Play hit sound
LK.getSound('Hit').play();
// Add hitting effect
LK.effects.flashObject(bat, 0xffff00, 500); // Flash bat yellow for 500ms
// Launch ball to top of screen
ball.speed = -10; // Set speed to negative to move upwards
ball.update = function () {
this.y += this.speed;
if (this.y < 0) {
this.destroy();
balls.splice(i, 1);
ballThrown = false; // Update the ballThrown variable to false
}
};
score += 4;
scoreTxt.setText('Score: ' + score);
pitchInterval = Math.max(500, 1000 - score * 10);
// Increase ball speed every 20 points
if (score % 20 === 0) {
balls.forEach(function (ball) {
ball.speed += 2; // Increase speed by 2
});
}
}
}
};
// Pitch balls at intervals
var pitchTimer = LK.setInterval(pitchBall, pitchInterval);
// Handle bat movement
game.down = function (x, y, obj) {
bat.down(x, y, obj);
};