xxxxxxxxxx
// object-oriented programming (OOP)
// is programming using OBJECTS
// objects are pieces of computer code
// that contain both functions *and* data
//
// objects in javascript are accessed through
// things called "classes"
// classes have methods which are functions
// classes have properties which are variables
// classes also have a constructor function which runs
// when you make one. this is called instantiation.
var NUMROWS = 5;
var NUMCOLUMNS = 10;
var BWIDTH = 40;
var BHEIGHT = 15;
var BHSPACING = 5;
var BVSPACING = 5;
var TOTALLIVES = 5;
var NUMLIVES;
var LEFTWALL, RIGHTWALL, TOPWALL, BOTTOMWALL; // these are walls
var bricks = []; // array to hold all the bricks
var ball; // variable to contain a ball
var paddle; // variable to contain the paddle
var isplaying = false; // ARE WE PLAYING THE GAME?
var bsound,psound,wsound,usound; // sounds
function preload() {
bsound = loadSound("bip.mp3");
psound = loadSound("k.mp3");
wsound = loadSound("bop.mp3");
usound = loadSound("ugh.mp3");
}
function setup() {
createCanvas(windowWidth, windowHeight);
LEFTWALL = width*0.2;
RIGHTWALL = width*0.8;
TOPWALL = height*0.1;
BOTTOMWALL = height*0.9;
textSize(60);
}
function draw() {
background(0);
if(!isplaying) // STARTUP SCREEN
{
fill(255);
textAlign(CENTER);
if(frameCount%60>30) text('PRESS ANY KEY TO BEGIN', width/2, height/2);
}
if(isplaying) // MAIN GAME LOOP
{
drawWallsScore(); // draw the walls and score
paddle.move(mouseX-paddle.w/2); // move the paddle
// draw the bricks and collision detect:
for(let i = bricks.length-1;i>=0;i--) // go through the bricks backwards
{
bricks[i].draw(); // draw them
if(bricks[i].collide(ball)) // see if the ball is hitting them
{
ball.bounce(bricks[i]);
ball.velocity*=1.01; // make it faster
bsound.play();
}
if(bricks[i].alive==false) // KILL THE BRICK
{
bricks.splice(i, 1); // deleted
}
}
if(paddle.collide(ball)) {
ball.bounce(paddle);
ball.angle+=map(ball.x-paddle.x,0,paddle.w,-0.3, 0.3);
psound.play();
}
ball.draw(); // draw the ball
paddle.draw(); // draw the paddle
}
}
function keyTyped() {
restartGame();
}