<!DOCTYPE html>
<html>
<head>
  <title>Basic Doodle Jump HTML Game</title>
  <meta charset="UTF-8">
  <style>
    html, body {
      height: 100%;
      margin: 0;
    }

    body {
      display: flex;
      align-items: center;
      justify-content: center;
    }

    canvas {
      border: 1px solid black;
      display: none; /* Hidden by default */
      background: linear-gradient(to bottom, #87CEEB, #f0f8ff); /* Sky-like gradient */
    }

    /* Game Over and Play Screens Style */
    #game-over, #play-screen {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: rgba(0, 0, 0, 0.8);
      color: white;
      padding: 20px;
      text-align: center;
      border-radius: 10px;
    }

    #game-over {
      display: none; /* Hidden by default */
    }
  </style>
</head>
<body>
<canvas width="375" height="667" id="game"></canvas>

<!-- Play Screen -->
<div id="play-screen">
  <h1>Doodle Jump</h1>
  <button id="play">Play</button>
</div>

<!-- Game Over Screen -->
<div id="game-over">
  <h1>Game Over</h1>
  <p>Your score is: <span id="score"></span></p>
  <button id="restart">Restart</button>
</div>

<script>
const canvas = document.getElementById('game');
const context = canvas.getContext('2d');

// width and height of each platform and where platforms start
const platformWidth = 65;
const platformHeight = 20;
const platformStart = canvas.height - 50;

// player physics
const gravity = 0.33;
const drag = 0.3;
const bounceVelocity = -12.5;

// minimum and maximum vertical space between each platform
let minPlatformSpace = 15;
let maxPlatformSpace = 20;

// variables to store game state
let platforms = [];
let doodle = {};
let playerDir = 0;
let keydown = false;
let prevDoodleY = 0;
let gameOver = false; // Track game over state
let score = 0; // Initialize score

const gameOverScreen = document.getElementById('game-over');
const playScreen = document.getElementById('play-screen');
const scoreDisplay = document.getElementById('score');
const playButton = document.getElementById('play');
const restartButton = document.getElementById('restart');

// Function to reset the game state
function resetGame() {
  // Reset doodle and platforms
  platforms = [{
    x: canvas.width / 2 - platformWidth / 2,
    y: platformStart
  }];
  
  doodle = {
    width: 40,
    height: 60,
    x: canvas.width / 2 - 20,
    y: platformStart - 60,
    dx: 0,
    dy: 0
  };

  // Refill the screen with platforms
  let y = platformStart;
  while (y > 0) {
    y -= platformHeight + random(minPlatformSpace, maxPlatformSpace);
    let x = random(25, canvas.width - 25 - platformWidth);
    platforms.push({ x, y });
  }

  // Reset game variables
  playerDir = 0;
  keydown = false;
  prevDoodleY = doodle.y;
  gameOver = false;
  score = 0;
}

// Get a random number between the min (inclusive) and max (exclusive)
function random(min, max) {
  return Math.random() * (max - min) + min;
}

// Game loop
function loop() {
  if (!gameOver) {
    requestAnimationFrame(loop);
    context.clearRect(0, 0, canvas.width, canvas.height);

    // Apply gravity to doodle
    doodle.dy += gravity;

    if (doodle.y < canvas.height / 2 && doodle.dy < 0) {
      platforms.forEach(function(platform) {
        platform.y += -doodle.dy;
      });

      while (platforms[platforms.length - 1].y > 0) {
        platforms.push({
          x: random(25, canvas.width - 25 - platformWidth),
          y: platforms[platforms.length - 1].y - (platformHeight + random(minPlatformSpace, maxPlatformSpace))
        });

        minPlatformSpace += 0.5;
        maxPlatformSpace += 0.5;
        maxPlatformSpace = Math.min(maxPlatformSpace, canvas.height / 2);
      }
    } else {
      doodle.y += doodle.dy;
    }

    if (!keydown) {
      if (playerDir < 0) {
        doodle.dx += drag;
        if (doodle.dx > 0) {
          doodle.dx = 0;
          playerDir = 0;
        }
      } else if (playerDir > 0) {
        doodle.dx -= drag;
        if (doodle.dx < 0) {
          doodle.dx = 0;
          playerDir = 0;
        }
      }
    }

    doodle.x += doodle.dx;

    if (doodle.x + doodle.width < 0) {
      doodle.x = canvas.width;
    } else if (doodle.x > canvas.width) {
      doodle.x = -doodle.width;
    }

    context.fillStyle = 'green';
    platforms.forEach(function(platform) {
      drawPlatform(platform.x, platform.y);
      if (
        doodle.dy > 0 &&
        prevDoodleY + doodle.height <= platform.y &&
        doodle.x < platform.x + platformWidth &&
        doodle.x + doodle.width > platform.x &&
        doodle.y < platform.y + platformHeight &&
        doodle.y + doodle.height > platform.y
      ) {
        doodle.y = platform.y - doodle.height;
        doodle.dy = bounceVelocity;
      }
    });

    // Draw doodle with gradient
    drawDoodle(doodle.x, doodle.y, doodle.width, doodle.height);
    prevDoodleY = doodle.y;

    // Check for game over condition
    if (doodle.y > canvas.height) {
      gameOver = true;
      scoreDisplay.textContent = score; // Display score
      gameOverScreen.style.display = 'block'; // Show game over screen
    }

    score = Math.floor(doodle.y / 10); // Update score
    platforms = platforms.filter(function(platform) {
      return platform.y < canvas.height;
    });
  }
}

// Draw the doodle with a gradient fill
function drawDoodle(x, y, width, height) {
  const gradient = context.createLinearGradient(x, y, x, y + height);
  gradient.addColorStop(0, 'yellow');
  gradient.addColorStop(1, 'orange');
  context.fillStyle = gradient;
  
  // Draw the doodle as a simple circle
  context.beginPath();
  context.arc(x + width / 2, y + height / 2, width / 2, 0, Math.PI * 2);
  context.fill();

  // Add a simple shadow
  context.shadowColor = 'rgba(0,0,0,0.5)';
  context.shadowBlur = 15;
  context.shadowOffsetX = 3;
  context.shadowOffsetY = 3;
}

// Draw platforms with a shadow and round edges
function drawPlatform(x, y) {
  context.fillStyle = 'green';
  context.shadowColor = 'rgba(0, 0, 0, 0.2)';
  context.shadowBlur = 10;
  context.shadowOffsetX = 2;
  context.shadowOffsetY = 2;

  // Draw rounded platforms
  context.beginPath();
  context.roundRect(x, y, platformWidth, platformHeight, 10); // Rounded corners
  context.fill();
}

// Listen to keyboard events to move doodle
document.addEventListener('keydown', function(e) {
  if (gameOver) return; // Do nothing if game is over
  if (e.which === 37) {
    keydown = true;
    playerDir = -1;
    doodle.dx = -3;
  } else if (e.which === 39) {
    keydown = true;
    playerDir = 1;
    doodle.dx = 3;
  }
});

document.addEventListener('keyup', function(e) {
  keydown = false;
});

// Play button to start the game
playButton.addEventListener('click', function() {
  playScreen.style.display = 'none'; // Hide the play screen
  canvas.style.display = 'block'; // Show the canvas
  resetGame(); // Reset game state
  requestAnimationFrame(loop); // Start the game loop
});

// Restart the game
restartButton.addEventListener('click', function() {
  gameOverScreen.style.display = 'none'; // Hide game over screen
  resetGame(); // Reset the game state
  requestAnimationFrame(loop); // Restart the game loop
});

// Add support for roundRect
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
  if (w < 2 * r) r = w / 2;
  if (h < 2 * r) r = h / 2;
  this.beginPath();
  this.moveTo(x + r, y);
  this.arcTo(x + w, y, x + w, y + h, r);
  this.arcTo(x + w, y + h, x, y + h, r);
  this.arcTo(x, y + h, x, y, r);
  this.arcTo(x, y, x + w, y, r);
  this.closePath();
  return this;
}
</script>
</body>
</html>
 

HTML Online Editor & Compiler

Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML. You can also specify the stylesheet information in styles.css tab and scripts information in scripts.js tab and start coding.

About HTML

HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.

Syntax help

Fundamentals

  • Any HTML document must start with document declaration <!DOCTYPE html>
  • HTML documents begin with <html> and ends with </html>
  • Headings are defined with <h1> to <h6> where <h1> is the highest important heading and <h6> is the least important sub-heading.
  • Paragrahs are defined in <p>..</p> tag.
  • Links are defined in <a> tag.

    Example:

    <a href="https://onecompiler.com/html">HTML online compiler</a>
    
  • Images are defined in <img> tag, where src attribute consists of image name.
  • Buttons are defined in <button>..</button> tag
  • Lists are defined in <ul> for unordered/bullet list and <ol> for ordered/number list, and the list items are defined in <li>.

HTML Elements and Attributes

  • HTML element is everything present from start tag to end tag.
  • The text present between start and end tag is called HTML element content.
  • Anything can be a tagname but it's preferred to put the meaningful title to the content present as tag name.
  • Do not forget the end tag.
  • Elements with no content are called empty elements.
  • Elements can have attributes which provides additional information about the element.
  • In the below example, href is an attribute and a is the tag name.

    Example:

    <a href="https://onecompiler.com/html">HTML online compiler</a>
    

CSS

CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.

Example:

Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.

body{
  padding: 25px;
}
.title {
	color: #228B22;
	font-family: Candara;
}

HTML Tables

  • HTML Tables are defined in <table> tag.
  • Table row should be defined in <tr> tag
  • Table header should be defined in <th> tag
  • Table data should be defined in <td> tag
  • Table caption should be defined in <caption> tag

HTML-Javascript

  • Javascript is used in HTML pages to make them more interactive.
  • <script> is the tag used to write scripts in HTML
  • You can either reference a external script or write script code in this tag.

Example

<script src="script.js"></script>