Make me a simple game that works like a CV/Resume with a CAR exploring a map with…
- User
Make me a simple game that works like a CV/Resume with a CAR exploring a map with projects in JS
- Assistant
Certainly! Below is a simple JavaScript game where a car explores a map representing a CV/Resume. As the car reaches different locations on the map, information about various projects is displayed.
You can copy the code below into a single HTML file and open it in a web browser to play the game.
HTML and JavaScript Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>CV Exploration Game</title> <style> canvas { border: 1px solid #000; background-color: #f0f0f0; } #info { margin-top: 10px; font-family: Arial, sans-serif; } #info h2 { margin: 0; } </style> </head> <body> <canvas id="gameCanvas" width="600" height="400"></canvas> <div id="info"> <h2>Welcome!</h2> <p>Use arrow keys to move the car around the map.</p> </div> <script> /** Game Variables **/ const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const car = { x: 50, y: 350, width: 20, height: 10, color: 'blue', speed: 2 }; const projects = [ { name: 'Project Alpha', description: 'A web application built with React and Node.js.', x: 100, y: 100, radius: 15 }, { name: 'Project Beta', description: 'An Android app developed using Kotlin.', x: 300, y: 50, radius: 15 }, { name: 'Project Gamma', description: 'Data analysis project using Python and Pandas.', x: 500, y: 300, radius: 15 }, { name: 'Project Delta', description: 'Machine learning project with TensorFlow.', x: 200, y: 250, radius: 15 } ]; let keys = {}; /** Event Listeners **/ window.addEventListener('keydown', function(e) { keys[e.key] = true; }); window.addEventListener('keyup', function(e) { keys[e.key] = false; }); /** Game Functions **/ function update() { // Move car based on key presses if (keys['ArrowUp']) car.y -= car.speed; if (keys['ArrowDown']) car.y += car.speed; if (keys['ArrowLeft']) car.x -= car.speed; if (keys['ArrowRight']) car.x += car.speed; // Keep car within canvas boundaries if (car.x < 0) car.x = 0; if (car.y < 0) car.y = 0; if (car.x + car.width > canvas.width) car.x = canvas.width - car.width; if (car.y + car.height > canvas.height) car.y = canvas.height - car.height; // Check for collisions with projects projects.forEach(project => { if (isColliding(car, project)) { displayProjectInfo(project); } }); } function draw() { // Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw car ctx.fillStyle = car.color; ctx.fillRect(car.x, car.y, car.width, car.height); // Draw projects projects.forEach(project => { ctx.beginPath(); ctx.arc(project.x, project.y, project.radius, 0, Math.PI * 2); ctx.fillStyle = 'green'; ctx.fill(); ctx.closePath(); }); // Draw map features (optional) // Example: Draw some roads or landmarks // Request next frame requestAnimationFrame(gameLoop); } function gameLoop() { update(); draw(); } function isColliding(rect, circle) { // Simple collision detection between rectangle and circle const distX = Math.abs(circle.x - rect.x - rect.width / 2); const distY = Math.abs(circle.y - rect.y - rect.height / 2); if (distX > (rect.width / 2 + circle.radius)) return false; if (distY > (rect.height / 2 + circle.radius)) return false; if (distX <= (rect.width / 2)) return true; if (distY <= (rect.height / 2)) return true; const dx = distX - rect.width / 2; const dy = distY - rect.height / 2; return (dx * dx + dy * dy <= (circle.radius * circle.radius)); } function displayProjectInfo(project) { const infoDiv = document.getElementById('info'); infoDiv.innerHTML = ` <h2>${project.name}</h2> <p>${project.description}</p> `; } /** Start Game **/ gameLoop(); </script> </body> </html>
How to Use
-
Copy the Code: Copy the entire code snippet above.
-
Create an HTML File: Open a text editor (e.g., Notepad, VS Code) and paste the code into a new file.
-
Save the File: Save the file with an
.htmlextension, for example,cv_game.html. -
Open in Browser: Open the saved HTML file in a modern web browser (e.g., Chrome, Firefox).
-
Play the Game:
- Use the arrow keys on your keyboard to move the car around the map.
- Navigate to the green circles representing different projects.
- As you reach each project, information about the project will be displayed below the canvas.
Code Explanation
- Canvas Setup: We use the HTML5 Canvas API to draw the game elements.
- Game Objects:
car: Represents the player's car with position, size, and movement speed.projects: An array of project objects, each with a name, description, and position.
- Event Listeners:
keydownandkeyupevents are used to track which keys are pressed for smooth movement.
- Game Loop:
- The
updatefunction handles the car's movement and checks for collisions. - The
drawfunction renders the car and projects onto the canvas. requestAnimationFrameis used for smooth animation.
- The
- Collision Detection:
- The
isCollidingfunction checks whether the car is colliding with any project location.
- The
- Display Project Info:
- When a collision is detected, the project's information is displayed in the
info<div>.
- When a collision is detected, the project's information is displayed in the
Customization
You can customize the game according to your needs:
- Add More Projects: Add more project objects to the
projectsarray with their ownname,description, andx,ycoordinates. - Change Car Appearance: Modify the
carobject's properties likecolor,width, andheight. - Enhance the Map: You can add more visual elements to the canvas, such as roads, buildings, or landmarks, to make the map more engaging.
- Improve Collision Detection: For more accurate collision detection, you can enhance the
isCollidingfunction. - Mobile Support: Implement touch controls for mobile device compatibility.
- Styling: Modify the CSS to change fonts, colors, and layout as per your preference.
Notes
- This is a basic implementation meant for educational purposes.
- Make sure to test the game in different browsers to ensure compatibility.
- You can expand upon this foundation to create a more complex and interactive experience.
Enjoy exploring your CV/Resume through this interactive game!
-