xxxxxxxxxx
137
let w;
let h;
let baloons=[]
var barray=[]
var screen=0
var score=0
var gtimer=180
function preload() {
cur = loadImage("assets/cur.png");
b=loadImage("assets/baloon-2.png");
bg = loadImage('assets/bg.png');
screen1 = loadImage('assets/screen1.png');
screen2 = loadImage('assets/screen2.png');
burst = loadSound('balloon.wav');
shoot = loadSound('assets/shoot.mp3');
}
function setup() {
createCanvas(500, 500);
setInterval(45, 1000);
for(let j=0; j<4; j++){
for (let i=0; i<6; i++){
baloons.push(new Baloon(10+(i*80),90+(j*90)))
}}
}
function draw(){
if (screen==0){
startscreen()
}
else if (screen==1){
gamestart()
}
else if (screen==2){
gameover()
}
}
function gamestart()
{
background(bg);
for (let i = 0; i < baloons.length; i++) {
baloons[i].run();
}
noCursor()
image(cur,mouseX,mouseY,30,30)
if (gtimer > 0) {
gtimer--;
}
text("Time Left: "+ str(gtimer), 50,55);
if (gtimer==0){
screen=2
}
}
function mousePressed(){
shoot.play();
screen=1
for (let i = baloons.length - 1; i >= 0; i--) {
let popped = baloons[i].pop();
if (popped) {
baloons.splice(i, 1);
burst.play();
score+=10
break;
}
}}
class Baloon{
constructor(x,y){
this.x=x
this.y=y
}
run() {
this.display()
rect(350,35,100,30,20)
text(" SCORE: "+ str(score), 350,55);
textSize(16);
}
display()
{
image(b, this.x, this.y,70,80)
}
pop(){
let dis;
dis=dist(mouseX, mouseY,this.x,this.y)
if (dis<40){
return true
}
}
}
function startscreen(){
background(screen1);
rect(200,430,100,30,20)
text("PLAY!", 223,453)
textSize(20)
noCursor()
image(cur,mouseX,mouseY,30,30)
}
function gameover(){
background(screen2);
image(cur,mouseX,mouseY,30,30)
noCursor()
textSize(25)
text("SCORE: "+str(score), 180,410)
textSize(15)
text("Press ENTER/RETURN to restart", 140,440);
}
function keyPressed()
{
if (keyCode === ENTER) {
screen=0
gtimer=180+60
baloons=[]
score=0
text(" SCORE: "+ str(score), 350,55);
textSize(16);
for(let j=0; j<4; j++){
for (let i=0; i<6; i++){
baloons.push(new Baloon(10+(i*80),90+(j*90)))
}}
}
}