In Trash Toss, players control a character who must throw trash into recycling bins while avoiding obstacles. The goal is to score points by successfully tossing as much trash as possible within a time limit.
Gameplay:
Controls:
Use the arrow keys to move the character left and right.
Press the space bar to throw trash.
Mechanics:
The game screen shows a simple street scene with a character at the bottom and two recycling bins (one for paper and one for plastics) at the top.
Trash items fall from the top of the screen at random intervals (represented by simple shapes, e.g., squares for paper, circles for plastic).
Players must catch the trash items by positioning the character and throwing them into the correct bin.
Scoring:
Players earn points for successfully tossing trash into the right bin.
A penalty is given for tossing trash into the wrong bin or missing completely.
Bonus points for combos: if players throw three pieces of trash in a row into the correct bin without a miss.
Time Limit:
The game lasts for 60 seconds, and the score is tallied at the end.
Visuals:
Use simple geometric shapes to represent the character (e.g., a rectangle), trash (e.g., squares, circles), and bins (e.g., rectangles with labels).
Background can be a solid color or simple gradients to represent a street scene.
Sounds (Optional):
Add simple sound effects for throwing trash, scoring points, or missing.
Implementation:
Set up the HTML canvas.
Create the game loop to update the character's position and check for collisions with trash and bins.
Use setInterval to spawn trash at random intervals and animate their fall.
Handle input events for moving the character and throwing trash.
This simple game can be engaging and fun while promoting the theme of recycling and waste management!
Assistant
Sure! Below is a complete implementation of the Trash Toss game using HTML5 Canvas and JavaScript. This implementation covers the game mechanics you've described, including character movement, trash spawning and throwing, scoring, and a countdown timer.
You can copy the following code into an .html file and open it in your web browser to play the game.
The HTML consists of a <canvas> element where the game is rendered.
A <div> with the ID scoreboard displays the current score and remaining time.
Styling:
Basic CSS centers the canvas and adds a gradient background to represent the sky and street.
JavaScript Components:
Game Variables: Keep track of the score, time left, game state, and combo count.
Character: Represented as a blue rectangle that the player can move left and right.
Bins: Two recycling bins (one for paper and one for plastic) positioned at the top of the canvas.
Trash Items: An array to store falling trash objects, each with a type, position, and speed.
Input Handling: Listen for keyboard events to move the character and throw trash.
Timer: A countdown from 60 seconds using setInterval. When the time runs out, gameOver is set to true.
Trash Spawning: Trash items are spawned at random intervals and fall from the top of the screen.
Game Loop (gameLoop):
Update: Handles character movement, trash movement, collision detection, scoring, and updating the scoreboard.
Draw: Renders bins, trash items, thrown trash, and the character on the canvas.
Game Over: Displays a game over screen with the final score when time runs out.
Throwing Mechanism: When the space bar is pressed, the closest trash above the character is thrown towards the bins.
Collision Detection: Checks if thrown trash intersects with any recycling bin. Correct throws increase the score and possibly trigger a combo bonus, while incorrect throws or misses penalize the score.
Controls:
Arrow Keys: Move the character left (ArrowLeft) and right (ArrowRight).
Space Bar: Throw trash into the bins.
Visuals:
Simple geometric shapes represent the character (rectangle), trash (squares for paper, circles for plastic), and bins (colored rectangles with labels).
How to Play
Move the character left and right using the arrow keys.
Throw trash into the correct recycling bin by pressing the space bar when trash is aligned with the bins.
Score points by successfully throwing trash into the correct bin.
Avoid penalties by not missing throws or throwing trash into the wrong bin.
Achieve combos by successfully throwing three pieces of trash in a row.
Aim for a high score within the 60-second time limit!
Feel free to customize the game further by adding sound effects, improving graphics, or introducing additional game mechanics. Enjoy playing Trash Toss!