Skip to main content
This lesson demonstrates two practical ways to use GitHub Copilot in VS Code: comment-driven autocomplete and the Agent (edit) mode for analyzing and improving code. We’ll walk through short examples from a small browser game project (showing how to add sound effects, improve collision detection, and save a high score).
Make sure you have the GitHub Copilot Chat extension installed in VS Code and that Copilot is enabled for the workspace. For reference, see the official GitHub Copilot documentation.
Quick feature overview:

1) Autocomplete by commenting

Copilot can infer intent from a comment and propose a context-aware implementation based on nearby code and the repository. Example context (existing snippet):
Now add this comment in the file:
Copilot will propose an implementation grounded in the project context. Accepting the suggestion (for example, pressing Tab) gives a concise, usable function like this:
Note how Copilot inferred the localStorage key and the gameState.highScore property from the surrounding project context and also reminded you to wire up the UI initial setup.
Avoid storing large or complex objects in localStorage unless intentional. Prefer storing a single value (e.g., a number or string). For example, store gameState.highScore instead of the entire gameState object.

2) Use the Agent / Edit mode to analyze and improve functions

The Agent/edit mode is powerful for selecting an existing block of code and requesting targeted edits: explanations, bug fixes, performance improvements, or feature additions (like sound effects). You can preview the generated diff and accept or reject changes. Below is a typical collision detection function before changes:
Common Agent tasks here:
  • Fix incorrect math or typos.
  • Replace overly simplified collision tests with radius-aware checks.
  • Add sound playback hooks on collisions.
  • Mark bricks as cleared so they aren’t processed again.

Preload sound effects once

A good practice is preloading audio assets and providing a small playSound helper so the Agent can simply call playSound('brickHit') when needed:

Improved collision detection with sound and robust checks

After requesting improvements from the Agent, you might end up with a cleaned-up and corrected collision handler that:
  • Uses proper circle-circle and circle-rectangle collision checks (taking ball radius into account).
  • Calls playSound for collisions.
  • Marks bricks as cleared (brick.status = 0) to prevent repeated collisions.

Paddle collision helper with sound

Encapsulating paddle collision logic in a helper improves readability and makes it easier for the Agent to target only this function in future edits:
When you use the Agent/edit mode, remember to preview the diff. The Agent references your current file contents to produce context-aware suggestions, which helps avoid simple typos (for example, replacing an incorrect Math.any call with the correct Math.sqrt) and adding necessary preloads and sound calls.

Save high score (final, corrected)

Here’s a compact, correct save function and an initial UI setup snippet that you can accept directly via autocomplete or allow the Agent to add for you:

These two Copilot features—comment-driven autocomplete and the Agent/edit mode—let you quickly add small features (like saving high scores), inject media (sound effects), and iteratively refactor or fix logic in place. Always review proposed diffs and test after applying changes. For more, see:

Watch Video