Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Car Racer JavaScript Game Exercise Vanilla JavaScript
Rating: 4.3 out of 5(31 ratings)
1,822 students

Car Racer JavaScript Game Exercise Vanilla JavaScript

Explore how to create a custom JavaScript Car racer game from scratch using only JavaScript - Practice Exercise
Created byLaurence Svekis
Last updated 11/2024
English
English [Auto],

What you'll learn

  • Create a car racing game from scratch
  • Apply JavaScript and move elements within a page using JavaScript
  • Detect overlapping collision between two elements
  • Run animations within JavaScript

Course content

2 sections32 lectures2h 23m total length
  • Car Racer Introduction.2:48

    Build a car racing game from scratch in vanilla JavaScript with keyboard controls, collision detection, and a lives-based win or lose system, plus access to the source code.

  • New JavaScript Racer Game Exercise3:45

    Set up a vanilla JavaScript racing game from scratch using Brackets and Chrome, linking HTML, CSS, and JavaScript files and previewing locally to prepare the game play area.

  • HTML setup container2:41

    Set up the core HTML for the car racer game by creating a start button, a dashboard with score, lives, and speed, and a main container.

  • Car images Download0:02
  • Gameplay CSS setup styling4:48

    Apply css styling to the game container with height, width, relative positioning, green background, and hidden overflow, then style the start button and road elements with absolute positioning and borders.

  • JavaScript Element Selection6:16

    Set up a vanilla JavaScript game by selecting DOM elements with querySelector, caching them in constants, and starting requestAnimationFrame loop once start button sets gamePlay to true for keyboard input.

  • JavaScript Event Listeners5:14

    Introduce event listeners to drive a vanilla JavaScript car racer game, wiring start game actions, key down, and key up actions, and dynamically create and manage the player car element.

  • Setup Player Car and Start Functions6:27

    Set up the player car with absolute positioning, height, width, and background image; hide the start button and initialize a player object with speed, lives, and score to update dashboard.

  • Source Code Index and CSS0:37
  • JavaScript Road Building5:06

    Build a dynamic road for the car racer game by looping to create 13 road segments inside a player object, starting the board, and adding white center lines.

  • Interaction with Keyboard11:59

    Handle keyboard input in vanilla JavaScript by mapping arrow keys to booleans, preventing default actions, and moving a player's car via x and y updates in an animation loop.

  • JavaScript Source Code0:37
  • Update Dashboard Values5:30

    Update the dashboard values by rounding the speed and multiplying for mph. Style the dashboard with a black background, white text, centered, padded, and add braking, boundary constraints, road movement.

  • Moving Road JavaScript6:10

    Create a move road function that uses querySelectorAll to update each road segment’s top position, looping through dynamically added elements and wrapping them to the top as speed changes.

  • Tweak moving road Side move7:16

    Stabilize road movement by using a temporary speed holder to maintain consistent spacing, then widen the road to 55, and add left-right motion with a mover and random offset.

  • Source Code0:57
  • Keep player on the road7:32

    Implement on-road checks by returning road width and left offset, validate the car’s position, and slow the car when it goes off-road to stay within the play area.

  • Source Code1:03
  • JavaScript Game Adding other Cars10:18

    Add other cars to the road by dynamically creating car elements with unique IDs and random colors. Start the game to set up these cars and animate them at speeds.

  • Making other cars move7:02

    Loop through each car element, update its top position using its speed to move the bad guys, and reset cars off-screen with a speed and color, preparing for collision detection.

  • JavaScript Element Collision detection Element Overlap Detection8:59

    Use getBoundingClientRect to compute element bounding boxes and detect overlapping in a vanilla JavaScript game, testing vertical and horizontal collisions between the player and enemies.

  • Crash Checker JavaScript Collide3:43

    Enhance the car racer game by implementing collision detection responses: reduce player speed to zero, decrement lives, reset enemies, and trigger a gradual game end with a game counter.

  • Source Code0:57
  • JavaScript Game Over end Game planning2:59

    Explore end-game logic in a vanilla JavaScript car racer: monitor countdown, adjust player and car positions, stop gameplay at zero, and reveal the start button to replay.

  • Enemy collision detection Car hit Car Check4:17
  • Source Code1:55
  • Final Game Over Play JavaScript6:01

    Set up a game over function and increment score when the car passes cars to pass threshold. Create finish line, style it, append to the container, and adjust start button.

  • Next level Restart Game4:12

    learn to restart a vanilla JavaScript car racer game by clearing the container, resetting score, lives, and speed, and canceling animation frames for a fresh start.

  • JavaScript Exercise Code Review and Overview10:49

    Set up a dynamic game arena with a road, cars, and score display using vanilla JavaScript to manage movement, collisions, scoring, and replay via keyboard input.

  • Final Source Code2:40

Requirements

  • Fundamental JavaScript concepts
  • Access to computer
  • Use of editor to write code
  • HTML and CSS knowledge

Description

Practice and learn more about JavaScript DOM while building a fun interactive car racing game from scratch.  

No libraries, no shortcuts just learning JavaScript!

Source code is included to get you started quickly.

Prefect course for beginners to learn more about JavaScript!!! Step by step learning with all steps included

Fun and exciting way to learn and practice JavaScript.

Beginner JavaScript knowledge is required as the course covers only JavaScript relevant to the building of the game.  Also HTML and CSS knowledge is essential as scope of this course is all JavaScript focused.  

  • Learn how to setup your gameplay area using HTML and CSS

  • Add JavaScript setting event listeners

  • Add Keyboard actions to move an html div using keyboard arrow keys

  • Run animationframe to change element position and respond to the users commands

  • Setup start game functionality 

  • Create a dynamic JavaScript road

  • Create elements within JavaScript

  • Access the JavaScript Document Object Model to manipulate element properties

  • Setup and add values within an object to track location and move

  • Setup a dashboard to output variables for the user like score and speed

  • Add road movement

  • Track speed dynamically with braking and accelerating 

  • Add other cars and have them move within the gameplay area

  • Check for element overlap and detect collisions

  • Score and extend gameplay

All this is included and a whole lot more.  

Along with friendly support in the Q&A to help you learn and answer any questions you may have.

Try it now you have nothing to lose, comes with a 30 day money back guarantee.  

Start building your own version of the game today!!!!



Who this course is for:

  • Beginners to JavaScript
  • Anyone who wants to practice writing JavaScript
  • Anyone who wants to learn to make a JavaScript game without any libraries
  • Web developers
  • Webmasters