xxxxxxxxxx
334
let pp=0;
let mutton=0;
var c;
let values = [];
// variable to hold an instance of the p5.webserial library:
const serial = new p5.WebSerial();
// HTML button object:
let portButton;
let inData; // for incoming serial data
let outByte = 0; // for outgoing data
let rate = 0;
let gamestatus;
let arrCars;
let carrs = [];
let carsctr = 0;
let difficulty = 0;
let currentScore = 0;
let prevScore = 0;
let lanes = [8,88,168,248,328];
let lanes2 = [0,0,0,0,0];
function preload(){
img = loadImage('maincar.png');
car1 = loadImage('car1.png');
car2 = loadImage('car2.png');
car3 = loadImage('car3.png');
backgroundd = loadImage('background.png');
backgrounddd = loadImage('final.gif');
}
function setup() {
createCanvas(400, 550);
//------------------------------------------------
if (!navigator.serial) {
alert("WebSerial is not supported in this browser. Try Chrome or MS Edge.");
}
// if serial is available, add connect/disconnect listeners:
navigator.serial.addEventListener("connect", portConnect);
navigator.serial.addEventListener("disconnect", portDisconnect);
// check for any ports that are available:
serial.getPorts();
// if there's no port chosen, choose one:
serial.on("noport", makePortButton);
// open whatever port is available:
serial.on("portavailable", openPort);
// handle serial errors:
serial.on("requesterror", portError);
// handle any incoming serial data:
serial.on("data", serialEvent);
serial.on("close", makePortButton);
//--------------------------------------------------
maincar = new main_car();
arrCars = [car1,car2,car3] ;
}
function keyPressed() {
if(keyCode === ENTER){
gamestatus = "running";
}
if (keyCode === UP_ARROW){
}
// if(keyCode === LEFT_ARROW){
// rate = -5;
// }
// if(keyCode === RIGHT_ARROW){
// rate = 5;
// }
// if(keyCode === BACKSPACE){
// rocks.splice(0,rocks.length);
// rocksctr = 0;
// }
}
function keyReleased()
{
// if (keyCode === RIGHT_ARROW || keyCode === LEFT_ARROW)
// {
// rate = 0;
// }
if(keyCode === ENTER){
gamestatus = "running";
}
}
function generateCars(){
let rand = int(random(0, 100));
let rand2 = int(random(0, 100));
if(rand % 7 == 0 && carrs.length < 4){
if(rand2 % 3 == 0){
if(rand2 % 2 == 0 && rand % 2 == 0){
carrs[carsctr] = new cars();
carrs[carsctr].display();
// console.log(carsctr);
carsctr++;
}
}
}
}
function displayCars(){
for(let i = 0; i < carrs.length; i++){
carrs[i].display();
// console.log(">",carrs.length);
let temp = false;
if(maincar.didcollide(carrs[i])){
checklanes(0,carrs[i]);
carrs.splice(i,1);
carsctr--;
currentScore = 0;
// console.log("collision!")
// gamestatus = "end";
// bomb.play();
}else if(carrs[i].y > height || temp){
checklanes(0,carrs[i]);
carrs.splice(i,1);
carsctr--;
currentScore++;
}
}
}
function score(){
textSize(32);
fill(250);
text("Score: "+currentScore,20,50);
}
function input(){
console.log(pp);
if(pp == 1){
rate = 0;
}else if(pp == 2){
rate = 5;
}else if(pp == 0){
rate = -5;
}
}
function draw() {
pp=int(values[1]);
mutton=int(values[0]);
image(backgrounddd,0,0);
input();
maincar.display();
generateCars();
displayCars();
score();
//---------------------------------------------------
}
function increaseD(){
if(currentScore === 10 + prevScore){
difficulty += 0.5;
prevScore = currentScore;
// console.log(difficulty);
}
return random(1,5)+difficulty;
}
function checklanes(x,other){
if(x === 1){
if(lanes2[other.temp] === 1){
other.temp = int(random(0,4));
other.x = lanes[other.temp];
checklanes(1,other);
}else{
lanes2[other.temp] = 1;
}
}else if(x === 0){
lanes2[other.temp] = 0;
}
}
class cars{
constructor(){
this.temp = int(random(0,4));
this.x = lanes[this.temp];
this.y = -20;
this.cartype = int(random(0, 3));
this.rateFall = increaseD();
checklanes(1,this);
}
display(){
image(arrCars[this.cartype],this.x,this.y);
this.move();
}
move(){
this.y += this.rateFall;
}
width(){
return 70;
}
increaseD(){
}
}
class main_car{
constructor(){
this.x = 200;
this.y = 450;
this.display();
}
display(){
imageMode(CENTER);
image(img,this.x,this.y);
this.move();
this.checkboundries();
imageMode(CORNER);
}
move(){
this.x += rate;
}
checkboundries(){
if(this.x > width){
this.x = 0;
}else if(this.x < 0){
this.x = width;
}
}
didcollide(other){
if ( (this.x <= (other.x + other.width())) && (this.x >= other.x)) {
if ((this.y <= (90 + other.y + other.width())) && (this.y >= other.y)){
// print("Collision");
return true;
}
}
}
}
//----------------------------------------------
// if there's no port selected,
// make a port select button appear:
function makePortButton() {
// create and position a port chooser button:
portButton = createButton("choose port");
portButton.position(10, 10);
// give the port button a mousepressed handler:
portButton.mousePressed(choosePort);
}
// make the port selector window appear:
function choosePort() {
if (portButton) portButton.show();
serial.requestPort();
}
// open the selected port, and make the port
// button invisible:
function openPort() {
// wait for the serial.open promise to return,
// then call the initiateSerial function
serial.open().then(initiateSerial);
// once the port opens, let the user know:
function initiateSerial() {
console.log("port open");
}
// hide the port button once a port is chosen:
if (portButton) portButton.hide();
}
// pop up an alert if there's a port error:
function portError(err) {
alert("Serial port error: " + err);
}
// read any incoming data as a string
// (assumes a newline at the end of it):
function serialEvent() {
//inData = Number(serial.read());
//console.log(inData);
let data = serial.readStringUntil('\n');
Console.log(">>",data);
values = split(data, ',');
}
// try to connect if a new serial port
// gets added (i.e. plugged in via USB):
function portConnect() {
console.log("port connected");
serial.getPorts();
}
// if a port is disconnected:
function portDisconnect() {
serial.close();
console.log("port disconnected");
}
function closePort() {
serial.close();
}