user iconOzecon on March 3rd
33 views

topic iconim taking a break from devopment from space huggers

heres a code for u guys.
bye
// Simple run-and-gun style demo for OpenProcessing (p5.js)
// Original character, not based on Cuphead assets.

let player;
let bullets = [];
let enemies = [];
let groundY;
let scrollX = 0;
let spawnTimer = 0;

function setup() {

 createCanvas(800, 450);
 groundY = height - 60;
 player = new Player(100, groundY);

}

function draw() {

 background(180, 220, 255);
 // Parallax background
 push();
 translate(-scrollX * 0.3, 0);
 drawBackground();
 pop();
 // Ground
 fill(60, 180, 80);
 rect(0, groundY, width, height - groundY);
 // Update scroll
 scrollX += 2;
 // Player
 player.update();
 player.show();
 // Bullets
 for (let i = bullets.length - 1; i >= 0; i--) {
   bullets[i].update();
   bullets[i].show();
   if (bullets[i].offscreen()) {
     bullets.splice(i, 1);
   }
 }
 // Enemies
 spawnTimer--;
 if (spawnTimer <= 0) {
   enemies.push(new Enemy(scrollX + width + random(50, 200), groundY));
   spawnTimer = int(random(60, 120));
 }
 for (let i = enemies.length - 1; i >= 0; i--) {
   enemies[i].update();
   enemies[i].show();
   // Bullet–enemy collision
   for (let j = bullets.length - 1; j >= 0; j--) {
     if (enemies[i] && bullets[j] && enemies[i].hits(bullets[j])) {
       enemies.splice(i, 1);
       bullets.splice(j, 1);
       break;
     }
   }
   // Remove enemies that passed
   if (enemies[i] && enemies[i].x < scrollX - 100) {
     enemies.splice(i, 1);
   }
 }
 // HUD
 fill(0);
 textSize(16);
 text("Left/Right: Move   Up: Jump   Z: Shoot", 20, 30);

}

function keyPressed() {

 if (keyCode === LEFT_ARROW) {
   player.move(-1);
 } else if (keyCode === RIGHT_ARROW) {
   player.move(1);
 } else if (keyCode === UP_ARROW) {
   player.jump();
 } else if (key === 'z' || key === 'Z') {
   bullets.push(new Bullet(player.x + 25, player.y - 20, player.dir));
 }

}

function keyReleased() {

 if (keyCode === LEFT_ARROW || keyCode === RIGHT_ARROW) {
   player.move(0);
 }

}

// Player class (cartoony hero)
class Player {

 constructor(x, groundY) {
   this.x = x;
   this.y = groundY;
   this.groundY = groundY;
   this.vy = 0;
   this.gravity = 0.8;
   this.jumpForce = -15;
   this.onGround = true;
   this.speed = 5;
   this.dir = 1; // 1 = right, -1 = left
   this.moveDir = 0;
 }
 move(d) {
   this.moveDir = d;
   if (d !== 0) this.dir = d;
 }
 jump() {
   if (this.onGround) {
     this.vy = this.jumpForce;
     this.onGround = false;
   }
 }
 update() {
   // Horizontal movement
   this.x += this.moveDir * this.speed;
   // Keep player roughly centered in world
   this.x = constrain(this.x, 50, width - 50);
   // Gravity
   this.vy += this.gravity;
   this.y += this.vy;
   if (this.y > this.groundY) {
     this.y = this.groundY;
     this.vy = 0;
     this.onGround = true;
   }
 }
 show() {
   push();
   translate(this.x, this.y);
   scale(this.dir, 1);
   // Body
   noStroke();
   fill(40);
   rect(-15, -50, 30, 40, 8);
   // Head
   fill(255, 230, 200);
   ellipse(0, -70, 35, 35);
   // Eyes
   fill(0);
   ellipse(-5, -72, 5, 8);
   ellipse(5, -72, 5, 8);
   // Mouth
   noFill();
   stroke(0);
   strokeWeight(2);
   arc(0, -63, 15, 10, 0, PI);
   // Arms
   stroke(40);
   strokeWeight(5);
   line(-15, -40, -30, -25);
   line(15, -40, 30, -25);
   // Legs
   line(-8, -10, -8, 5);
   line(8, -10, 8, 5);
   // Shoes
   noStroke();
   fill(200, 0, 0);
   ellipse(-8, 8, 14, 8);
   ellipse(8, 8, 14, 8);
   pop();
 }

}

// Bullet class
class Bullet {

 constructor(x, y, dir) {
   this.x = x;
   this.y = y;
   this.r = 6;
   this.speed = 10 * dir;
 }
 update() {
   this.x += this.speed;
 }
 show() {
   push();
   noStroke();
   fill(255, 240, 120);
   ellipse(this.x, this.y, this.r * 2);
   pop();
 }
 offscreen() {
   return this.x < 0 || this.x > width;
 }

}

// Enemy class
class Enemy {

 constructor(worldX, groundY) {
   this.worldX = worldX;
   this.groundY = groundY;
   this.x = worldX;
   this.y = groundY;
   this.r = 20;
 }
 update() {
   // Convert worldX to screen x using scrollX
   this.x = this.worldX - scrollX;
 }
 show() {
   push();
   translate(this.x, this.y);
   // Simple cartoon enemy
   noStroke();
   fill(150, 50, 50);
   ellipse(0, -20, this.r * 2, this.r * 2);
   fill(255);
   ellipse(-5, -22, 8, 10);
   ellipse(5, -22, 8, 10);
   fill(0);
   ellipse(-5, -22, 4, 6);
   ellipse(5, -22, 4, 6);
   fill(0);
   rect(-10, -10, 20, 5, 2);
   pop();
 }
 hits(bullet) {
   let d = dist(this.x, this.y - 20, bullet.x, bullet.y);
   return d < this.r + bullet.r;
 }

}

function drawBackground() {

 // Simple layered hills and clouds
 noStroke();
 // Far hills
 fill(120, 200, 140);
 ellipse(200, groundY + 40, 400, 200);
 ellipse(600, groundY + 60, 500, 250);
 // Clouds
 fill(255);
 ellipse(150, 100, 80, 50);
 ellipse(180, 90, 60, 40);
 ellipse(120, 90, 60, 40);
 ellipse(450, 70, 90, 55);
 ellipse(480, 60, 60, 40);
 ellipse(420, 60, 60, 40);

}
go to openprocessing and copy it lol
bye😁

user icon