To create an animated effect where the road appears to move upwards, we can simulate the illusion of movement by adjusting the grid of the game over time. This can be achieved by continuously shifting the rows downwards and updating the ASCII representation periodically while the player moves.
Here's how to modify your code to implement this effect:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>THIS IS NOT A MAZAFAKIN GAME!!!</title>
<style>
* {
font-size: 20px !important;
line-height: 20px; /* Consistent line height to match font size */
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: monospace; /* Ensuring a consistent font */
white-space: pre; /* Preserve whitespace for ASCII output */
}
svg {
position: absolute;
pointer-events: none;
width: 200px; /* SVG width */
height: 200px; /* SVG height */
display: none; /* Initially hidden */
}
</style>
</head>
<body>
<pre id="asciiDisplay"></pre>
<svg id="overlayVisualization" width="200" height="200" viewBox="0 0 200 200">
<text id="playerIndicator" x="100" y="100" text-anchor="middle"
dominant-baseline="middle" fill="red" font-family="monospace" font-size="20" transform="translate(0, 1)">O</text>
<text id="obstacleIndicator" x="100" y="100" text-anchor="middle"
dominant-baseline="middle" fill="black" opacity="0.5" font-family="monospace" font-size="20" transform="translate(0, 1)">:</text>
</svg>
<script>
const asciiDisplayElement = document.getElementById('asciiDisplay');
const overlayVisualizationElement = document.getElementById('overlayVisualization');
const playerIndicatorElement = document.getElementById('playerIndicator');
const obstacleIndicatorElement = document.getElementById('obstacleIndicator');
const GRID_ROWS = 20; // Increased rows for animation fluidity
const GRID_COLUMNS = 17;
let currentPlayerHorizontalPosition = 8;
let currentPlayerVerticalPosition = GRID_ROWS - 2; // Start near the bottom
let stationaryObstacleHorizontalPosition = 8;
let stationaryObstacleVerticalPosition = GRID_ROWS - 3; // Obstacle one row above player
let isCollisionActive = false;
function executeGameUpdate() {
let gameGrid = Array(GRID_ROWS).fill('').map(() => Array(GRID_COLUMNS).fill(' '));
if (currentPlayerHorizontalPosition === stationaryObstacleHorizontalPosition && currentPlayerVerticalPosition === stationaryObstacleVerticalPosition) {
isCollisionActive = true;
gameGrid[currentPlayerVerticalPosition][currentPlayerHorizontalPosition] = ' ';
} else {
isCollisionActive = false;
gameGrid[stationaryObstacleVerticalPosition][stationaryObstacleHorizontalPosition] = ':';
}
gameGrid[currentPlayerVerticalPosition][currentPlayerHorizontalPosition] = isCollisionActive ? ' ' : 'O';
let asciiOutput = '';
for (let row of gameGrid) {
asciiOutput += '|' + row.join('') + '|\n';
}
asciiDisplayElement.innerHTML = asciiOutput;
if (isCollisionActive) {
overlayVisualizationElement.style.display = 'block';
playerIndicatorElement.setAttribute('x', currentPlayerHorizontalPosition * 11.76 + 7);
playerIndicatorElement.setAttribute('y', currentPlayerVerticalPosition * 10 + 10);
obstacleIndicatorElement.setAttribute('x', currentPlayerHorizontalPosition * 11.76 + 7);
obstacleIndicatorElement.setAttribute('y', currentPlayerVerticalPosition * 10 + 10);
} else {
overlayVisualizationElement.style.display = 'none';
}
}
function moveRoadUpwards() {
stationaryObstacleVerticalPosition--;
currentPlayerVerticalPosition--;
// Reset positions if they go beyond the top
if (stationaryObstacleVerticalPosition < 0) {
stationaryObstacleVerticalPosition = GRID_ROWS - 2;
stationaryObstacleHorizontalPosition = Math.floor(Math.random() * GRID_COLUMNS); // Randomize position
}
if (currentPlayerVerticalPosition < 0) {
currentPlayerVerticalPosition = GRID_ROWS - 2;
}
executeGameUpdate();
}
window.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
currentPlayerHorizontalPosition = Math.max(currentPlayerHorizontalPosition - 1, 0);
} else if (event.key === 'ArrowRight') {
currentPlayerHorizontalPosition = Math.min(currentPlayerHorizontalPosition + 1, GRID_COLUMNS - 1);
} else if (event.key === 'ArrowUp') {
currentPlayerVerticalPosition = Math.max(currentPlayerVerticalPosition - 1, 0);
} else if (event.key === 'ArrowDown') {
currentPlayerVerticalPosition = Math.min(currentPlayerVerticalPosition + 1, GRID_ROWS - 1);
}
executeGameUpdate();
});
// Start the upwards movement loop
setInterval(moveRoadUpwards, 500); // Adjust the interval for speed
executeGameUpdate();
</script>
</body>
</html>
Explanation of Changes:
-
Increased Grid Rows: The number of rows was increased to 20 to allow more space for the road to move upwards.
-
Moving Road Upwards: A new moveRoadUpwards function was added, which will shift the obstacle and player position upwards every 500 milliseconds, creating the effect of constant movement.
-
Random Obstacle Generation: When the obstacle moves off the screen, it resets to the bottom and randomly randomizes its horizontal position.
-
Adjusted Display Logic: The display update logic was modified to handle the changes without disrupting the overall flow of the game.
With these changes, you should see the road (represented by the obstacle) moving upwards, giving a simple dynamic effect to the ASCII game. Adjust the interval timing in the setInterval function to change the speed of the upward motion.