xxxxxxxxxx
583
let dir;
let h, w;
let things = [];
let predators = [];
let droplets = [];
let speed = 5;
let overallSpeed;
let score;
let gamestarted, gameover;
let btn = "";
let lastB;
let n = 6;
let gbl = 17;
let u;
let bgcolor;
let loupyestu;
let img = [];
let items = [];
let loveMeter;
let points = [];
let notes, musicDisabled, lastToggled;
let m, b, coeur;
let moutons = [];
let genders = [];
let edouard, sPose, rejane, hotchoc, title, loup;
function preload() {
hotchoc = loadSound("Untitled.mp3");
m = loadImage("images/brun.png");
b = loadImage("images/blanc.png");
loup = loadImage("images/loup.png");
coeur = loadImage("images/coeur.png");
edouard = new Sprite(loadImage("images/avatar-ed.png"), 0);
rejane = new Sprite(loadImage("images/avatar-rere.png"), 1);
raisin = loadImage("images/raisin.png");
notes = loadImage("images/notes.png");
title = loadImage("images/titre.png");
}
function setup() {
gamestarted = false;
gameInitialized = false;
loupyestu = false;
musicDisabled = true;
bgcolor = "#E7F8E3";
pixelDensity(1);
h = windowHeight;
w = windowWidth;
createCanvas(windowWidth, windowHeight);
dir = 1;
gameover = false;
score = 0;
lastB = millis();
lastToggled = millis();
overallSpeed = 0;
items = [1, 2, 3, 4, 5, 6, 7];
points = [5, 10, 5, 20, 20, 50, -50];
loveMeter = 100;
sPose = 0;
noSmooth();
genders = [0, 0, 0, 0, 0, 0];
while (genders.slice(1).reduce((a, b) => a + b, 0)<3) {
genders[floor(random(6))]=1;
}
for (i = 1; i < n; i++) {
moutons[i] = new Mouton(i);
}
edouard.x = 130;
edouard.y = height - 70;
bullets = [];
}
function cleanMess() {
for (let i = bullets.length - 1; i >= 0; i--) {
if (dist(bullets[i].x, bullets[i].y, bullets[i].tx, bullets[i].ty) < 5)
bullets.splice(i, 1);
}
}
function initializeGame() {
for (let i = 0; i < 2; i++) {
things[i] = new Thing();
}
gameInitialized = true;
}
function draw() {
background(bgcolor);
h = windowHeight;
w = windowWidth;
u = width / 8;
imageMode(CENTER);
//buttons
strokeWeight(5);
stroke("#48556E");
ellipseMode(CENTER);
fill(btn == "right" ? "#F00" : "#6488D3");
ellipse(width - u, height - 2 * u, u, u);
fill(btn == "up" ? "#F00" : "#6488D3");
ellipse(width - 2 * u, height - 3 * u, u, u);
fill(btn == "left" ? "#F00" : "#6488D3");
ellipse(width - 3 * u, height - 2 * u, u, u);
fill(btn == "down" ? "#F00" : "#6488D3");
ellipse(width - 2 * u, height - u, u, u);
//loveMeter
rectMode(CORNER);
for (let i = 0; i < 5; i++) {
imageMode(CORNER);
image(coeur, 5 + 5 * (i % 2), height - 40 - i * 20, 20, 20);
}
noStroke();
fill(bgcolor);
rect(5, height - 120, 25, 100 - loveMeter);
if (loveMeter < 50 && gamestarted && frameCount % 30 < 15)
rect(5, height - 120, 25, 100);
herbe();
btn = "";
if (touches.length) {
u = width / 8;
let d = u;
if (
loupyestu &&
bullets.length < 10 &&
touches[0].y < (3 * height) / 4 &&
!(touches[0].x < 30 && touches[0].y < 30) &&
millis() - lastToggled > 200
) {
bullets[bullets.length] = new Bullet(touches[0].x, touches[0].y);
navigator.vibrate([50, 30, 50, 30, 100]);
lastToggled = millis();
}
if (
dist(touches[0].x, touches[0].y, 20, 20) < 20 &&
millis() - lastToggled > 100
) {
musicDisabled = !musicDisabled;
lastToggled = millis();
if (musicDisabled) {
hotchoc.pause();
} else {
hotchoc.play();
}
} else if (
dist(touches[0].x, touches[0].y, width - 3 * u, height - 2 * u) < d
)
btn = "left";
else if (dist(touches[0].x, touches[0].y, width - u, height - 2 * u) < d)
btn = "right";
else if (
dist(touches[0].x, touches[0].y, width - 2 * u, height - 3 * u) < d
)
btn = "up";
else if (dist(touches[0].x, touches[0].y, width - 2 * u, height - u) < d)
btn = "down";
else if (
gameover &&
dist(touches[0].x, touches[0].y, width / 2, (3 * height) / 4) < 50
)
btn = "restart";
if (btn != "") gamestarted = true;
}
if (btn === "restart") {
setup();
}
if (gamestarted && !gameInitialized) initializeGame();
if (!gameover) {
if (btn === "left") {
edouard.o = 3;
} else if (btn === "right") {
edouard.o = 9;
} else if (btn === "up") {
edouard.o = 6;
} else if (btn === "down") {
edouard.o = 0;
}
}
if (gameInitialized) {
if (frameCount % 1200 == 0 && !gameover && predators.length < 1) {
append(predators, new Loup());
loupyestu = true;
}
for (let i = 0; i < predators.length; i++) {
predators[i].display();
}
for (let i = 0; i < things.length; i++) {
things[i].display();
}
textSize(30);
textAlign(RIGHT, TOP);
if (!gameover) {
textSize(30);
stroke(0);
strokeWeight(2);
fill("#009688");
text("" + score, (19 * width) / 20, height / 20);
}
if (things.length < 3 && frameCount % 200 == 0 && !gameover) {
append(things, new Thing());
if (random(1) < 0.5) append(things, new Thing());
}
}
if (gameover) {
fill("#009688");
textAlign(CENTER, CENTER);
textSize(30);
text("Score: " + score, width / 2, height / 2);
textSize(20);
text("Restart", width / 2, (3 * height) / 4);
return;
}
imageMode(CENTER);
if (gameInitialized) {
loveMeter = max(0, loveMeter - 0.04);
if (loveMeter == 0) gameover = true;
}
// herbe();
edouard.draw();
for (i = 1; i < n; i++) {
moutons[i].move();
moutons[i].draw();
}
rejane.x = 60;
rejane.y = height - 70;
rejane.draw();
if (gameInitialized && dist(rejane.x, rejane.y, edouard.x, edouard.y) < 50) {
loveMeter = min(loveMeter + 0.5, 100);
if (frameCount % 60 == 0 && loveMeter < 100) navigator.vibrate(50);
}
if (width > height) {
background(0);
return;
}
imageMode(CORNER);
image(notes, 5, 5, 30, 30);
if (musicDisabled) {
stroke("#380");
strokeWeight(3);
line(5, 35, 35, 5);
}
for (let i = 0; i < bullets.length; i++) {
bullets[i].move();
bullets[i].display();
}
for (let i = 0; i < droplets.length; i++) {
droplets[i].display();
}
cleanMess();
if (!gameInitialized) {
stroke(0);
fill("#009688");
textAlign(CENTER, CENTER);
textSize(30);
// text("Any button to start", width / 2, height / 2 + u);
imageMode(CENTER);
image(title, width / 2, height / 2);
}
}
function debug() {
textAlign(CENTER, CENTER);
textSize(12);
noStroke();
text(
windowWidth +
"x" +
windowHeight +
"y" +
pixelDensity() +
"d" +
int(frameRate()) +
"\nL" +
int(loveMeter) +
":T" +
things.length,
windowWidth / 2,
30
);
}
function touchStarted() {
var fs = fullscreen();
if (!fs) {
fullscreen(true);
}
}
/* full screening will change the size of the canvas */
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
edouard.x = 130;
edouard.y = height - 70;
}
/* prevents the mobile browser from processing some default
* touch events, like swiping left for "back" or scrolling
* the page.
*/
document.ontouchmove = function (event) {
event.preventDefault();
};
function isUndefined(value) {
// Obtain `undefined` value that's
// guaranteed to not have been re-assigned
var undefined = void 0;
return value === undefined;
}
function keyPressed() {
gamestarted = true;
}
function herbe() {
randomSeed(1);
let hmax = (3 * height) / 4;
for (i = 1; i < n; i++) {
for (x = width / 20; x < (19 * width) / 20; x = x + 3) {
colorMode(HSB, 100, 100, 100);
strokeWeight(1);
stroke(20 + random(30), 80, 60);
line(
x,
gbl + (i * hmax) / n - random(1),
x + 10 * noise((frameCount + x) / 50) - 5,
gbl + (i * hmax) / n - random(10) - 3
);
}
}
}
class Mouton {
constructor(index) {
this.index = index;
this.dir = -1 * (random(1) < 0.5);
this.dir = this.dir < 0 ? -1 : 1;
this.x =
(this.index < 5) * (width / 10 + random((8 * width) / 10)) +
((this.index == 5) * width) / 2;
this.y = index * floor(height / n);
this.speed = 1 + random(1);
this.state = 0;
this.hittime = -1;
}
move() {
let hmax = (3 * height) / 4;
this.y = (this.index * hmax) / n;
if (this.x < width / 20 || this.x > (19 * width) / 20)
this.dir = -1 * this.dir;
if (this.state < 1) this.x = this.x - this.speed * this.dir;
if (predators.length > 0)
if (
(this.state == 0) &
(dist(this.x, this.y, predators[0].x, predators[0].y) < 40)
) {
this.state = 1;
this.hittime = frameCount;
}
if (this.state > 0) {
score = score - 2;
droplets[droplets.length] = new Droplet(this.x, this.y);
}
}
draw() {
push();
translate(this.x, this.y);
rotate(noise((frameCount + 10 * this.index) / 100) / 3);
scale(this.dir, 1);
imageMode(CENTER);
if (this.state > 0) rotate(12 * (frameCount % 360));
image(
genders[this.index] == 0 ? m : b,
0,
-5,
0.6 * m.width,
0.6 * m.height
);
pop();
if (this.state > 0)
if (predators.length==0 || frameCount - this.hittime > 120) {
this.state = 0;
droplets = [];
predators = [];
}
}
}
class Sprite {
constructor(img, qui) {
this.img = img;
this.qui = qui;
this.x = width / 2;
this.y = height / 2;
this.o = 0;
this.dx = [0, -5, 0, 5];
this.dy = [5, 0, -5, 0];
this.state = 0;
this.hittime = -1;
}
draw() {
if (frameCount % 2 == 0 && btn != "" && this.qui == 0) {
sPose = (sPose + 1) % 3;
let oldx = this.x;
let oldy = this.y;
this.x += this.dx[this.o / 3];
this.y += this.dy[this.o / 3];
this.y = constrain(this.y, 30, height - 30);
this.x = constrain(this.x, 30, width - 30);
if (dist(edouard.x, edouard.y, rejane.x, rejane.y) < 40) {
this.x = oldx;
this.y = oldy;
}
}
push();
translate(this.x, this.y);
if (this.state > 0 && this.qui == 0) {
angleMode(DEGREES);
translate(0, -60 * sin((180 * (frameCount - this.hittime)) / 120));
rotate(12 * (frameCount % 360));
}
if (frameCount - this.hittime > 120) this.state = 0;
imageMode(CENTER);
image(
this.img.get(32 * (this.o + sPose * (this.qui == 0)), 0, 32, 64),
0,
0,
64,
128
);
pop();
for (let i = things.length - 1; i >= 0; i--) {
if (dist(this.x, this.y, things[i].x, things[i].y) < 30) {
score = score + 10 * (6 - things[i].row);
things.splice(i, 1);
navigator.vibrate(70);
}
}
if (this.state == 0) {
for (i = 1; i < n; i++) {
if (dist(this.x, this.y, moutons[i].x, moutons[i].y) < 20) {
this.state = 1;
this.hittime = frameCount;
score = score - 200;
navigator.vibrate([100, 20, 100, 20, 200]);
}
}
}
if (frameCount - this.hittime > 120) this.state = 0;
}
}
class Bullet {
constructor(tx, ty) {
this.x = rejane.x;
this.y = rejane.y;
this.tx = tx;
this.ty = ty;
this.speed = 0.1;
}
move() {
this.x += this.speed * (this.tx - this.x);
this.y += this.speed * (this.ty - this.y);
}
display() {
push();
translate(this.x, this.y);
noStroke();
fill(0);
ellipse(0, 0, 10, 10);
fill(220);
ellipse(-1, -1, 7, 7);
fill(255);
ellipse(-2, -2, 3, 3);
pop();
if (predators.length > 0)
if (dist(this.x, this.y, predators[0].x, predators[0].y) < 40) {
predators = [];
navigator.vibrate(200);
loupyestu = false;
score = score + 200;
}
}
}
class Droplet {
constructor(x, y) {
randomSeed(frameCount);
this.x = x;
this.y = y;
this.dist = 0;
this.angle = random(360);
this.t0 = frameCount;
}
display() {
this.dist += 3;
push();
translate(this.x, this.y);
angleMode(DEGREES);
this.angle = random(360);
rotate(this.angle);
translate(this.dist, 0);
noStroke();
fill("#f00");
if (this.dist < 50) ellipse(0, 0, 10, 5);
pop();
}
}
class Thing {
constructor() {
randomSeed(frameCount);
let hmax = (3 * height) / 4;
this.row = 1 + int(random(5));
this.x = 20 + random(width - 40);
this.y = (this.row * hmax) / n;
this.t0 = frameCount;
}
move() {}
display() {
let hmax = (3 * height) / 4;
imageMode(CENTER);
image(raisin, this.x, this.y, 40, 40);
}
}
class Loup {
constructor() {
randomSeed(millis());
let hmax = (3 * height) / 4;
this.row = 1 + int(random(5));
this.side = random() < 0.5 ? 1 : -1;
this.x = this.side < 0 ? 0 : width;
this.y = (this.row * hmax) / n;
this.t0 = frameCount;
}
move() {}
display() {
let hmax = (3 * height) / 4;
push();
translate(this.x, this.y);
scale(this.side, 1);
imageMode(CENTER);
image(loup, 0, 0);
pop();
}
}