NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

<!-- HTML structure for the game -->
<body>
<!-- Canvas element to display the game -->
<canvas id="gameCanvas" width="800" height="400"></canvas>
<!-- Element to show the player's score -->
<div id="score">Score: 0</div>

<!-- JavaScript code for the game logic -->
<script>
// Dino character's properties and initial values
const dino = {
x: 50, // Dino's starting horizontal position
y: canvas.height - 50, // Dino's starting vertical position
width: 50, // Dino's width
height: 50, // Dino's height
color: "#00F", // Dino's color (blue)
speed: 5, // Dino's movement speed
isJumping: false, // Flag to check if the dino is jumping
jumpHeight: 100, // Maximum jump height
jumpCount: 0, // Jump counter
};

// Array to store obstacle information
const obstacles = [];

// Initial score value
let score = 0;

// Minimum gap between obstacles
const minimumObstacleGap = 75;

// Counter to increase the gap between obstacles
let increaseGap = 0;

// Function to continuously update and render the game
function gameLoop() {
update(); // Update game logic
draw(); // Render game elements
increaseGap++; // Increment the obstacle gap counter
requestAnimationFrame(gameLoop); // Request the next animation frame
}

// Function to update the game logic
function update() {
moveDino(); // Update dino's position
moveObstacles(); // Update obstacle positions
checkCollisions(); // Check for collisions
}

// Function to move the dino
function moveDino() {
// Check if the dino is jumping
if (dino.isJumping) {
// Move the dino upwards during the jump
if (dino.jumpCount < dino.jumpHeight) {
dino.y -= 5;
dino.jumpCount += 5;
} else {
dino.isJumping = false; // End the jump when the max height is reached
}
} else if (dino.y < canvas.height - dino.height) {
dino.y += 5; // Move the dino downwards after the jump
}
updateScore(); // Update the score after a successful jump
}

// Function to move and create obstacles
function moveObstacles() {
// Check if it's time to create a new obstacle
if (increaseGap > minimumObstacleGap && Math.random() > 0.005) {
increaseGap = 0; // Reset the gap counter
// Create a new obstacle and add it to the array
const obstacle = {
x: canvas.width,
y: canvas.height - 50,
width: 50,
height: 50,
color: "#F00", // Obstacle color (red)
speed: 5,
};
obstacles.push(obstacle);
}

// Move existing obstacles and remove those that are out of the screen
for (let i = 0; i < obstacles.length; i++) {
obstacles[i].x -= obstacles[i].speed;

// Check if the obstacle is out of the screen
if (obstacles[i].x + obstacles[i].width < 0) {
obstacles.splice(i, 1); // Remove the obstacle from the array
i--; // Adjust the loop counter
score++; // Increase the player's score
}
}
}

// Function to check for collisions between the dino and obstacles
function checkCollisions() {
// Array to store the dino's collision points
const dinoPoints = [
{ x: dino.x, y: dino.y + dino.height }, // Bottom left corner
{ x: dino.x + dino.width / 2, y: dino.y }, // Top center
{ x: dino.x + dino.width, y: dino.y + dino.height }, // Bottom right corner
];

// Loop through obstacles to check for collisions
for (let i = 0; i < obstacles.length; i++) {
// Array to store the current obstacle's collision points
const obstaclePoints = [
{ x: obstacles[i].x, y: obstacles[i].y + obstacles[i].height }, // Bottom left corner
{ x: obstacles[i].x + obstacles[i].width / 2, y: obstacles[i].y }, // Top center
{ x: obstacles[i].x + obstacles[i].width, y: obstacles[i].y + obstacles[i].height }, // Bottom right corner
];

// Check collision using point-in-triangle algorithm
if (
pointInTriangle(dinoPoints[0].x, dinoPoints[0].y, ...obstaclePoints) ||
pointInTriangle(dinoPoints[1].x, dinoPoints[1].y, ...obstaclePoints) ||
pointInTriangle(dinoPoints[2].x, dinoPoints[2].y, ...obstaclePoints)
) {
alert("Game Over! Your Score: " + score);
resetGame(); // Reset the game after a collision
return;
}
}
}

// Function to check if a point is inside a triangle
function pointInTriangle(px, py, p1, p2, p3) {
// Helper function to determine the sign of a point relative to a line
function sign(p1, p2, p3) {
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}

// Check signs for each corner of the triangle
const b1 = sign({ x: px, y: py }, p1, p2) < 0;
const b2 = sign({ x: px, y: py }, p2, p3) < 0;
const b3 = sign({ x: px, y: py }, p3, p1) < 0;

// Return true if the point is inside the triangle
return b1 === b2 && b2 === b3;
}

// Function to reset the game to its initial state
function resetGame() {
dino.x = 50;
dino.y = canvas.height - 50;
dino.isJumping = false;
dino.jumpCount = 0;

obstacles.length = 0; // Clear the obstacles array
score = 0; // Reset the player's score
}

// Function to render game elements on the canvas
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
drawDino(); // Render the dino
drawObstacles(); // Render the obstacles
}

// Function to render the dino on the canvas
function drawDino() {
ctx.fillStyle = dino.color; // Set dino color
ctx.fillRect(dino.x, dino.y, dino.width, dino.height); // Draw dino rectangle
}

// Function to render obstacles on the canvas
function drawObstacles() {
for (let i = 0; i < obstacles.length; i++) {
ctx.fillStyle = obstacles[i].color; // Set obstacle color

// Draw a triangle obstacle
ctx.beginPath();
ctx.moveTo(obstacles[i].x, obstacles[i].y + obstacles[i].height); // Bottom left corner
ctx.lineTo(obstacles[i].x + obstacles[i].width / 2, obstacles[i].y); // Top center
ctx.lineTo(obstacles[i].x + obstacles[i].width, obstacles[i].y + obstacles[i].height); // Bottom right corner
ctx.closePath();
ctx.fill();
}
}

// Event listener for the up arrow key to make the dino jump
window.addEventListener("keydown", (e) => {
// Check if the up arrow key is pressed and the dino is not already jumping
if (e.key === "ArrowUp" && !dino.isJumping && dino.y === canvas.height - dino.height) {
dino.isJumping = true; // Set the jump flag to true
dino.jumpCount = 0; // Reset the jump counter
}
});

// Function to update and display the player's score
function updateScore() {
const scoreElement = document.getElementById("score"); // Get the score element
scoreElement.textContent = "Score: " + score; // Update the score display
}

gameLoop(); // Start the game loop
</script>
</body>
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.