xxxxxxxxxx
330
// I AJKSDHKJASH ok so this is really more javascript iwth some p5play input in it cuz i am more cozy with javascript and was running auto pilot coding this
// CREDITS: Coding = kane (lost my shit with sm errors left and right but i made it thanks reddit and google and the code coaches for catching my errors) , sprites = sky, music and soundtrack = rebecca (ITS SO GOOD AND ALL MADE BY HER)
class Sprite {
constructor(x, y, width, height) {
this.position = createVector(x, y);
this.velocity = createVector(0, 0);
this.width = width;
this.height = height;
this.maxHealth = 500;
this.health = this.maxHealth;
this.moveLeftKey = '';
this.moveRightKey = '';
this.punchKey = '';
this.kickKey = '';
this.jumpKey = '';
}
addImage(image) {
this.image = image;
}
setCollider(type, offsetX, offsetY, width, height) {
}
collide(sprite) {
// overlap collision sigh
if (
this.position.x < sprite.position.x + sprite.width &&
this.position.x + this.width > sprite.position.x &&
this.position.y < sprite.position.y + sprite.height &&
this.position.y + this.height > sprite.position.y
) {
return true; // Colliding
} else {
return false; // Not colliding
}
}
// p5 play moving sprite system acc may make me cry so heres the pj5 one in general cuz GOD
moveLeft(speed) {
this.velocity.x = -20;
}
moveRight(speed) {
this.velocity.x = 20;
}
stopMoving() {
this.velocity.x = 0;
}
}
let player;
let enemy;
let playerSprite;
let enemySprite;
let backgroundImage;
let playerPunchSprite;
let playerKickSprite;
let enemyPunchSprite;
let enemyKickSprite;
let gameState = "title"; // initial game state
let titleScreenImage;
let gameStarted = false;
//i am so aware i couldve used p5play change animations but hahah auto pilot java was on
let playerOriginalSprite;
let enemyOriginalSprite;
// music
let punchSound;
let kickSound;
let bgMusic;
function preload() { // upload files to folder and put names in
//character sprites
playerSprite = loadImage('player.gif');
enemySprite = loadImage('enemy.gif');
playerOrignalSprite = loadImage('player.png');
enemyOrignalSprite = loadImage('enemy.png');
backgroundImage = loadImage('bg.gif');
playerPunchSprite = loadImage('punch player.png');
playerKickSprite = loadImage('kick player.png');
enemyPunchSprite = loadImage('punch enemy.png');
enemyKickSprite = loadImage('kick enemy.png');
// start screen
titleScreenImage = loadImage('title screen.png');
// sound files
punchSound = loadSound('punch.wav');
kickSound = loadSound('kick.wav');
bgMusic = loadSound('BONK.wav');
}
function setup() {
createCanvas(1000, 500);
textFont("Arial");
textSize(16);
textAlign(CENTER);
// Create player and enemy sprites
player = new Sprite(100, 220, 100, 100);
player.addImage(playerSprite);
playerSprite.resize(100, 200);
playerPunchSprite.resize(120, 200);
playerKickSprite.resize(120, 200);
enemy = new Sprite(800, 220, 100, 100);
enemy.addImage(enemySprite);
enemySprite.resize(100, 200);
enemyPunchSprite.resize(120, 200);
enemyKickSprite.resize(120, 200);
playerOriginalSprite = playerSprite;
enemyOriginalSprite = enemySprite;
// sfx sprites
punchSoundSprite = new Sprite(-100, -100);
punchSoundSprite.sound = punchSound;
kickSoundSprite = new Sprite(-100, -100);
kickSoundSprite.sound = kickSound;
// Play background music
bgMusic.loop();
// keys yuh
player.moveLeftKey = 'a';
player.moveRightKey = 'd';
player.punchKey = 'e';
player.kickKey = 's';
player.jumpKey = 'w';
enemy.moveLeftKey = 'j';
enemy.moveRightKey = 'l';
enemy.punchKey = 'o';
enemy.kickKey = 'k';
enemy.jumpKey = 'i';
// basic ass colliders cuz im on my wits end rn
player.setCollider("rectangle", 0, 0, 100, 100);
enemy.setCollider("rectangle", 0, 0, 100, 100);
}
function draw() {
if (gameState === "title") {
// Draw title screen
image(titleScreenImage, 0, 0, width, height);
} else if (gameState === "playing") {
// Draw background image
image(backgroundImage, 0, 0, width, height);
// player movement
if (keyIsDown(65)) { // 'a' key
player.velocity.x = -5;
} else if (keyIsDown(68)) { // 'd' key
player.velocity.x = 5;
} else {
player.velocity.x = 0;
}
// enemy movement
if (keyIsDown(74)) { // 'j' key
enemy.velocity.x = -5;
} else if (keyIsDown(76)) { // 'l' key
enemy.velocity.x = 5;
} else {
enemy.velocity.x = 0;
}
// player's actions
if (keyIsDown(69)) { // 'e' key for punch
punch(player, enemy);
} else if (keyIsDown(83)) { // 's' key for kick
kick(player, enemy);
}
// enemy's actions
if (keyIsDown(79)) { // 'o' key for enemy's punch
punch(enemy, player);
} else if (keyIsDown(75)) { // 'k' key for enemy's kick
kick(enemy, player);
}
// Draw player
image(playerSprite, player.position.x, player.position.y);
// Draw enemy
image(enemySprite, enemy.position.x, enemy.position.y);
// Update player and enemy position based on velocities IT GIBSLLT MOVED EAHHSHSHDH
player.position.add(player.velocity);
enemy.position.add(enemy.velocity);
// Display health bars
drawHealthBar(10, 10, player.health, player.maxHealth); // Player's health bar on the left
drawHealthBar(width - 110, 10, enemy.health, enemy.maxHealth); // Enemy's health bar on the right
// Check for game over condition
if (player.health <= 0 || enemy.health <= 0) {
gameState = "game over";
}
} else if (gameState === "game over") {
background(0);
textSize(32);
fill(255);
text("Game Over!", width / 2, height / 2 - 30);
if (player.health <= 0) {
text("Player 2 Wins!", width / 2, height / 2 + 10);
} else {
text("Player Wins!", width / 2, height / 2 + 10);
}
}
}
function mousePressed() {
if (!gameStarted && gameState === "title") {
// Check if the mouse click occurs within the canvas
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
// Start the game
gameStarted = true;
gameState = "playing";
}
}
}
// HEALTH BAR DRAWWW
function drawHealthBar(x, y, health, maxHealth) {
let barWidth = 100;
let barHeight = 10;
let healthRatio = health / maxHealth;
let barColor = color(0, 255, 0); // Green color for max/high health bar
if (healthRatio < 0.3) {
barColor = color(255, 0, 0); // Red color for low health
}
noStroke();
fill(150);
rect(x, y, barWidth, barHeight);
fill(barColor);
rect(x, y, barWidth * healthRatio, barHeight);
}
//kick n punch statements player
function keyPressed() {
// Player actions
if (key === player.punchKey) {
punch(player, enemy);
} else if (key === player.kickKey) {
kick(player, enemy);
} else if (key === player.jumpKey) {
jump(player);
}
// Enemy kick n punch jazz
if (key === enemy.punchKey) {
punch(enemy, player);
} else if (key === enemy.kickKey) {
kick(enemy, player);
} else if (key === enemy.jumpKey) {
jump(enemy);
}
}
// PUNCH input
function punch(attacker, target) {
if (attacker.collide(target)) {
let damage = random(5, 10); // Random damage between 5 and 10
target.health -= damage;
punchSoundSprite.sound.play(); // punch sfx
}
// Replace player sprite with punch sprite
if (attacker === player) {
playerSprite = playerPunchSprite;
setTimeout(() => {
playerSprite = playerOriginalSprite;
}, 1000); // Revert back to original sprite after 1000 milliseconds (1 second) why is it in milliseconds but remember that
} else {
enemySprite = enemyPunchSprite;
setTimeout(() => {
enemySprite = enemyOriginalSprite;
}, 1000); // Revert back to original sprite after 1000 milliseconds (1 second)
}
}
// KICK input
function kick(attacker, target) {
if (attacker.collide(target)) {
let damage = random(10, 15); // Random damage between 10 and 15
target.health -= damage;
kickSoundSprite.sound.play(); // kick sfx
}
// Replace player sprite with kick sprite
if (attacker === player) {
playerSprite = playerKickSprite;
setTimeout(() => {
playerSprite = playerOriginalSprite;
}, 1000); // Revert back to original sprite after 1000 milliseconds (1 second)
} else {
enemySprite = enemyKickSprite;
setTimeout(() => {
enemySprite = enemyOriginalSprite;
}, 1000); // Revert back to original sprite after 1000 milliseconds (1 second)
}
}
// JUMP input (why was this the easiest one to do like the movement OF LEFT N RIGHT genuinely drove me CRAZY)
function jump(character) {
console.log(`${character.name} jumps.`);
character.position.y -= 200; // Move the character up
setTimeout(function() {
character.position.y += 200; // Move the character down after
}, 500); // delay amount in miliseconds lessen/higher it if needed to be smoother
}