User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'addEventListener')' in or related to this line: 'document.addEventListener('keydown', function (event) {' Line Number: 114
User prompt
Make the character's movement keys w (move forward), a (move left), s (move backward), and d (move right). Make the obstacles progressively faster and larger. For each obstacle that goes off the map, add 1 point to the score. The score is displayed at the top of the screen, and the score is also displayed when you lose the game.
Code edit (1 edits merged)
Please save this source code
User prompt
Wasteland Rider
Initial prompt
Crea una carretera donde hay un motorista. El escenario es de una ciudad postapocalíptica, destruida. El motorista con la moto puede avanzar hacia delante, hacia atrás y hacia los lados. Por la carretera habrá obstáculos dignos de una ciudad postapocalíptica por ejemplo escombros. Estos obstaculos iran apareciendo por la carretera a medida que el motorista va avanzando por la carretera.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Motorcycle = Container.expand(function () {
var self = Container.call(this);
var motorcycleGraphics = self.attachAsset('motorcycle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
self.moveLeft = false;
self.moveRight = false;
self.moveBackward = false;
self.moveForward = true;
self.laneWidth = 512;
self.currentLane = 1;
self.minX = 256;
self.maxX = 1792;
self.velocity = {
x: 0,
y: 0
};
self.update = function () {
var targetX = self.x;
if (self.moveLeft && self.currentLane > 0) {
targetX -= self.laneWidth;
self.currentLane--;
}
if (self.moveRight && self.currentLane < 3) {
targetX += self.laneWidth;
self.currentLane++;
}
targetX = Math.max(self.minX, Math.min(self.maxX, targetX));
self.velocity.x = (targetX - self.x) * 0.15;
self.x += self.velocity.x;
self.moveLeft = false;
self.moveRight = false;
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 12;
self.lastY = self.y;
self.update = function () {
self.lastY = self.y;
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
var roadGraphic = LK.getAsset('road', {
anchorX: 0.5,
anchorY: 0.0
});
roadGraphic.x = 1024;
roadGraphic.y = 0;
game.addChild(roadGraphic);
var motorcycle = game.addChild(new Motorcycle());
motorcycle.x = 1024;
motorcycle.y = 2400;
var obstacles = [];
var score = 0;
var distance = 0;
var gameActive = true;
var spawnRate = 60;
var spawnCounter = 0;
var difficulty = 1;
var scoreTxt = new Text2('Distance: 0m', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 1024;
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
var lanePositions = [256, 768, 1280, 1792];
game.move = function (x, y, obj) {
if (!gameActive) return;
var relativeX = x;
var threshold = 512;
if (relativeX < 512) {
motorcycle.moveLeft = true;
} else if (relativeX > 1536) {
motorcycle.moveRight = true;
}
};
game.update = function () {
if (!gameActive) return;
motorcycle.update();
spawnCounter++;
if (spawnCounter >= Math.max(30, spawnRate - Math.floor(difficulty))) {
spawnCounter = 0;
var randomLane = Math.floor(Math.random() * 4);
var newObstacle = new Obstacle();
newObstacle.x = lanePositions[randomLane];
newObstacle.y = -100;
newObstacle.lastY = newObstacle.y;
obstacles.push(newObstacle);
game.addChild(newObstacle);
LK.getSound('whoosh').play();
}
distance++;
score = Math.floor(distance / 10);
difficulty = 1 + Math.floor(distance / 500);
scoreTxt.setText('Distance: ' + score + 'm');
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
if (obstacle.lastY <= 2732 && obstacle.y > 2732) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
var isColliding = motorcycle.intersects(obstacle);
if (isColliding) {
gameActive = false;
LK.getSound('crash').play();
LK.effects.flashScreen(0xFF0000, 500);
LK.setScore(score);
LK.showGameOver();
return;
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,145 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var Motorcycle = Container.expand(function () {
+ var self = Container.call(this);
+ var motorcycleGraphics = self.attachAsset('motorcycle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 15;
+ self.moveLeft = false;
+ self.moveRight = false;
+ self.moveBackward = false;
+ self.moveForward = true;
+ self.laneWidth = 512;
+ self.currentLane = 1;
+ self.minX = 256;
+ self.maxX = 1792;
+ self.velocity = {
+ x: 0,
+ y: 0
+ };
+ self.update = function () {
+ var targetX = self.x;
+ if (self.moveLeft && self.currentLane > 0) {
+ targetX -= self.laneWidth;
+ self.currentLane--;
+ }
+ if (self.moveRight && self.currentLane < 3) {
+ targetX += self.laneWidth;
+ self.currentLane++;
+ }
+ targetX = Math.max(self.minX, Math.min(self.maxX, targetX));
+ self.velocity.x = (targetX - self.x) * 0.15;
+ self.x += self.velocity.x;
+ self.moveLeft = false;
+ self.moveRight = false;
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var obstacleGraphics = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 12;
+ self.lastY = self.y;
+ self.update = function () {
+ self.lastY = self.y;
+ self.y += self.speed;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x1a1a1a
+});
+
+/****
+* Game Code
+****/
+var roadGraphic = LK.getAsset('road', {
+ anchorX: 0.5,
+ anchorY: 0.0
+});
+roadGraphic.x = 1024;
+roadGraphic.y = 0;
+game.addChild(roadGraphic);
+var motorcycle = game.addChild(new Motorcycle());
+motorcycle.x = 1024;
+motorcycle.y = 2400;
+var obstacles = [];
+var score = 0;
+var distance = 0;
+var gameActive = true;
+var spawnRate = 60;
+var spawnCounter = 0;
+var difficulty = 1;
+var scoreTxt = new Text2('Distance: 0m', {
+ size: 100,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+scoreTxt.x = 1024;
+scoreTxt.y = 50;
+LK.gui.top.addChild(scoreTxt);
+var lanePositions = [256, 768, 1280, 1792];
+game.move = function (x, y, obj) {
+ if (!gameActive) return;
+ var relativeX = x;
+ var threshold = 512;
+ if (relativeX < 512) {
+ motorcycle.moveLeft = true;
+ } else if (relativeX > 1536) {
+ motorcycle.moveRight = true;
+ }
+};
+game.update = function () {
+ if (!gameActive) return;
+ motorcycle.update();
+ spawnCounter++;
+ if (spawnCounter >= Math.max(30, spawnRate - Math.floor(difficulty))) {
+ spawnCounter = 0;
+ var randomLane = Math.floor(Math.random() * 4);
+ var newObstacle = new Obstacle();
+ newObstacle.x = lanePositions[randomLane];
+ newObstacle.y = -100;
+ newObstacle.lastY = newObstacle.y;
+ obstacles.push(newObstacle);
+ game.addChild(newObstacle);
+ LK.getSound('whoosh').play();
+ }
+ distance++;
+ score = Math.floor(distance / 10);
+ difficulty = 1 + Math.floor(distance / 500);
+ scoreTxt.setText('Distance: ' + score + 'm');
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ var obstacle = obstacles[i];
+ obstacle.update();
+ if (obstacle.lastY <= 2732 && obstacle.y > 2732) {
+ obstacle.destroy();
+ obstacles.splice(i, 1);
+ continue;
+ }
+ var isColliding = motorcycle.intersects(obstacle);
+ if (isColliding) {
+ gameActive = false;
+ LK.getSound('crash').play();
+ LK.effects.flashScreen(0xFF0000, 500);
+ LK.setScore(score);
+ LK.showGameOver();
+ return;
+ }
+ }
+};
\ No newline at end of file