Code edit (5 edits merged)
Please save this source code
User prompt
in drawNextSegment update the points & delay calculation to take into account slideAmount when moveSegments is true
User prompt
in drawSegment update the width calculation to take into account slideAmount when moveSegments is true
User prompt
graphpoints x shoud never exeed game center
User prompt
graphPoints x should never exeed game center
User prompt
like for the indicator, segments (x+width) should never exeed the game center
User prompt
move var slideAmount = 4.0; to global scope
Code edit (1 edits merged)
Please save this source code
User prompt
updateSlide call is missinf
User prompt
segments are not sliding when moveSegments is true
Code edit (1 edits merged)
Please save this source code
User prompt
the slide() function of segments should be called in a tick loop
User prompt
remove moveSegmentsLeft() call
User prompt
in slide() function, only update position when moveSegments is true
User prompt
use moveSegments to control the slide of the GraphSegments
User prompt
when (segment.x + segment.width / 2 >= game.width / 2) also set moveSegments to true
User prompt
add a global boolean moveSegments defalting to false
User prompt
in moveSegmentsLeft, use the slide(à function instead of segment.x -= segment.width;
User prompt
add a slide() function in GraphSegment class that move the segment to the left
User prompt
in drawSegment use the GraphSegment class instead of a direct asset
User prompt
in the GraphSegment class add an asset like LK.getAsset('segment', 'Graph Segment', 0, 0.5)
User prompt
in moveSegmentsLeft use the global array of segments to move them
User prompt
store a global array of segments. add them in drawSegment()
User prompt
Fix Bug: 'TypeError: segment.slide is not a function' in this line: 'segment.slide();' Line Number: 189
User prompt
instead of moving the segments "externally" in moveSegmentsLeft, use a global boolean "moveSegments" defaulting to false. when `(segment.x + segment.width / 2 >= game.width / 2)` instead of calling moveSegmentsLeft, set moveSegments to true. Also add a slide() function in segments to make them move left when moveSegments is true
/****
* Classes
****/
var GraphSegment = Container.expand(function () {
var self = Container.call(this);
self.slide = function () {
if (moveSegments) {
self.x -= 20;
}
};
});
// ValueIndicator class to represent the current value of the stock on the graph
var ValueIndicator = Container.expand(function () {
var self = Container.call(this);
var indicatorGraphics = self.createAsset('valueIndicator', 'Current Stock Value Indicator', 0.5, 0.5);
self.updatePosition = function (price) {
if (!running) {
return;
}
// Position ValueIndicator based on the stock price
self.x = 0; // Start from the left side
self.y = 2732 / 2; // Start from the left side
var targetX = game.width / 2; // Center horizontally
var targetY = game.height / 2; // Center vertically
// Custom tween function to animate the ValueIndicator and update SpecialGraphSegment
var startTime = Date.now();
var duration = 5500; // Duration in milliseconds
var startX = self.x;
var endX = targetX;
var startY = self.y;
var endY = targetY;
var animateX = function animateX() {
var endTime = startTime + duration;
var currentTime = Date.now();
var timeRemaining = endTime - currentTime;
var nextX = startX + (endX - startX) * (1 - timeRemaining / duration);
var nextY = startY + (endY - startY) * (1 - timeRemaining / duration);
self.x = nextX;
if (currentTime < endTime) {
LK.setTimeout(animateX, 16);
}
};
animateX();
// Function to animate the ValueIndicator based on stockValuesHistory
var historyIndex = 1; // Start with the first value in the history
var updateYPosition = function updateYPosition() {
if (historyIndex < stockValuesHistory.length) {
var targetY = game.height / 2 - stockValuesHistory[historyIndex] * 10; // Scale the stock value for display
var nextX = self.x;
console.log('ValueIndicator moving to next position: ' + stockValuesHistory[historyIndex], nextX, 1366 - targetY);
var startY = self.y;
var duration = 1000; // Duration in milliseconds for Y movement
var startTime = Date.now();
var animateY = function animateY() {
var currentTime = Date.now();
var timeElapsed = currentTime - startTime;
var newY = startY + (targetY - startY) * (timeElapsed / duration);
self.y = newY;
if (timeElapsed < duration) {
LK.setTimeout(animateY, 16);
} else {
self.y = targetY; // Ensure final position is set
}
};
animateY();
historyIndex++;
LK.setTimeout(function () {
updateYPosition();
}, 1000); // Update position every second and add a new segment
}
};
updateYPosition();
};
});
// Player class to represent the player's portfolio
var Player = Container.expand(function () {
var self = Container.call(this);
self.balance = 100; // Start with $100
self.stocks = {}; // Object to hold stocks and quantities
self.buyStock = function (stock, quantity) {
var cost = stock.getPrice() * quantity;
if (self.balance >= cost) {
self.balance -= cost;
if (!self.stocks[stock]) {
self.stocks[stock] = 0;
}
self.stocks[stock] += quantity;
}
};
self.sellStock = function (stock, quantity) {
if (self.stocks[stock] && self.stocks[stock] >= quantity) {
self.balance += stock.getPrice() * quantity;
self.stocks[stock] -= quantity;
if (self.stocks[stock] === 0) {
delete self.stocks[stock];
}
}
};
self.getBalance = function () {
return self.balance;
};
self.getPortfolio = function () {
return self.stocks;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
// No need to set backgroundColor since we are adding a background asset
});
/****
* Game Code
****/
var moveSegments = false;
var segmentsArray = [];
// Create and add background asset
var running = false;
var background = game.addChild(LK.getAsset('background', 'Game background', 0.5, 0.5));
background.x = 1024; // Center x position
background.y = 2732 - background.height / 2; // Center y position
// Initialize game elements
var player = new Player();
var stocks = [];
// Initialize stock values history with the first value of $10
var stockValuesHistory = [10];
// Add 10 other random stock values
for (var i = 0; i < 10; i++) {
stockValuesHistory.push(Math.floor(Math.random() * 100) + 1);
}
// Instantiate ValueIndicator
var valueIndicator = new ValueIndicator();
game.addChild(valueIndicator);
// Compute the coordinates for ValueIndicator based on stockValuesHistory
var graphPoints = [];
var graphWidth = game.width;
var segmentWidth = graphWidth / stockValuesHistory.length;
// Set the first graphPoints to match the start coordinates of valueIndicator
var x = 0;
var y = 1366;
console.log('valueIndicator start at', {
x: Math.round(x),
y: Math.round(1366 - y)
});
graphPoints.push({
x: x,
y: y
});
// Compute the remaining coordinates
for (var i = 1; i < stockValuesHistory.length; i++) {
x = segmentWidth * i;
y = game.height / 2 - stockValuesHistory[i] * 10; // Scale the stock value for display
graphPoints.push({
x: x,
y: y
});
}
// Draw a sample square using drawGraph
var stockDisplay = []; // Array to hold stock display elements
var balanceText = new Text2('Balance: $' + player.getBalance(), {
size: 50,
fill: "#ffffff"
});
balanceText.anchor.set(0.5, 0);
LK.gui.top.addChild(balanceText);
// Update the player's balance display
function updateBalanceDisplay() {
balanceText.setText('Balance: $' + player.getBalance().toFixed(2));
}
// Function to handle buying stocks
function buyStock(stock) {
player.buyStock(stock, 1); // Buy 1 stock for simplicity
updateBalanceDisplay();
}
// Function to handle selling stocks
function sellStock(stock) {
player.sellStock(stock, 1); // Sell 1 stock for simplicity
updateBalanceDisplay();
}
// Main game loop
LK.on('tick', function () {
if (!running) {
return;
}
if (moveSegments) {
segmentsArray.forEach(function (segment) {
segment.slide();
});
}
// Check for game over conditions (e.g., player runs out of money)
if (player.getBalance() <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
function drawSegment(gameRef, x1, y1, x2, y2, delay, callback) {
var segment = gameRef.addChild(LK.getAsset('segment', 'Graph Segment', 0, 0.5));
segmentsArray.push(segment);
segment.x = x1;
segment.y = y1;
segment.width = 1;
segment.height = 20;
var deltaX = x2 - x1;
var deltaY = y2 - y1;
var distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
segment.rotation = Math.atan2(deltaY, deltaX);
var startTime = Date.now();
var endTime = startTime + delay * 1000;
var updateWidth = function updateWidth() {
if (!running) {
return;
}
var currentTime = Date.now();
var timeElapsed = currentTime - startTime;
var newWidth = Math.min(timeElapsed / (delay * 1000) * distance, distance);
segment.width = newWidth;
if (currentTime < endTime) {
LK.setTimeout(updateWidth, 16);
} else {
console.log("Past center ? " + (segment.x + segment.width / 2 >= game.width / 2));
if (segment.x + segment.width / 2 >= game.width / 2) {
moveSegments = true;
}
if (typeof callback === 'function') {
callback();
}
}
};
updateWidth();
}
function moveSegmentsLeft() {
var segments = segmentsArray;
console.log("moveSegmentsLeft..." + segments.length);
segments.forEach(function (segment) {
segment.x -= 20;
});
}
function drawGraph(gameRef, points) {
console.log('Starting to draw graph');
if (points.length < 2) {
console.log('Not enough points to draw a graph segment.');
return;
} // Need at least two points to draw a segment
function drawNextSegment(index) {
if (index < points.length - 1) {
var pointA = points[index];
var pointB = points[index + 1];
console.log('Drawing segment from', {
x: Math.round(pointA.x),
y: Math.round(1366 - pointA.y)
}, 'to', {
x: Math.round(pointB.x),
y: Math.round(1366 - pointB.y)
});
drawSegment(gameRef, pointA.x, pointA.y, pointB.x, pointB.y, 1, function () {
/*console.log('Segment drawn from', {
x: Math.round(pointA.x),
y: Math.round(pointA.y)
}, 'to', {
x: Math.round(pointB.x),
y: Math.round(pointB.y)
});*/
drawNextSegment(index + 1);
});
} else {
console.log('Finished drawing graph');
}
}
drawNextSegment(0);
}
function startGame() {
// Initialize and start the game logic here
running = true;
// Pass the series of points to drawGraph
drawGraph(game, graphPoints);
// Add ValueIndicator to the game and update its position
valueIndicator.updatePosition(100); // Example stock price
}
LK.setTimeout(function () {
startGame();
}, 1000);
A Technical dark background. Nothing just a gradiant of colors from black to dark blue. Theme : stock market. background
A modern clean empty rectangular button without borders. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
without shadow
a basic empty ui popup with a black background. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.