Code for the best information:
// Get the canvas and context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Variables
let keys = {};
let player = {
x: canvas.width / 2,
y: canvas.height / 2,
size: 20,
speed: 3,
vx: 0,
vy: 0,
grappling: false,
grapplePoint: null,
maxHealth: 100,
health: 100,
maxStamina: 100,
stamina: 100,
staminaRegen: 0.5,
staminaDrain: 1.0,
ultimateReady: true,
ultimateActive: false,
ultimateDuration: 5000,
ultimateTimer: 0,
};
let gravity = 1.5;
let friction = 0.9;
let squares = [];
let enemies = [];
let particles = [];
let projectiles = [];
let enemySpawnInterval = 2000;
let lastEnemySpawnTime = 0;
let score = 0;
let canShoot = true;
const SHOOT_COOLDOWN = 100;
const FPS = 60;
const FRAME_DURATION = 1000 / FPS;
const musicBtn = document.getElementById('music');
const music = new Audio('./assets/music.mp3');
music.loop = true;
music.volume = 0.1;
let isMusic = false;
// Create squares to grapple on
function createSquares() {
squares = []; // Clear existing squares
for (let i = 0; i < 10; i++) {
squares.push({
x: Math.random() * (canvas.width - 100) + 50,
y: Math.random() * (canvas.height / 2 - 100) + 50,
size: 30,
});
}
}
Assistant
// Get the square under the mouse position
function getSquareUnderMouse(x, y) {
return squares.find(square =>
x >= square.x - square.size / 2 &&
x <= square.x + square.size / 2 &&
y >= square.y - square.size / 2 &&
y <= square.y + square.size / 2
);
}
// Particle effect generator
function createParticles(x, y, color, options = {}) {
let { vxRange = [-1, 1], vyRange = [-3, -1], count = 10 } = options;
for (let i = 0; i < count; i++) {
particles.push({
x: x,
y: y,
vx: Math.random() * (vxRange[1] - vxRange[0]) + vxRange[0],
vy: Math.random() * (vyRange[1] - vyRange[0]) + vyRange[0],
alpha: 1,
color: color,
});
}
}
// Spawn enemies off-screen
function spawnEnemy() {
let side = Math.floor(Math.random() * 4);
let x, y;
if (side === 0) {
// Left
x = -20;
y = Math.random() * canvas.height;
} else if (side === 1) {
// Right
x = canvas.width + 20;
y = Math.random() * canvas.height;
} else if (side === 2) {
// Top
x = Math.random() * canvas.width;
y = -20;
} else {
// Bottom
x = Math.random() * canvas.width;
y = canvas.height + 20;
}
enemies.push({
x: x,
y: y,
size: 20,
speed: 2 + Math.random() * 1.5,
maxHealth: 30,
health: 30,
});
}
// Activate Ultimate
function activateUltimate() {
if (player.ultimateReady) {
player.ultimateActive = true;
player.ultimateReady = false;
player.ultimateTimer = player.ultimateDuration;
player.health = player.maxHealth;
player.stamina = player.maxStamina;
new Audio('./assets/ultimate.mp3').play();
// Add an effect for activation
createParticles(player.x, player.y, '255,215,0', { count: 50, vyRange: [-5, 0] }); // Gold color particles
}
}
// Update function
let lastTime = performance.now();
function update(deltaTime) {
// Player movements
// Left and right movement only
if (keys['a'] || keys['ArrowLeft']) {
player.vx -= player.speed;
}
if (keys['d'] || keys['ArrowRight']) {
player.vx += player.speed;
}
// Apply gravity
player.vy += gravity;
// Apply velocity
player.x += player.vx * 0.1;
player.y += player.vy * 0.1;
// Apply friction
player.vx *= friction;
player.vy *= friction;
// Update player stamina
if (player.grappling) {
if (!player.ultimateActive) {
player.stamina -= player.staminaDrain;
if (player.stamina <= 0) {
player.stamina = 0;
player.grappling = false;
player.grapplePoint = null;
}
}
} else {
if (!player.ultimateActive) {
player.stamina += player.staminaRegen;
if (player.stamina > player.maxStamina) {
player.stamina = player.maxStamina;
}
}
}
// Prevent player from falling through the lava
let lavaHeight = canvas.height - 50; // Lava height from the top
if (player.y + player.size / 2 > lavaHeight) {
player.y = lavaHeight - player.size / 2;
player.vy = 0;
if (!player.ultimateActive) {
// Damage player over time when in lava
player.health -= 0.5;
createParticles(player.x, player.y + player.size / 2, '255,69,0', { vxRange: [0, 0], vyRange: [-1, -0.5] });
}
}
// Grappling physics
if (player.grappling && player.grapplePoint) {
const dx = player.grapplePoint.x - player.x;
const dy = player.grapplePoint.y - player.y;
const dist = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx);
const force = (dist - 100) * 0.02;
player.vx += Math.cos(angle) * force;
player.vy += Math.sin(angle) * force;
}
// Update enemies
enemies.forEach((enemy, eIndex) => {
const angle = Math.atan2(player.y - enemy.y, player.x - enemy.x);
enemy.x += Math.cos(angle) * enemy.speed;
enemy.y += Math.sin(angle) * enemy.speed;
// Check collision with player
const dx = enemy.x - player.x;
const dy = enemy.y - player.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < enemy.size + player.size) {
if (!player.ultimateActive) {
// Damage player
player.health -= 1;
createParticles(player.x, player.y, '255,0,0'); // Red color
}
}
});
// Update projectiles
projectiles.forEach((proj, pIndex) => {
proj.x += proj.vx;
proj.y += proj.vy;
// Remove projectile if off-screen
if (proj.x < 0 || proj.x > canvas.width || proj.y < 0 || proj.y > canvas.height) {
projectiles.splice(pIndex, 1);
}
// Check collision with enemies
enemies.forEach((enemy, eIndex) => {
const dx = proj.x - enemy.x;
const dy = proj.y - enemy.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < proj.size + enemy.size) {
// Enemy hit
enemy.health -= 10; // Damage to enemy
createParticles(enemy.x, enemy.y, '255,0,0'); // Red color
projectiles.splice(pIndex, 1);
// Check if enemy is dead
if (enemy.health <= 0) {
enemies.splice(eIndex, 1);
// Increase player's stamina by +20%
player.stamina += player.maxStamina * 0.2;
if (player.stamina > player.maxStamina) {
player.stamina = player.maxStamina;
}
// Increase score
score += 10; // Adjust the value as needed
// 30% chance to heal player by +30% health
if (Math.random() < 0.3) {
player.health += player.maxHealth * 0.3;
if (player.health > player.maxHealth) {
player.health = player.maxHealth;
}
createParticles(player.x, player.y, '0,255,0', { count: 20 }); // Green color for healing
}
new Audio('./assets/kill.mp3').play();
}
}
});
});
// Update particles
particles.forEach((part, index) => {
part.x += part.vx;
part.y += part.vy;
part.alpha -= 0.02;
if (part.alpha <= 0) {
particles.splice(index, 1);
}
});
// Spawn enemies at intervals
if (performance.now() - lastEnemySpawnTime > enemySpawnInterval) {
spawnEnemy();
lastEnemySpawnTime = performance.now();
}
// Prevent player from moving off-screen
if (player.x - player.size / 2 < 0) {
player.x = player.size / 2;
player.vx = 0;
} else if (player.x + player.size / 2 > canvas.width) {
player.x = canvas.width - player.size / 2;
player.vx = 0;
}
// Update Ultimate
if (player.ultimateActive) {
player.ultimateTimer -= deltaTime;
if (player.ultimateTimer <= 0) {
player.ultimateActive = false;
player.ultimateTimer = 0;
// Start cooldown (if you want to add one)
} else {
// Automatically shoot at the nearest enemy
if (enemies.length > 0) {
const nearestEnemy = enemies.reduce((closest, enemy) => {
const dx = enemy.x - player.x;
const dy = enemy.y - player.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < closest.dist) {
return { enemy, dist };
} else {
return closest;
}
}, { enemy: null, dist: Infinity });
if (nearestEnemy.enemy) {
const angle = Math.atan2(nearestEnemy.enemy.y - player.y, nearestEnemy.enemy.x - player.x);
shootProjectile(null, null, angle);
}
}
// Ensure health and stamina are maxed out
player.health = player.maxHealth;
player.stamina = player.maxStamina;
}
}
// If you want to reset the ultimate after some cooldown, you can implement it here
}
// Draw function
let lavaWaveOffset = 0;
function draw() {
// Clear canvas with bloom effect
ctx.fillStyle = 'rgba(34,34,34,0.2)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
# Tri-Force Orbit
[](LICENSE)
[](https://github.com/yssf8/tri-force-orbit/commits/main)
**Tri-Force Orbit** is an action-packed browser game where players navigate a dynamic environment, battle enemies, and utilize unique abilities to achieve high scores. Hosted on GitHub Pages, Tri-Force Orbit offers engaging gameplay with responsive controls and stunning visuals.
[🎮 Play the game now!](https://yssf8.github.io/tri-force-orbit)
---
## Table of Contents
- [Features](#features)
- [Gameplay](#gameplay)
- [Controls](#controls)
- [Technologies](#technologies)
- [Installation](#installation)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)
---
## Features
- **Responsive Controls:** Smooth left/right movement, grappling, shooting, and a powerful ultimate ability.
- **Dynamic Environments:** Experience a vibrant world with dynamic elements like lava waves and interactive grapple points.
- **Enemy AI:** Challenging enemies that actively seek out and attack the player.
- **Health & Stamina Systems:** Manage your health and stamina strategically to survive and thrive.
- **Particle Effects:** Immersive particle effects enhance the visual experience.
- **Scoring System:** Track your progress and aim for high scores.
- **Ultimate Ability:** Activate a game-changing ultimate ability to turn the tide in your favor.
## Gameplay
In **Tri-Force Orbit**, you control a nimble character navigating through a dynamic environment filled with enemies and obstacles. Utilize your movement capabilities, grappling hooks, projectiles, and ultimate abilities to defeat foes and rack up points.
### Controls
- **Move Left:** Press `A` or `Arrow Left`
- **Move Right:** Press `D` or `Arrow Right`
- **Shoot Projectile:** Left-click the mouse
- **Grapple:** Right-click on a grapple point
- **Activate Ultimate:** Middle-click the mouse
- **Toggle Music:** Click the music button on the interface
**Additional Mechanics:**
- **Grappling:** Use stamina to grapple onto designated squares, pulling yourself towards them.
- **Shooting:** Fire projectiles to eliminate enemies; limited by a cooldown period.
- **Ultimate Ability:** Once ready, activate your ultimate to max health and stamina, and automatically target enemies.
## Technologies
Tri-Force Orbit is built using the following technologies:
- **HTML5:** Markup structure and canvas rendering.
- **CSS3:** Styling and responsive design.
- **JavaScript:** Game logic, interactivity, and animations.
- **GitHub Pages:** Hosting the game online for easy access.
## Installation
To run **Tri-Force Orbit** locally on your machine:
1. **Clone the repository:**
```bash
git clone https://github.com/yssf8/tri-force-orbit.git
Navigate to the project directory:
cd tri-force-orbit
Open index.html in your preferred web browser:
You can simply double-click the index.html file, or
Use a local server for better performance (optional):
---
### Additional Notes:
1. **Screenshots/GIFs:**
- To enhance the README, consider adding screenshots or GIFs of the game in action. You can add them using Markdown syntax:
```markdown

```
- Make sure to include the actual images in your repository under an appropriate directory like `./assets/screenshots/`.
2. **LICENSE File:**
- Ensure you have a `LICENSE` file in your repository if you’re referencing it. The MIT License is commonly used for open-source projects.
- You can create a `LICENSE` file with the following content:
```markdown
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
[Full MIT License Text Here]
```
3. **Badges:**
- You can add more badges to showcase aspects like build status, issues, pull requests, etc., using [shields.io](https://shields.io/).
4. **Customization:**
- Feel free to customize the sections further based on your game's specifics. For example, if there are power-ups, levels, or unique mechanics, add sections to explain them.
5. **Contact Information:**
- You might want to add a section for contact information or ways to reach out for feedback and support.
Here’s how you can include a screenshot section as an example:
```markdown
## Screenshots


Remember to replace the paths with the actual paths to your images.
By following this structure, your README.md will provide a comprehensive overview of Tri-Force Orbit, enticing visitors to play the game and contribute to its development.