Please make a browser based pvp fighting game like Street Fighter or Tekken.
I want you to generate a highly detailed and actionable game, it should have atleast 2 different characters to choose from.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a complete, self-contained example of a browser‐based PvP fighting game using HTML5 Canvas and JavaScript. In this example you’ll find:
A character selection screen where each player chooses one of two fighters (“Samurai” and “Ninja”).
A game loop that handles movement, jumping, attacking, and collision detection.
Two fighters with different colors and simple attributes.
Keyboard controls for two players (Player 1 uses WASD keys; Player 2 uses the arrow keys).
Health bars that decrease when an opponent’s attack lands, and an end‐of‐game message when one fighter’s health reaches zero.
You can copy the code into a new HTML file (for example, “index.html”) and open it in your browser to play.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Browser-Based PvP Fighting Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #222;
color: #fff;
font-family: Arial, sans-serif;
}
#gameCanvas {
background: #444;
display: block;
margin: 0 auto;
border: 2px solid #fff;
}
/* Character selection overlay */
#characterSelection {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.9);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 10;
}
.selector {
margin: 20px;
}
.selector h2 {
margin-bottom: 10px;
}
.character-options {
display: flex;
gap: 20px;
}
.character-option {
padding: 10px 20px;
border: 2px solid #fff;
cursor: pointer;
transition: background 0.3s;
}
.character-option:hover {
background: #666;
}
#message {
position: absolute;
top: 20px;
width: 100%;
text-align: center;
font-size: 24px;
z-index: 5;
}
</style>
</head>
<body>
<!-- UI overlay for game messages -->
<div id="message"></div>
<!-- Canvas -->
<canvas id="gameCanvas" width="800" height="500"></canvas>
<!-- Character Selection Screen -->
<div id="characterSelection">
<div class="selector" id="player1Select">
<h2>Player 1: Choose Your Fighter</h2>
<div class="character-options">
<div class="character-option" data-character="samurai">Samurai</div>
<div class="character-option" data-character="ninja">Ninja</div>
</div>
</div>
<div class="selector" id="player2Select">
<h2>Player 2: Choose Your Fighter</h2>
<div class="character-options">
<div class="character-option" data-character="ninja">Ninja</div>
<div class="character-option" data-character="samurai">Samurai</div>
</div>
</div>
<button id="startGame" style="margin-top: 30px; padding:10px 20px; font-size:18px; display:none;">Start Game</button>
</div>
<script>
// --- Global Variables ---
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const messageDiv = document.getElementById("message");
const selectionDiv = document.getElementById("characterSelection");
const startGameBtn = document.getElementById("startGame");
// To store player selection. Default selections:
let playerSelections = {
player1: null,
player2: null
};
// --- Character Definitions ---
// Each fighter type has its own attributes such as color, speed, attack damage and size.
const fighterTypes = {
samurai: {
name: "Samurai",
color: "crimson",
width: 50,
height: 80,
speed: 4,
jumpVelocity: -12,
attackDamage: 20,
attackRange: 50
},
ninja: {
name: "Ninja",
color: "deepskyblue",
width: 40,
height: 70,
speed: 5,
jumpVelocity: -14,
attackDamage: 15,
attackRange: 40
}
};
// --- Fighter Class ---
class Fighter {
constructor(x, y, type, controls) {
this.x = x;
this.y = y; // y is the top of the fighter
this.spawnY = y; // ground level for jump reset
this.type = type;
this.width = type.width;
this.height = type.height;
this.color = type.color;
this.speed = type.speed;
this.jumpVelocity = type.jumpVelocity;
this.attackDamage = type.attackDamage;
this.attackRange = type.attackRange;
this.health = 100;
this.maxHealth = 100;
this.velocityY = 0;
this.onGround = true;
this.isAttacking = false;
this.attackTimer = 0;
this.direction = 1; // 1 means facing right, -1 means facing left
this.controls = controls; // keyboard mapping for this fighter
}
// Draw fighter and optional attack box
draw() {
// Draw fighter body
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
// If attacking, draw the attack hitbox (a translucent rectangle)
if (this.isAttacking) {
ctx.fillStyle = "rgba(255, 255, 0, 0.5)";
// Attack box appears in the direction the fighter faces
let attackX;
if (this.direction === 1) {
attackX = this.x + this.width;
} else {
attackX = this.x - this.attackRange;
}
ctx.fillRect(attackX, this.y + 10, this.attackRange, this.height - 20);
}
// Draw health bar above the fighter
ctx.fillStyle = "white";
ctx.fillRect(this.x, this.y - 15, this.width, 8);
ctx.fillStyle = "green";
let healthWidth = (this.health / this.maxHealth) * this.width;
ctx.fillRect(this.x, this.y - 15, healthWidth, 8);
}
// Update physics and cooldowns
update() {
// Gravity
if (!this.onGround) {
this.velocityY += 0.5; // gravity acceleration
this.y += this.velocityY;
if (this.y >= this.spawnY) {
this.y = this.spawnY;
this.velocityY = 0;
this.onGround = true;
}
}
// Handle attack timer (attack lasts for a brief period)
if (this.isAttacking) {
this.attackTimer--;
if (this.attackTimer <= 0) {
this.isAttacking = false;
}
}
}
// Initiate an attack action
attack() {
if (!this.isAttacking) {
this.isAttacking = true;
this.attackTimer = 15; // frames duration for attack
}
}
}
// --- Game State ---
let player1, player2;
let keys = {}; // currently pressed keys
// --- Keyboard Input Handling ---
window.addEventListener("keydown", (e) => {
keys[e.key.toLowerCase()] = true;
});
window.addEventListener("keyup", (e) => {
keys[e.key.toLowerCase()] = false;
});
// --- Collision Detection ---
// Check if fighter1's attack hitbox collides with fighter2
function checkAttackCollision(attacker, defender) {
if (!attacker.isAttacking) return false;
// Compute attack hitbox based on attacker
let attackX = attacker.direction === 1 ? attacker.x + attacker.width : attacker.x - attacker.attackRange;
let attackY = attacker.y + 10;
let attackWidth = attacker.attackRange;
let attackHeight = attacker.height - 20;
// Axis-Aligned Bounding Box (AABB) collision detection
if (
attackX < defender.x + defender.width &&
attackX + attackWidth > defender.x &&
attackY < defender.y + defender.height &&
attackY + attackHeight > defender.y
) {
return true;
}
return false;
}
// --- Update and Render Loop ---
function gameLoop() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background (simple ground)
ctx.fillStyle = "#333";
ctx.fillRect(0, canvas.height - 50, canvas.width, 50);
// --- Process input for Player 1 (WASD / S for attack) ---
if (player1.health > 0) {
// Move left
if (keys["a"]) {
player1.x -= player1.speed;
player1.direction = -1;
}
// Move right
if (keys["d"]) {
player1.x += player1.speed;
player1.direction = 1;
}
// Jump (W)
if (keys["w"] && player1.onGround) {
player1.velocityY = player1.jumpVelocity;
player1.onGround = false;
}
// Attack (S)
if (keys["s"]) {
player1.attack();
}
}
// --- Process input for Player 2 (Arrow keys) ---
if (player2.health > 0) {
// Move left
if (keys["arrowleft"]) {
player2.x -= player2.speed;
player2.direction = -1;
}
// Move right
if (keys["arrowright"]) {
player2.x += player2.speed;
player2.direction = 1;
}
// Jump (ArrowUp)
if (keys["arrowup"] && player2.onGround) {
player2.velocityY = player2.jumpVelocity;
player2.onGround = false;
}
// Attack (ArrowDown)
if (keys["arrowdown"]) {
player2.attack();
}
}
// Constrain fighters within canvas horizontal boundaries
[player1, player2].forEach((fighter) => {
if (fighter.x < 0) fighter.x = 0;
if (fighter.x + fighter.width > canvas.width)
fighter.x = canvas.width - fighter.width;
});
// Update fighters (handle gravity and cooldowns)
player1.update();
player2.update();
// Check for collisions and update health
if (checkAttackCollision(player1, player2)) {
player2.health -= player1.attackDamage;
// Prevent multiple hits per single key press by resetting attack
player1.isAttacking = false;
}
if (checkAttackCollision(player2, player1)) {
player1.health -= player2.attackDamage;
player2.isAttacking = false;
}
// Draw fighters
player1.draw();
player2.draw();
// Check for game over
if (player1.health <= 0 || player2.health <= 0) {
// Display winner message
let winner = player1.health <= 0 ? "Player 2 Wins!" : "Player 1 Wins!";
messageDiv.textContent = winner;
return; // stop the game loop
}
// Continue the loop
requestAnimationFrame(gameLoop);
}
// --- Character Selection Handling ---
const p1Options = document.querySelectorAll("#player1Select .character-option");
const p2Options = document.querySelectorAll("#player2Select .character-option");
// Highlight selected option on click
p1Options.forEach(option => {
option.addEventListener("click", function() {
// Clear previous selection
p1Options.forEach(opt => opt.style.backgroundColor = "");
// Mark as selected
this.style.backgroundColor = "#888";
playerSelections.player1 = this.getAttribute("data-character");
checkSelections();
});
});
p2Options.forEach(option => {
option.addEventListener("click", function() {
p2Options.forEach(opt => opt.style.backgroundColor = "");
this.style.backgroundColor = "#888";
playerSelections.player2 = this.getAttribute("data-character");
checkSelections();
});
});
// Show start button when both players have selected a character
function checkSelections() {
if (playerSelections.player1 && playerSelections.player2) {
startGameBtn.style.display = "block";
}
}
startGameBtn.addEventListener("click", startGame);
// --- Start Game Function ---
function startGame() {
// Hide the character selection overlay
selectionDiv.style.display = "none";
messageDiv.textContent = "";
// Create fighters with starting positions.
// Player 1 spawns on the left, Player 2 on the right.
let p1Type = fighterTypes[playerSelections.player1];
let p2Type = fighterTypes[playerSelections.player2];
// Set up control schemes:
// Player 1: W (jump), A (left), D (right), S (attack)
// Player 2: ArrowUp (jump), ArrowLeft (left), ArrowRight (right), ArrowDown (attack)
player1 = new Fighter(100, canvas.height - 50 - p1Type.height, p1Type, {
jump: "w", left: "a", right: "d", attack: "s"
});
player2 = new Fighter(canvas.width - 150, canvas.height - 50 - p2Type.height, p2Type, {
jump: "ArrowUp", left: "ArrowLeft", right: "ArrowRight", attack: "ArrowDown"
});
// Start the game loop
requestAnimationFrame(gameLoop);
}
</script>
</body>
</html>
How It Works
Character Selection:
When the page loads, an overlay lets each player choose from two fighters (“Samurai” and “Ninja”). Their selection is recorded (with a simple highlight) and the “Start Game” button appears once both players have chosen a fighter.
Fighter Class:
The Fighter class defines properties (position, size, speed, health, etc.) and methods such as draw(), update(), and attack(). During an attack, a temporary “attack hitbox” is drawn in front of the fighter.
Game Loop:
The gameLoop() function uses requestAnimationFrame for smooth animations. It first clears the canvas, then processes inputs and applies physics (gravity for jumping). It then checks if either fighter’s attack hitbox collides with the opponent. If a collision is detected, the opponent loses health.
User Controls:
Player 1 (left side):
A: Move left
D: Move right
W: Jump
S: Attack
Player 2 (right side):
Left Arrow: Move left
Right Arrow: Move right
Up Arrow: Jump
Down Arrow: Attack
Game Over:
When one fighter’s health reaches zero, a winning message is shown and the game loop stops.
Next Steps
This code serves as a foundation. You might expand it by:
Adding more animations and sprite artwork.
Creating more sophisticated collision detection and attack types (such as combos or special moves).
Implementing online multiplayer (using WebSockets, for example).
Including sound effects and background music.
Enjoy experimenting with and extending your browser-based PvP fighting game!