0% found this document useful (0 votes)
13 views2 pages

Juego Básico de Evitación en Canvas

This document contains the HTML code for a basic game where a player controls a white square to avoid falling red obstacles. The game utilizes a canvas element for rendering and includes functionality for player movement and collision detection. If the player collides with an obstacle, an alert is triggered and the game reloads.

Uploaded by

Jairo Hernández
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Juego Básico de Evitación en Canvas

This document contains the HTML code for a basic game where a player controls a white square to avoid falling red obstacles. The game utilizes a canvas element for rendering and includes functionality for player movement and collision detection. If the player collides with an obstacle, an alert is triggered and the game reloads.

Uploaded by

Jairo Hernández
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

<!

DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Juego Básico</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
background: black;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = [Link]('gameCanvas');
const ctx = [Link]('2d');
[Link] = [Link];
[Link] = [Link];

const player = {
x: [Link] / 2 - 25,
y: [Link] - 60,
width: 50,
height: 50,
color: 'white',
speed: 10
};

const obstacle = {
x: [Link]() * ([Link] - 50),
y: 0,
width: 50,
height: 50,
color: 'red',
speed: 5
};

function drawPlayer() {
[Link] = [Link];
[Link](player.x, player.y, [Link], [Link]);
}

function drawObstacle() {
[Link] = [Link];
[Link](obstacle.x, obstacle.y, [Link], [Link]);
}

function movePlayer(event) {
if ([Link] === 'ArrowLeft' && player.x > 0) {
player.x -= [Link];
}
if ([Link] === 'ArrowRight' && player.x < [Link] -
[Link]) {
player.x += [Link];
}
}

function update() {
[Link](0, 0, [Link], [Link]);
drawPlayer();
drawObstacle();

obstacle.y += [Link];
if (obstacle.y > [Link]) {
obstacle.y = 0;
obstacle.x = [Link]() * ([Link] - 50);
}

if (player.x < obstacle.x + [Link] &&


player.x + [Link] > obstacle.x &&
player.y < obstacle.y + [Link] &&
player.y + [Link] > obstacle.y) {
alert('¡Game Over!');
[Link]();
}

requestAnimationFrame(update);
}

[Link]('keydown', movePlayer);
update();
</script>
</body>
</html>

Common questions

Powered by AI

To improve the gameplay experience for new players, strategies could include familiarizing themselves with the controls by practicing moving left and right using the arrow keys. They should also focus on observing the pattern and speed at which the obstacles fall, which could help them anticipate when and where to move. New players could start by positioning their character closer to the center to allow time for adjustments, and consciously practice predicting the trajectory of obstacles to effectively dodge them .

Event listeners in the game capture keypress events to handle player movement. The 'keydown' event listener is used to detect when the ArrowLeft or ArrowRight keys are pressed, triggering the 'movePlayer' function. This interaction allows players to move the player character left or right within the game canvas, facilitating user interaction and engagement with the game mechanics .

The purpose of using 'Math.random()' in the obstacle configuration is to randomly generate the x-coordinate of the obstacle's initial position each time it reappears at the top of the canvas. This ensures that the obstacle does not appear in the same location repeatedly, making the game more challenging and engaging .

Increasing the speed property of the obstacle would result in it moving faster across the screen. This would make the game more challenging, as the player would have less time to react and avoid collisions, potentially leading to a higher difficulty level. Faster obstacle speed requires quicker reflexes from the player to maneuver successfully, altering the game's pace and difficulty .

The game detects a collision by checking if the player's rectangle intersects with the obstacle's rectangle. This is done using conditions to see if the player’s boundaries overlap with those of the obstacle. If a collision is detected, indicated by a player's dimensions overlapping with the obstacle's dimensions, an alert with the message '¡Game Over!' is displayed and the game reloads, ending the current session .

The game achieves a reset after a collision by showing an alert with '¡Game Over!' and calling 'document.location.reload()'. This command reloads the entire webpage, resetting all game variables and starting a new session from the beginning, effectively restarting the game state and allowing the player to try again .

The 'requestAnimationFrame' function is used to update the game state continuously and render graphics smoothly. It tells the browser that you wish to perform an animation and requests that the browser schedule a repaint of the window for the next animation frame. This provides an efficient way to animate the game, ensuring animations run smoothly and in sync with the display’s refresh rate, which improves performance over traditional loop methods .

The player's movement is constrained by checking the player's x-coordinate before updating its position. The player object moves left when the 'ArrowLeft' key is pressed, provided that the movement does not take it outside the left boundary of the canvas. Similarly, it moves right when the 'ArrowRight' key is pressed, ensuring it does not exceed the right boundary of the canvas. This prevents the player from moving off the visible game area .

The game's visual elements are rendered and updated using the Canvas API's 2D rendering context. The 'drawPlayer()' and 'drawObstacle()' functions use the 'fillRect()' method to draw the player and obstacle rectangles on the canvas. The game state is continuously updated and rendered in the 'update()' function, which clears the canvas with 'clearRect()' before redrawing the game elements, ensuring each frame represents the current game state accurately. This loop is driven by the 'requestAnimationFrame()' method for smooth animations .

The game dynamically adjusts to different screen sizes by setting the canvas width and height to match the window's inner width and height properties. This is done using 'canvas.width = window.innerWidth;' and 'canvas.height = window.innerHeight;', ensuring that the game canvas scales to fit the viewport, making it responsive to resizing and different display sizes .

You might also like