User prompt
add a small flash of text that says “+1” every time there is a score
User prompt
Change the background to a basketball court
User prompt
Make each obstacle have its own radius of damage
User prompt
Make obstacles move in randomly sized circular patterns
User prompt
Allow obstacles to move both clockwise and counterclockwise
User prompt
Obstacles must initially appear at least 3 squares away from the ball
User prompt
Randomize obstacle positions
User prompt
Spread out the obstacles more
User prompt
Make the obstacles only move around inside of the screen limit
User prompt
Make each obstacle move asynchronously
User prompt
Make 9 more obstacles
User prompt
Make the obstacles move around the screen
User prompt
Add obstacles that end the game when hit
User prompt
Randomize the position of the goal each game
Initial prompt
Basketball Bangers
/**** * 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: 50, 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; } } });
===================================================================
--- original.js
+++ change.js
@@ -91,8 +91,19 @@
// 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: 50,
+ 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++) {