Hello all,
So I read this tutorial on MDN for creating a breakout game using Javascript and Canvas as part of a project for the web development course I’m currently taking. I’m happy to say that it went well! However, I’m trying to modify the code for it so that instead of getting one point for each brick you hit, your score doubles for each brick you hit.
I have copied some of the code for the breakout game below. You’ll see some code that is commented out in the variables section as well as in the Collision Detection function towards the bottom. The commented out code is the work my web development instructor and me did when I asked him for his assistance after class on Wednesday. But because we were unsuccessful in getting the code to work the way I envisioned it, he suggested that I seek out one of the assistant instructors for help.
var canvas = document.getElementById(“myCanvas”);
var ctx = canvas.getContext(“2d”);
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
var ballRadius = 10;
var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width-paddleWidth)/2;
var rightPressed = false;
var leftPressed = false;
var brickRowCount = 3;
var brickColumnCount = 5;
var brickWidth = 75;
var brickHeight = 20;
var brickPadding = 10;
var brickOffsetTop = 30;
var brickOffsetLeft = 30;
var score = 0;
//var score = 1;
//var score2 = score*2;
var lives = 3;
var bricks = [];
for(c=0; c<brickColumnCount; c++) {
bricks[c] = [];
for(r=0; r<brickRowCount; r++) {
bricks[c][r] = { x: 0, y: 0, status: 1 };
}
}
function collisionDetection() {
for(c=0; c<brickColumnCount; c++) {
for(r=0; r<brickRowCount; r++) {
var b = bricks[c][r];
if(b.status == 1) {
if(x > b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) {
dy = -dy;
b.status = 0;
score++;
//score**;
//score**2;
//score*;
//score2;
if(score == brickRowCountbrickColumnCount) {
alert(“YOU WIN, CONGRATULATIONS!”);
document.location.reload();
}
}
}
}
}
}
I emailed the assistant instructor twice already, but have yet to receive any meaningful input from him(his daytime job is a web developer). Would I have to create new variables and/or functions for this, or would I only have to modify the existing code? If I have to add new code, does that mean I also have to comment out and/or remove some of the current code?
Please let me know if you have any questions or if there is anything you want me to clarify.
Thanks to all who responded to this post. I greatly appreciate your input!
-Abhi