xxxxxxxxxx
246
// vector arts from href="http://www.freepik.com">Designed by macrovector
// help from Dylan and Cezar
// sound mp3 from freesound.org
// resources: https://creative-coding.decontextualize.com/making-games-with-p5-play/
// resources: https://molleindustria.github.io/p5.play/
// library https://molleindustria.github.io/p5.play/docs/classes/Group.html
let serial;
let options = {baud: 9600}
let port = "/dev/tty.usbmodem142301 "; // write your port info here
let x,y;
let xoffset=0;
let yoffset=0;
let play = 1;
let end = 0;
let gamestate = play;
let intro;
let sword,fruit1,fruit2,fruit3,fruit4,fruit5,bomb;
let swordImage,bombImage,gameoverImage;
let fruitGroup,enemyGroup;
let gameover;
let score = 0;
let gameoversound, winsound;
function preload() {
fruit1 = loadImage("apple.png");
fruit2 = loadImage("banana.png");
fruit3 = loadImage("grape.png");
fruit4 = loadImage("lemon.png");
fruit5 = loadImage("strawberry.png");
bombImage = loadImage("bomb.png");
swordImage = loadImage("basket.png");
gameoverImage = loadImage("gameover.png");
slicing = loadSound("pop.mp3");
gameoversound = loadSound('gameover.mp3');
winsound = loadSound('win.mp3');
intro = loadImage('intro.png');
}
function setup() {
createCanvas(windowWidth, windowHeight);
serial = new p5.SerialPort(options);
let portlist = serial.list();
// Change this to the name of your arduino's serial port
// Register some callbacks
serial.on('connected', serverConnected);
serial.on('list', gotList);
serial.on('data', gotData);
serial.on('error', gotError);
serial.on('open', gotOpen);
serial.open(port);
mode = 0;
score = 0;
sword = createSprite(-10,-10,20,20);
fruitGroup = new Group();
enemyGroup = new Group();
x = windowWidth/2;
y = windowHeight/2;
// let button = createButton("Press button to play again!");
// button.mousePressed(resetSketch)
}
// function resetSketch(){
// //if the game is over, reset to play
// if(gamestate === end ){
// mode = 1;
// gamestate = play;
// }
// }
function draw() {
background('lightyellow');
if (mode == 0){
textSize(30);
textAlign(CENTER, CENTER);
image(intro, 0, 0);
intro.resize(windowWidth, windowHeight);
textStyle(ITALIC);
fill(random(30, 100), random(100, 200), random(0, 80));
text('Press enter to start', windowWidth/2, 550);
}
if (mode == 1){
if(gamestate === play && score <10){
sword.scale = 0.13;
sword.addImage(swordImage);
enemy();
fruits();
sword.position.y = x;
sword.position.x = y;
for (let i = 0; i < fruitGroup.length; i++){
if (sword.overlap(fruitGroup[i])){
slicing.play();
score += 2;
fruitGroup[i].remove();
}
if (enemyGroup.overlap(sword)){
gamestate = end;
}
}
}
if (gamestate === end){
for (let i = 0; i< fruitGroup.length; i++){
fruitGroup.removeSprites(fruit);
enemyGroup.removeSprites(bomb);
fruitGroup.velocityY = 0;
enemyGroup.velocityY = 0;
sword.scale = 0.3;
sword.addImage(gameoverImage);
sword.position.x = windowWidth/2;
sword.position.y = windowHeight/2;
gameoversound.play();
}
}
if (score === 10){
textSize(35);
text("YOU WIN!", windowWidth/2, windowHeight/2);
for (let i = 0; i< fruitGroup.length; i++){
fruitGroup.removeSprites(fruit);
enemyGroup.removeSprites(bomb);
fruitGroup.velocityY = 0;
enemyGroup.velocityY = 0;
winsound.play();
}
}
drawSprites();
textSize(32);
text("Score: " + score, (windowWidth/2)-50, 50);
}
}
function enemy(){
if (frameCount % 200 === 0){
bomb = createSprite(random(0, windowWidth),0, 20, 20);
bomb.addAnimation("moving", bombImage);
bomb.x = Math.round(random(100,300));
bomb.scale = 0.16;
bomb.velocity.y = 6;
bomb.life = 200;
enemyGroup.add(bomb);
}
}
function fruits(){
if(frameCount % 50 === 0){
fruit = createSprite(random(0, windowWidth),0,20,20);
fruit.scale = 0.07;
r = Math.round(random(1,4));
if (r == 1) {
fruit.addImage(fruit1);
} else if (r == 2) {
fruit.addImage(fruit2);
} else if (r == 3) {
fruit.addImage(fruit3);
} else if (r == 4) {
fruit.addImage(fruit4);
} else {
fruit.addImage(fruit5);
}
fruit.x = Math.round(random(50,340));
fruit.velocity.y = 6;
fruit.life = 200;
fruitGroup.add(fruit);
}
}
function keyPressed(){
if (keyCode === ENTER){
mode = 1;
}
}
function gotData() {
let currentString = serial.readStringUntil("\r\n");
if (currentString){
let data = split(currentString , ",");
console.log(data);
xoffset = map(float(data[1]),-180,180,-8,8 );
yoffset = map(float(data[2]),-180,180,-8,8 );
if (x <= (windowWidth - 50) && x >= 0) {
x = x + xoffset;
} else {
if (x > (windowWidth - 50)){
x = windowWidth - 50;
}
if ( x < 0){
x = 0;
}
}
if (y <= windowHeight && y >= 0){
y = y + yoffset;
} else {
if (y > windowHeight){
y = windowHeight;
}
if ( y < 0){
y = 0;
}
}
serial.write('x');
}
}
// We are connected and ready to go
function serverConnected() {
console.log("We are connected!");
}
// Got the list of ports
function gotList(thelist) {
// theList is an array of their names
for (let i = 0; i < thelist.length; i++) {
// Display in the console
// console.log(i + " " + thelist[i]);
}
}
// Connected to our serial device
function gotOpen() {
console.log("Serial Port is open!");
serial.write('x');
}
// Ut oh, here is an error, let's log it
function gotError(theerror) {
console.log(theerror);
}