User prompt
Randomize the color of the background each game
User prompt
Make the ‘background' asset apply to the entire background
User prompt
Add a “background” asset that applies to the background
User prompt
Make the hoop 2x bigger
User prompt
Do not start the stopwatch until the player has moved
User prompt
Make the background have some detail, like a basketball court
User prompt
Allow the hoop to appear anywhere on the screen each game
User prompt
Do not let obstacles hit the player until the player moves
User prompt
Crear a half-circle around the spawn point adjacent to the bottom of the screen
User prompt
The ball cannot hit obstacles in the bottom 0.1 of the scren
User prompt
Obstacles do not appear within 200 pixels of the ball
User prompt
The spawn point of the ball does not have any obstacles near it
User prompt
If the hoop is on the top half of the screen, the “+1” is changed to “+3”
User prompt
If the hoop is on the bottom half of the screen, the goal is worth 2 points
User prompt
If the hoop is on the to half of the screen, the goal is worth 3 points
User prompt
If the hoop is more than halfway across the screen, the goal is worth 3 points
User prompt
If the player exceeds the screen boundary, end the game
User prompt
Allow the player to pause the game with a button in the top right corner
User prompt
Add a pause button in the top right corner
User prompt
Only end the game if an obstacle is hit
User prompt
If the game is ended, display the time survived
User prompt
Add a stopwatch in the top left corner
User prompt
Make the score numbers yellow
User prompt
Make the “+1” fade away rather than vanish
User prompt
Make the “+1” 2x larger
/****
* Classes
****/
// Assets will be automatically generated based on usage in the code.
// Ball class for the basketball
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.move = function () {
self.x += self.speedX;
self.y += self.speedY;
};
self.reset = function () {
self.x = game.width / 2;
self.y = game.height - 200;
self.speedX = 0;
self.speedY = 0;
hoop.setPosition(Math.random() * game.width, Math.random() * game.height);
};
});
// Hoop class for the basketball hoop
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
});
// Obstacle class for the game ending obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.radius = Math.random() * 50 + 50; // radius of damage, random between 50 and 100
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008000 // Change background color to represent a basketball court
});
/****
* Game Code
****/
var ball = game.addChild(new Ball());
var hoop = game.addChild(new Hoop());
var obstacles = [];
for (var i = 0; i < 10; i++) {
var obstacle = game.addChild(new Obstacle());
do {
obstacle.x = Math.random() * game.width;
obstacle.y = Math.random() * game.height;
} while (Math.abs(obstacle.x - ball.x) < 300 || Math.abs(obstacle.y - ball.y) < 300);
obstacles.push(obstacle);
}
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Set initial positions
ball.reset();
hoop.setPosition(game.width / 2, 100);
// Score variable
var score = 0;
// Touch event to launch the ball
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
var dx = pos.x - ball.x;
var dy = pos.y - ball.y;
var distance = Math.sqrt(dx * dx + dy * dy);
ball.speedX = dx / distance * 10;
ball.speedY = dy / distance * 10;
});
// Game tick event
LK.on('tick', function () {
ball.move();
// Check for collision with hoop
if (ball.intersects(hoop)) {
score += 1;
scoreTxt.setText(score.toString());
// Create a small flash of text that says "+1" every time there is a score
var scoreFlash = new Text2('+1', {
size: 100,
fill: "#ffffff"
});
scoreFlash.x = hoop.x;
scoreFlash.y = hoop.y;
game.addChild(scoreFlash);
LK.setTimeout(function () {
game.removeChild(scoreFlash);
}, 500);
ball.reset();
}
// Check for collision with obstacles
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var dx = ball.x - obstacle.x;
var dy = ball.y - obstacle.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < obstacle.radius) {
LK.showGameOver();
}
// Move the obstacle around the screen asynchronously
// Alternate between clockwise and counterclockwise movement for each obstacle
if (i % 2 == 0) {
obstacle.x += Math.cos((LK.ticks + i * 6) / 60) * 5;
obstacle.y += Math.sin((LK.ticks + i * 6) / 60) * 5;
} else {
obstacle.x -= Math.cos((LK.ticks + i * 6) / 60) * 5;
obstacle.y -= Math.sin((LK.ticks + i * 6) / 60) * 5;
}
// Restrict obstacle movement within screen limits
if (obstacle.x < 0) {
obstacle.x = 0;
if (ball.y < -50 || ball.y > game.height + 50 || ball.x < -50 || ball.x > game.width + 50) {
ball.reset();
}
// Wrap obstacle around screen
}
if (obstacle.x > game.width) {
obstacle.x = game.width;
}
if (obstacle.y < 0) {
obstacle.y = 0;
}
if (obstacle.y > game.height) {
obstacle.y = game.height;
}
}
});