Sunday, December 15, 2024

Game development courses sri lanka online for beginner projects student BIT UCSC UOM

Guess the Number Game

Maths Maximum Minimum Guess the Number Game !

I'm thinking of a number between 1 and 100.

Can you guess what it is?

The video is the first episode of a series on creating a video game using HTML5

Here is the reconstructed code based on the provided explanation from the conversation. The code creates a canvas element in HTML, draws text on it, and demonstrates various optimization techniques using JavaScript. The steps described involve modifying variables, optimizing repetitive code using functions, and debugging with the browser's developer tools.

Here’s the complete code:

HTML and JavaScript Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Canvas Text Example</title>
</head>
<body>
  <canvas id="myCanvas" width="800" height="400" style="border:1px solid #000;"></canvas>
  <script>
    // Get the canvas and context
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');

    // Variables for positioning
    let x = 50; // Initial x position
    let y = 50; // Initial y position
    const speedX = 40; // Distance between letters (x-axis)
    const speedY = 5;  // Distance between letters (y-axis)

    // Function to draw text
    function drawText(letter) {
      ctx.font = "30px Arial";
      ctx.fillText(letter, x, y); // Draw the letter at (x, y)
      x += speedX; // Update x position
      y += speedY; // Update y position
    }

    // Draw multiple letters
    function drawMultipleLetters() {
      drawText("P");
      drawText("I");
      drawText("E");
      drawText("C");
      drawText("E");
      drawText("S");
    }

    // Call the function to draw letters
    drawMultipleLetters();

    // Debugging using console.log
    console.log("Canvas drawing complete.");
    console.log("Final x position:", x);
    console.log("Final y position:", y);
  </script>
</body>
</html>

Code Explanation

  1. Canvas Setup:

    • A canvas element is created in the HTML file with a specified width and height.
    • The getContext('2d') method is used to enable 2D drawing.
  2. Variables:

    • Variables x and y set the starting position of the text.
    • speedX and speedY control the spacing between letters horizontally and vertically.
  3. Functionality:

    • A drawText() function is defined to draw a single letter on the canvas and update the x and y positions.
    • The drawMultipleLetters() function calls drawText() multiple times to draw a sequence of letters.
  4. Optimization:

    • By using a function (drawText), repetitive code is avoided, making it easier to modify the logic.
    • speedX and speedY variables allow changing the spacing in one place instead of modifying multiple lines.
  5. Debugging:

    • Debugging tools, such as console.log(), are used to log information about the drawing process.
  6. Browser Debugging:

    • The F12 developer tools in the browser can be used to inspect variables and control the execution flow by setting breakpoints.

Output

The code will draw the word "PIECES" on the canvas, with each letter positioned at a progressively increasing distance along the x and y axes.

Let me know if you'd like further refinements or additional explanations!






No comments:

Post a Comment