User prompt
spawn bullets bit closer to player. make player movement speed relate to distance from cursor
Code edit (1 edits merged)
Please save this source code
User prompt
make player unit movement more interesting
User prompt
fix bullet spawn position
User prompt
make player movement force-based
User prompt
make bullet speed fixed and not relate to playuer speed
User prompt
make player unit movement foce based. implement
User prompt
adjust collision logic
User prompt
refactor graceperiod and lifespan into single var
Code edit (2 edits merged)
Please save this source code
User prompt
if (gracePeriod <= 0 && bullets[i].lifespan <= 0 && bullets[i].intersects(player)) { can you check for intersection with center of player. add console logs
User prompt
still broken, fix
User prompt
fix bullet intersects code.
Code edit (1 edits merged)
Please save this source code
User prompt
Change collision detection to check whether bullet intersects center of player
User prompt
fix collision detection
Code edit (1 edits merged)
Please save this source code
User prompt
adjust code
User prompt
add debugging to determine why there are no collisions
User prompt
in collision detection, instead of player intersect with bullet, check whether bullet intersects center of player
User prompt
fix collisions
User prompt
implement
User prompt
review and fix
User prompt
fix blaxck screen
User prompt
add hitarea to player and bullet. make hitarea round
/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5, shape: 'ellipse', hitArea: new Rectangle(49.5, 49.5, 1, 1) }); self.speed = 0.25; // Half the player's movement speed self.lifespan = 60; // 1 second lifespan (60 frames) self.update = function () { self.x += self.speedX * self.speed; self.y += self.speedY * self.speed; self.lifespan--; if (self.lifespan > 0) { self.alpha = 0.5; // Make the bullet half transparent } else { self.alpha = 1; // Make the bullet fully opaque } if (self.y > 2732) { self.speedY = -self.speedY; } if (self.y < 0) { self.speedY = -self.speedY; } if (self.x > 2048) { self.speedX = -self.speedX; } if (self.x < 0) { self.speedX = -self.speedX; } }; }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5, shape: 'ellipse' }); self.update = function () { var dx = targetX - self.x; var dy = targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 1) { // Randomly change target position occasionally if (Math.random() < 0.01) { // Adjust probability as needed targetX = Math.random() * 2048; targetY = Math.random() * 2732; } // Only move if the distance is greater than a small threshold var speed = 5; // Adjust speed as needed self.x += dx / distance * speed; self.y += dy / distance * speed; playerDirection.x = dx / distance; playerDirection.y = dy / distance; playerMovedDistance += distance; } else { // Add random movement when the player is not moving towards a target var randomAngle = Math.random() * 2 * Math.PI; self.x += Math.cos(randomAngle) * 2; // Adjust random movement speed as needed self.y += Math.sin(randomAngle) * 2; } }; self.down = function (x, y, obj) { var game_position = game.toLocal(obj.global); targetX = game_position.x; targetY = game_position.y; }; self.move = function (x, y, obj) { var game_position = game.toLocal(obj.global); targetX = game_position.x; targetY = game_position.y; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 / 2; var bullets = []; var score = 0; var gracePeriod = 180; // 3 seconds at 60FPS var targetX = Math.random() * 2048; var targetY = Math.random() * 2732; var playerDirection = { x: 0, y: 0 }; var playerMovedDistance = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); game.update = function () { for (var i = bullets.length - 1; i >= 0; i--) { bullets[i].update(); if (gracePeriod <= 0 && bullets[i].lifespan <= 0) { var playerCenter = { x: player.x, y: player.y }; var bulletCenter = { x: bullets[i].x, y: bullets[i].y }; if (Math.abs(playerCenter.x - bulletCenter.x) < 50 && Math.abs(playerCenter.y - bulletCenter.y) < 50) { console.log("Bullet intersects with player center at:", playerCenter, "Bullet center at:", bulletCenter); LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } } if (gracePeriod > 0) { gracePeriod--; } if (playerMovedDistance >= 50 && LK.ticks % 35 == 0) { var newBullet = new Bullet(); newBullet.x = player.x - playerDirection.x * 150; // Spawn bullet behind the player newBullet.y = player.y - playerDirection.y * 150; newBullet.speedX = playerDirection.x * 5; // Bullet moves in the player's direction newBullet.speedY = playerDirection.y * 5; bullets.push(newBullet); game.addChild(newBullet); score++; scoreTxt.setText(score); } }; game.down = function (x, y, obj) { player.down(x, y, obj); }; game.move = function (x, y, obj) { player.move(x, y, obj); }; game.up = function (x, y, obj) { // No action needed on up event };
===================================================================
--- original.js
+++ change.js
@@ -48,42 +48,32 @@
var dx = targetX - self.x;
var dy = targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 1) {
- // Apply acceleration towards the target
- playerAcceleration.x = dx / distance;
- playerAcceleration.y = dy / distance;
+ // Randomly change target position occasionally
+ if (Math.random() < 0.01) {
+ // Adjust probability as needed
+ targetX = Math.random() * 2048;
+ targetY = Math.random() * 2732;
+ }
+ // Only move if the distance is greater than a small threshold
+ var speed = 5; // Adjust speed as needed
+ self.x += dx / distance * speed;
+ self.y += dy / distance * speed;
+ playerDirection.x = dx / distance;
+ playerDirection.y = dy / distance;
+ playerMovedDistance += distance;
} else {
- // No acceleration if close to the target
- playerAcceleration.x = 0;
- playerAcceleration.y = 0;
+ // Add random movement when the player is not moving towards a target
+ var randomAngle = Math.random() * 2 * Math.PI;
+ self.x += Math.cos(randomAngle) * 2; // Adjust random movement speed as needed
+ self.y += Math.sin(randomAngle) * 2;
}
- // Update velocity with acceleration
- playerVelocity.x += playerAcceleration.x;
- playerVelocity.y += playerAcceleration.y;
- // Apply friction to slow down the player over time
- playerVelocity.x *= friction;
- playerVelocity.y *= friction;
- // Limit the maximum speed
- if (Math.sqrt(playerVelocity.x * playerVelocity.x + playerVelocity.y * playerVelocity.y) > maxSpeed) {
- var angle = Math.atan2(playerVelocity.y, playerVelocity.x);
- playerVelocity.x = Math.cos(angle) * maxSpeed;
- playerVelocity.y = Math.sin(angle) * maxSpeed;
- }
- // Update player position with velocity
- self.x += playerVelocity.x;
- self.y += playerVelocity.y;
- // Update player direction and moved distance
- playerDirection.x = playerVelocity.x;
- playerDirection.y = playerVelocity.y;
- playerMovedDistance += Math.sqrt(playerVelocity.x * playerVelocity.x + playerVelocity.y * playerVelocity.y);
};
self.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
targetX = game_position.x;
targetY = game_position.y;
- playerVelocity.x = 0;
- playerVelocity.y = 0;
};
self.move = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
targetX = game_position.x;
@@ -106,25 +96,15 @@
player.y = 2732 / 2;
var bullets = [];
var score = 0;
var gracePeriod = 180; // 3 seconds at 60FPS
-var targetX = player.x;
-var targetY = player.y;
+var targetX = Math.random() * 2048;
+var targetY = Math.random() * 2732;
var playerDirection = {
x: 0,
y: 0
};
var playerMovedDistance = 0;
-var playerVelocity = {
- x: 0,
- y: 0
-};
-var playerAcceleration = {
- x: 0,
- y: 0
-};
-var maxSpeed = 10;
-var friction = 0.95;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
@@ -153,12 +133,12 @@
gracePeriod--;
}
if (playerMovedDistance >= 50 && LK.ticks % 35 == 0) {
var newBullet = new Bullet();
- newBullet.x = Math.random() * 2048; // Random x position within game boundaries
- newBullet.y = Math.random() * 2732; // Random y position within game boundaries
- newBullet.speedX = (Math.random() - 0.5) * 10; // Random speed in x direction
- newBullet.speedY = (Math.random() - 0.5) * 10; // Random speed in y direction
+ newBullet.x = player.x - playerDirection.x * 150; // Spawn bullet behind the player
+ newBullet.y = player.y - playerDirection.y * 150;
+ newBullet.speedX = playerDirection.x * 5; // Bullet moves in the player's direction
+ newBullet.speedY = playerDirection.y * 5;
bullets.push(newBullet);
game.addChild(newBullet);
score++;
scoreTxt.setText(score);