xxxxxxxxxx
242
// learned from Xin Xin and Daniel Shiffman and p5.play
// all images from shutterstock
// you are on river Acheron, try to pass the bad ghosts on the river and reach Hell
let cars = []; // empty array
let frog1;
let carsNum = 10;
let sceneNum = 0;
let walls = [];
let wallsNum = 5;
var wallGroup;
var blockGroup;
var ghost;
var coins;
var score = 0;
var clouds;
var player;
let t = 0; // time variable
let bubbles = [];
function preload() {
ghost = loadImage("ghost2.png");
badGhosts = loadImage("bad_ghost.png");
soul = loadImage("soul.png");
}
function setup() {
createCanvas(600, 400);
player = createSprite(300, 390, 20, 20);
player.shapeColor = color(255);
for (let i = 0; i < 20; i++) { //numbers of objects inside the array
// let x = 20 + 100 * i; //initial location + spacing the objects
let x = random(width);
let y = random(height);
let r = random(5, 30);
bubbles[i] = new Bubble(x, y, r);
}
wallGroup = new Group();
blockGroup = new Group();
clouds = new Group();
// collect souls
coins = new Group();
for (var i = 0; i < 20; i++) {
var c = createSprite(
random(100, width - 50),
random(100, height - 50),
10, 10);
c.shapeColor = color(0, 255, 255);
c.addImage(soul);
coins.add(c);
}
for (var i = 0; i < 10; i++) {
var u = createSprite(
random(width), random(height),
random(25, 100), random(25, 100));
u.shapeColor = color(random(200, 255));
clouds.add(u);
u.addImage(badGhosts);
}
}
function draw() {
// waves learned from Aatish Bhatia at p5 examples
background(0, 32, 128, 15); // translucent background (creates trails)
noStroke();
fill(77, 121, 255, 40);
// make a x and y grid of ellipses
for (let x = 0; x <= width; x = x + 30) {
for (let y = 0; y <= height; y = y + 30) {
// starting point of each circle depends on mouse position
const xAngle = map(mouseX, 0, width, -4 * PI, 4 * PI, true);
const yAngle = map(mouseY, 0, height, -4 * PI, 4 * PI, true);
// and also varies based on the particle's location
const angle = xAngle * (x / width) + yAngle * (y / height);
// each particle moves in a circle
const myX = x + 20 * cos(2 * PI * t + angle);
const myY = y + 20 * sin(2 * PI * t + angle);
ellipse(myX, myY, 10); // draw particle
}
}
t = t + 0.01; // update time
// ghost moving
player.velocity.x = 0;
player.velocity.y = 0;
if (keyIsDown(37))
player.velocity.x = -2;
if (keyIsDown(39))
player.velocity.x = 2;
if (keyIsDown(38))
player.velocity.y = -2;
if (keyIsDown(40))
player.velocity.y = 2;
player.collide(clouds);
player.overlap(coins, getCoin);
player.addImage(ghost, player.x, player.y, player.size, player.size);
for (var i = 0; i < clouds.length; i++) {
// clouds[i].position.x += clouds[i].width * 0.01;
clouds[i].position.y += clouds[i].height * 0.01;
if (clouds[i].position.y > height) {
clouds[i].position.y = 0;
}
}
blockGroup.collide(wallGroup);
switch (sceneNum) {
case 0:
scene0();
break; // stop right here & exit
case 1:
scene1();
break;
case 2:
scene2();
break;
}
for (let i = 0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].show();
}
if (player.collide(clouds)){
this.player.remove();
player = createSprite(300, 390, 20, 20);
player.shapeColor = color(255);
textSize(50);
textAlign(CENTER);
fill(255);
text("oh no!", width/2, height/2);
// text(seconds);
sceneNum = 0;
}
// how to switch ????????????????
if(player.y < 0){
sceneNum++;
player.x = 300;
player.y = 390;
}
if(sceneNum > 2){
sceneNum = 0;
}
drawSprites();
}
function scene0() {
// background(50);
console.log('scene 0');
}
function scene1() {
// background(150);
console.log('scene 1');
}
function scene2() {
// background(0);
console.log('scene 2');
}
class Bubble {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
}
move() {
this.x = this.x + random(-3, 3);
this.y = this.y + random(-3, 3);
}
show() {
stroke(255, 70);
strokeWeight(0.3);
noFill();
ellipse(this.x, this.y, this.r * 2);
}
}
function getCoin(player, coin) {
coin.remove();
score += 1;
console.log(score);
}