webdevpuneet.com

Frog Crossing Game: Traffic, Logs and Diving Turtles

Frog Crossing Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

About this UI Snippet

Frog Crossing: Tile Hops, Riding Platforms, Diving Turtles and Hand-Drawn Canvas Art

Screenshot of the Frog Crossing Game snippet rendered live

Frog Crossing is the classic road-and-river arcade game: hop up through four lanes of traffic, cross a river by riding logs and turtles, and land the frog in each of the five lily-pad homes at the top. This snippet builds a complete version on one HTML5 <canvas> with no libraries and no image files. Every car, truck, bulldozer, log, turtle, fly and the frog itself is drawn with canvas paths, and the scenery (hedge, grass with flowers, asphalt with lane lines and curbs) is painted once into an off-screen canvas. It plays with the arrow keys or WASD, an on-screen D-pad, or swipes and taps on the game.

Hops are tweens between tiles

The frog lives on a 9 by 11 grid of 40-pixel tiles. A hop is a 0.11-second tween from one tile to the next: frogX() and frogY() interpolate the position, the sprite lifts and stretches its legs at the top of the arc, and it rotates to face the way it jumped. A key pressed during a hop is queued and fires the moment the frog lands, so quick double-taps feel responsive. On land the frog snaps back to the nearest column; on water it keeps its exact position so it can ride.

Lanes that wrap forever

Each lane is a row of evenly spaced items (cars, trucks, logs or turtle groups) moving at its own speed. When an item leaves one side it jumps back by the lane's full repeat length, so traffic never runs out and the spacing never changes. Cars are tested every frame, even in mid-hop, against a slightly smaller hitbox. In the river the rule is reversed: the frog must be standing on a log or a turtle, and it drifts with it. Drifting off the edge of the screen is a splash.

Diving turtles, flies and the timer

Some turtle groups dive on a 7-second cycle: they float, blink while sinking (still safe), vanish under ripples (not safe) and rise again, so you have to time your crossing. Every life has 30 seconds, shown as a bar that turns yellow and red and ticks in the last five seconds, and the time you have left becomes a bonus when you reach a home. Now and then a fly lands in an empty home, worth 200 more points if you get there in time. Hitting the hedge between homes, or a home that is already full, costs a life.

Levels and scoring

Every new row you reach is worth 10 points, a home 50 plus the time bonus, and filling all five homes 1000 and the next level. Each level makes the traffic faster and denser, spaces the logs further apart and makes more turtles dive. An extra life comes at 5000 points. Deaths have their own animation (a squashed frog on the road, splash rings in the river), and sound effects for hops, homes, flies, splats, splashes and the level fanfare are made with the Web Audio API, with a mute button. The game pauses on P, a button or a tab switch, and the best score is kept in localStorage.

What's included

Features

Hand-drawn canvas art: top-down cars, trucks, sports cars and bulldozers, bark-textured logs, paddling turtles, lily pads, a fly and an animated frog
Static scenery painted once into an off-screen canvas, with an animated river drawn on top
Tile hops as short tweens with a lift, leg stretch and facing direction, plus a queued next hop
Wrapping lanes with per-lane speed and spacing, car collision even in mid-hop, and riding logs and turtles
Turtles that dive on a timed cycle with a blinking warning
A 30-second timer per life with a time bonus, flies worth 200, extra life at 5000 points
Levels that speed up and densify traffic and make more turtles dive
Squash and splash death animations, particles and floating score text
Keyboard, D-pad and swipe or tap controls, pause (P, button or tab switch) and Web Audio sound with mute
Best score saved in localStorage with try/catch

Step by step

How to Use

  1. 1
    HopPress Hop in, then hop with the arrow keys or WASD, the D-pad, or by swiping on the game (a quick tap hops forward). A press during a hop is queued.
  2. 2
    Cross the roadFour lanes of trucks, sports cars, cars and bulldozers move at different speeds and directions. Wait for a gap, hop, and use the grass strip in the middle as a safe stop.
  3. 3
    Ride across the riverYou cannot swim: land on a log or a turtle and ride it. Turtles that blink are about to dive, so hop off before they go under, and do not drift off the edge.
  4. 4
    Fill the five homesLand exactly in a lily-pad notch. Hitting the hedge or a full home costs a life. A fly in a home is worth 200 more.
  5. 5
    Beat the clockEach life has 30 seconds. The bar at the bottom shows the time, and whatever is left becomes a bonus when you reach a home.
  6. 6
    Clear levelsFill all five homes for +1000 and the next level, where the traffic is faster and more turtles dive. Press P to pause; your best score is saved.

Real-world uses

Common Use Cases

Learning grid movement with platforms
Discrete tile hops combined with continuous riding on moving platforms is a compact example of mixing two movement models cleanly.
Canvas art without image files
Vehicles, logs, turtles and the frog are all drawn with paths and gradients, a useful reference for building a polished look with no assets.
A swipe-and-tap mobile game template
Swipes, taps, a D-pad and the keyboard all call one hop function with a queued input, which makes a touch game feel responsive.
An interactive 404 page or loading screen
A quick, self-contained arcade game is a friendly distraction on an error page, like the Maze Runner Arrow-Key Game.
A base for a bigger arcade game
The lane table and level scaling make it easy to add crocodiles, snakes on the median, a two-player mode or new themes.

Got questions?

Frequently Asked Questions

When the frog is on a water row and not in mid-hop, the game looks for an item in that lane under the frog (and, for turtles, one that is not under water). If there is none the frog drowns; if there is one, the frog moves by the lane speed every frame, which carries it along. Moving past either edge of the screen also ends in a splash.

Some turtle groups have a timer that runs on a 7-second cycle: about 4 seconds floating, a blinking sinking phase (still safe), a phase under water (not safe) and a rising phase. On level 3 and above every second group dives instead of every third.

Collisions with traffic are checked every frame using the frog's interpolated position, not only when it lands, so hopping into the side of a passing car still counts. The car hitbox is a few pixels smaller than the drawing so near misses feel fair.

Each new row you reach in a life gives 10 points, a home gives 50 plus 10 for every second left on the timer, a fly in that home gives 200 more, and filling all five homes gives 1000. An extra life is awarded at 5000 points.

The LANES object maps each row to what moves in it (log, turtle, truck, sport, car or dozer), its speed in pixels per second (positive moves right), the item length in tiles and the gap in tiles. buildLanes() applies the level scaling, so a change there carries through every level.

Yes. Swipe in any direction to hop that way, tap to hop forward, or use the D-pad. The game pauses on its own when you switch tabs.

Copy & paste

Source Code

<div class="game-card">
  <div class="game-header">
    <div class="game-title">
      <span class="game-icon">🐸</span>
      <h2>Frog Crossing</h2>
    </div>
    <div class="stat-badges">
      <span class="badge">Lvl <b id="level">1</b></span>
      <span class="badge">Home <b id="homes">0/5</b></span>
      <span class="badge">Best <b id="best-score">0</b></span>
    </div>
  </div>

  <div class="canvas-wrap">
    <canvas id="game-canvas" width="360" height="440"></canvas>
    <div class="hud">
      <span id="hud-score">0</span>
      <span class="lives" id="hud-lives">🐸🐸🐸</span>
    </div>
    <div class="overlay" id="start-overlay">
      <div class="overlay-card">
        <p class="overlay-title">Frog Crossing</p>
        <p class="overlay-sub">Hop across the road, ride the logs and turtles over the river, and fill all five lily-pad homes before the timer runs out. Watch out: some turtles dive!</p>
        <button class="btn btn-primary" id="btn-start">Hop in</button>
      </div>
    </div>
    <div class="overlay hidden" id="pause-overlay">
      <div class="overlay-card">
        <p class="overlay-title">Paused</p>
        <button class="btn btn-primary" id="btn-resume">Resume</button>
      </div>
    </div>
    <div class="overlay hidden" id="gameover-overlay">
      <div class="overlay-card">
        <p class="overlay-title" id="over-title">Game Over</p>
        <p class="overlay-sub" id="final-score">Score 0</p>
        <p class="overlay-best" id="best-msg"></p>
        <button class="btn btn-primary" id="btn-retry">Play again</button>
      </div>
    </div>
  </div>

  <div class="controls">
    <div class="dpad-cross">
      <button class="ctrl-btn up" id="btn-up" aria-label="Up">▲</button>
      <button class="ctrl-btn left" id="btn-left" aria-label="Left">◀</button>
      <button class="ctrl-btn right" id="btn-right" aria-label="Right">▶</button>
      <button class="ctrl-btn down" id="btn-down" aria-label="Down">▼</button>
    </div>
    <div class="side-box">
      <div class="side-btns">
        <button class="mini-btn" id="btn-pause" type="button" aria-label="Pause">⏸</button>
        <button class="mini-btn" id="btn-mute" type="button" aria-label="Sound on" aria-pressed="false">🔊</button>
      </div>
      <div class="tip-box">Eat the fly in a home for +200. Get home fast for a time bonus.</div>
    </div>
  </div>

  <p class="hint-text">Arrow keys / WASD, the D-pad, or swipe (tap to hop forward) · P to pause</p>
</div>

Extend with AI

Take This Snippet Further With AI

Paste this snippet's HTML, CSS and JS into an AI assistant like Claude and ask it to explain the hop tween and queued input, the wrapping lane system, the riding logic and the diving-turtle cycle. It is a good base to extend: ask for crocodiles in the river whose mouths are deadly, a snake on the median, a lady frog to escort home for bonus points, a two-player alternating mode, day and night themes, or a port to a React component with the game state in a ref. Treat it as a working prototype to question and rebuild.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a complete Frogger-style game on an HTML5 canvas in plain HTML, CSS and JavaScript, with no libraries and no image files.

Requirements:
- A 9 by 11 grid of 40px tiles: a top bank with five lily-pad homes, four river rows, a grass median, four road rows and a grass start row. Paint the static scenery once into an off-screen canvas and draw an animated river on top.
- Hand-drawn canvas art: top-down cars, trucks, sports cars and bulldozers with shadows, windows and lights; bark-textured logs; paddling turtles; lily pads; a fly; and a frog that faces its hop direction and stretches its legs mid-hop.
- Hops are 0.11 s tweens between tiles with one queued input. Snap to columns on land, keep exact x on water.
- Lanes are rows of evenly spaced items that wrap around by the lane's repeat length. Check car collisions every frame with the interpolated frog position. On water the frog must stand on a log or a surfaced turtle and drifts with it; leaving the screen is a splash.
- Some turtle groups dive on a timed cycle (float, blink while sinking, under water, rise).
- A 30 s timer per life with a bar and warning ticks, time bonus on reaching a home, flies worth 200 in empty homes, 10 points per new row, 1000 for filling all homes, an extra life at 5000, and levels that make traffic faster and denser and more turtles dive.
- Death animations (squash on the road, splash rings in the river), particles, floating score text and level banners. Keyboard, D-pad and swipe or tap controls; pause on P, a button and tab switch; Web Audio sound with mute; best score in localStorage (with try/catch).

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.