/Adlicious

Primary LanguageJavaScript

The HTML and JavaScript code in the assessment represents a simple soccer (football) game implemented in a web browser using the HTML5 canvas element and vanilla JavaScript. Let's break down the code and understand how the game functions:

  • The HTML file starts with the DOCTYPE declaration and contains a basic structure with a head and body section.
  • The tags provide information about the document, such as character encoding, viewport settings, and a description.
  • The title of the game is set using the <title> tag.
  • The game's stylesheet is linked using the tag.
  • Inside the section, there is a element where the game graphics will be drawn.
  • A "PLAY" button is created using a
    container with a nested element, which also includes an SVG arrow icon.

  • The JavaScript code begins by selecting elements from the DOM, including the play button and the canvas.
  • Various constants and variables are defined to control the game mechanics, such as player dimensions, ball properties, goal properties, mini-player properties, score, lives, and more.
  • Event listeners are set up to handle keyboard, mouse, and touch interactions for controlling the player's movement.
  • The main game loop function, draw(), is defined. This function is repeatedly called using the requestAnimationFrame() method to update and draw the game elements on the canvas.
  • Within the draw() function:
    • The canvas is cleared.
    • Game elements are drawn on the canvas, including the mini-players, goal, ball, player character, score, and lives.
    • Collision detection functions check for collisions between the ball and mini-players, as well as between the ball and the goal.
    • Logic is implemented for ball movement, collision with walls, collision with the player character, and handling lives and score.
    • Keyboard input (left and right arrow keys) controls the movement of the player character.
    • The ball's position is updated based on its velocity.
  • Image assets for the ball, goal, and mini-players are loaded and drawn on the canvas.
  • The draw() function is triggered when the "PLAY" button is clicked.

Logic

  • The game involves controlling a player character at the bottom of the canvas to hit a ball into a goal at the top.
  • Mini-players (cones) are placed on the canvas, and the player scores points by hitting the ball into them.
  • The game keeps track of the player's score and remaining lives.
  • Collisions are detected using bounding box checks between game elements (ball, player character, mini-players, and goal).
  • Keyboard, mouse, and touch input are used to control the player character's movement.

User Interface:

  • The "PLAY" button allows the player to start the game.
  • The player's score and remaining lives are displayed on the canvas during gameplay.
  • An alert is shown when a goal is scored, displaying the current score and remaining lives.

Overall, this code creates a simple soccer game where the player interacts with the game using keyboard, mouse, or touch controls to move the player character and hit the ball into the goal while avoiding obstacles (mini-players). The game loop continuously updates and renders the game elements to provide a basic gaming experience.