From 1325cdb6377b156e4f6adbf9703cd35aee42cd48 Mon Sep 17 00:00:00 2001 From: Joe James Date: Wed, 23 Jan 2019 19:31:30 -0800 Subject: [PATCH] Uploaded all JS files --- breakout_1.js | 27 +++++++++ breakout_2.js | 34 +++++++++++ breakout_3.js | 73 ++++++++++++++++++++++++ breakout_4.js | 83 +++++++++++++++++++++++++++ breakout_5.js | 114 +++++++++++++++++++++++++++++++++++++ breakout_6.js | 131 ++++++++++++++++++++++++++++++++++++++++++ breakout_7.js | 145 ++++++++++++++++++++++++++++++++++++++++++++++ breakout_8.js | 153 +++++++++++++++++++++++++++++++++++++++++++++++++ breakout_9.js | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 915 insertions(+) create mode 100644 breakout_1.js create mode 100644 breakout_2.js create mode 100644 breakout_3.js create mode 100644 breakout_4.js create mode 100644 breakout_5.js create mode 100644 breakout_6.js create mode 100644 breakout_7.js create mode 100644 breakout_8.js create mode 100644 breakout_9.js diff --git a/breakout_1.js b/breakout_1.js new file mode 100644 index 0000000..1a7d6ed --- /dev/null +++ b/breakout_1.js @@ -0,0 +1,27 @@ +// JAVASCRIPT-1: MOVE BALL ON CANVAS +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = ballRadius; +var dx = 2; +var dy = -2; + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} + +function draw() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawBall(); + + x += dx; + y += dy; +} + +setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_2.js b/breakout_2.js new file mode 100644 index 0000000..04021d5 --- /dev/null +++ b/breakout_2.js @@ -0,0 +1,34 @@ +// JAVASCRIPT-2: MAKE BALL BOUNCE +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = ballRadius; +var dx = 2; +var dy = -2; + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} + +function draw() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawBall(); + + if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) { + dx = -dx; + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + + x += dx; + y += dy; +} + +setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_3.js b/breakout_3.js new file mode 100644 index 0000000..a960a79 --- /dev/null +++ b/breakout_3.js @@ -0,0 +1,73 @@ +// JAVASCRIPT-3: SHOW & MOVE PADDLE +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = ballRadius; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; + +function keyDownHandler(e) { + if(e.key == "Down" || e.key == "ArrowDown") { + downPressed = true; + } + else if(e.key == "Up" || e.key == "ArrowUp") { + upPressed = true; + } +} + +function keyUpHandler(e) { + if(e.key == "Down" || e.key == "ArrowDown") { + downPressed = false; + } + else if(e.key == "Up" || e.key == "ArrowUp") { + upPressed = false; + } +} + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} +function drawPaddle() { + ctx.beginPath(); + ctx.rect(0, paddleY, paddleWidth, paddleHeight); + ctx.fillStyle = "#333"; + ctx.fill(); + ctx.closePath(); +} + +function draw() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawBall(); + drawPaddle(); + + if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) { + dx = -dx; + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_4.js b/breakout_4.js new file mode 100644 index 0000000..2b4bb14 --- /dev/null +++ b/breakout_4.js @@ -0,0 +1,83 @@ +// JAVASCRIPT-4: PADDLE COLLISIONS +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = ballRadius; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; + +function keyDownHandler(e) { + if(e.key == "Down" || e.key == "ArrowDown") { + downPressed = true; + } + else if(e.key == "Up" || e.key == "ArrowUp") { + upPressed = true; + } +} + +function keyUpHandler(e) { + if(e.key == "Down" || e.key == "ArrowDown") { + downPressed = false; + } + else if(e.key == "Up" || e.key == "ArrowUp") { + upPressed = false; + } +} + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} +function drawPaddle() { + ctx.beginPath(); + ctx.rect(0, paddleY, paddleWidth, paddleHeight); + ctx.fillStyle = "#333"; + ctx.fill(); + ctx.closePath(); +} + +function draw() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawBall(); + drawPaddle(); + + if(x + dx > canvas.width-ballRadius) { + dx = -dx; + } + else if (x + dx < ballRadius) { + if (y > paddleY && y < paddleY + paddleHeight) { + dx = -dx; + } + else { + alert("GAME OVER"); + document.location.reload(); + clearInterval(interval); + } + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +var interval = setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_5.js b/breakout_5.js new file mode 100644 index 0000000..a1887c5 --- /dev/null +++ b/breakout_5.js @@ -0,0 +1,114 @@ +// JAVASCRIPT-5: SHOWING BRICKS +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = ballRadius; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; +var brickRowCount = 5; +var brickColumnCount = 4; +var brickWidth = 25; +var brickHeight = 60; +var brickPadding = 10; +var brickOffsetTop = 30; +var brickOffsetLeft = 400; +var bricks = []; +for(var c=0; c canvas.width-ballRadius) { + dx = -dx; + } + else if (x + dx < ballRadius) { + if (y > paddleY && y < paddleY + paddleHeight) { + dx = -dx; + } + else { + alert("GAME OVER"); + document.location.reload(); + clearInterval(interval); + } + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +var interval = setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_6.js b/breakout_6.js new file mode 100644 index 0000000..24e7106 --- /dev/null +++ b/breakout_6.js @@ -0,0 +1,131 @@ +// JAVASCRIPT-6: BRICK COLLISIONS +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = ballRadius; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; +var brickRowCount = 5; +var brickColumnCount = 4; +var brickWidth = 25; +var brickHeight = 60; +var brickPadding = 10; +var brickOffsetTop = 30; +var brickOffsetLeft = 400; +var bricks = []; +for(var c=0; c b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) { + dy = -dy; + b.visible = 0; + } + } + } + } +} + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} +function drawPaddle() { + ctx.beginPath(); + ctx.rect(0, paddleY, paddleWidth, paddleHeight); + ctx.fillStyle = "#333"; + ctx.fill(); + ctx.closePath(); +} + +function drawBricks() { + for(var c=0; c canvas.width-ballRadius) { + dx = -dx; + } + else if (x + dx < ballRadius) { + if (y > paddleY && y < paddleY + paddleHeight) { + dx = -dx; + } + else { + alert("GAME OVER"); + document.location.reload(); + clearInterval(interval); + } + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +var interval = setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_7.js b/breakout_7.js new file mode 100644 index 0000000..dd67d45 --- /dev/null +++ b/breakout_7.js @@ -0,0 +1,145 @@ +// JAVASCRIPT-7: SCORE +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = Math.random() * (canvas.height - 2*ballRadius) + ballRadius;; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; +var score = 0; +var brickRowCount = 5; +var brickColumnCount = 4; +var brickWidth = 25; +var brickHeight = 60; +var brickPadding = 10; +var brickOffsetTop = 30; +var brickOffsetLeft = 400; +var bricks = []; +for(var c=0; c b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) { + dy = -dy; + b.visible = 0; + score += c+1; + if(score == 50) { + alert("Congratulations, you win!\nScore: " + score); + document.location.reload(); + clearInterval(interval); + } + } + } + } + } +} + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} +function drawPaddle() { + ctx.beginPath(); + ctx.rect(0, paddleY, paddleWidth, paddleHeight); + ctx.fillStyle = "#333"; + ctx.fill(); + ctx.closePath(); +} + +function drawBricks() { + for(var c=0; c canvas.width-ballRadius) { + dx = -dx; + } + else if (x + dx < ballRadius) { + if (y > paddleY && y < paddleY + paddleHeight) { + dx = -dx; + } + else { + alert("GAME OVER"); + document.location.reload(); + clearInterval(interval); + } + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +var interval = setInterval(draw, 10); \ No newline at end of file diff --git a/breakout_8.js b/breakout_8.js new file mode 100644 index 0000000..07a0907 --- /dev/null +++ b/breakout_8.js @@ -0,0 +1,153 @@ +// JAVASCRIPT-8: MOUSE CONTROL & BRICK SHADING, ANIMATIONFRAME +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = Math.random() * (canvas.height - 2*ballRadius) + ballRadius;; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; +var score = 0; +var brickRowCount = 5; +var brickColumnCount = 4; +var brickWidth = 25; +var brickHeight = 60; +var brickPadding = 10; +var brickOffsetTop = 30; +var brickOffsetLeft = 400; +var bricks = []; +for(var c=0; c 0 && relativeY < canvas.height) { + paddleY = relativeY - paddleHeight/2; + } +} + +function collisionDetection() { + for(var c=0; c b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) { + dy = -dy; + b.visible = 0; + score += c+1; + if(score == 50) { + alert("Congratulations, you win!\nScore: " + score); + document.location.reload(); + } + } + } + } + } +} + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} +function drawPaddle() { + ctx.beginPath(); + ctx.rect(0, paddleY, paddleWidth, paddleHeight); + ctx.fillStyle = "#333"; + ctx.fill(); + ctx.closePath(); +} + +function drawBricks() { + var brickColor = ["#0095DD", "#0088BB", "#007799", "#006677"]; + for(var c=0; c canvas.width-ballRadius) { + dx = -dx; + } + else if (x + dx < ballRadius) { + if (y > paddleY && y < paddleY + paddleHeight) { + dx = -dx; + } + else { + alert("GAME OVER"); + document.location.reload(); + } + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; + requestAnimationFrame(draw); +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +document.addEventListener("mousemove", mouseMoveHandler, false); +draw(); \ No newline at end of file diff --git a/breakout_9.js b/breakout_9.js new file mode 100644 index 0000000..099508a --- /dev/null +++ b/breakout_9.js @@ -0,0 +1,155 @@ +// JAVASCRIPT-9: BIG 600x900 FIELD, MORE BRICKS +// © 2018, wbamberg +var canvas = document.getElementById("myCanvas"); +var ctx = canvas.getContext("2d"); +var ballRadius = 12; +var x = ballRadius; +var y = Math.random() * (canvas.height - 2*ballRadius) + ballRadius;; +var dx = 2; +var dy = -2; +var paddleHeight = 100; +var paddleWidth = 12; +var paddleY = (canvas.height-paddleHeight)/2; +var downPressed = false; +var upPressed = false; +var score = 0; +var brickRowCount = 10; +var brickColumnCount = 8; +var brickCount = brickRowCount * brickColumnCount; +var brickWidth = 20; +var brickHeight = 48; +var brickPadding = 5; +var brickOffsetTop = 25; +var brickOffsetLeft = 600; +var bricks = []; +for(var c=0; c 0 && relativeY < canvas.height) { + paddleY = relativeY - paddleHeight/2; + } +} + +function collisionDetection() { + for(var c=0; c b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) { + dy = -dy; + b.visible = 0; + score += c+1; + brickCount -= 1; + if(brickCount == 0) { + alert("Congratulations, you win!\nScore: " + score); + document.location.reload(); + } + } + } + } + } +} + +function drawBall() { + ctx.beginPath(); + ctx.arc(x, y, ballRadius, 0, 2 * Math.PI); + ctx.fillStyle = "#FF0000"; + ctx.fill(); + ctx.closePath(); +} +function drawPaddle() { + ctx.beginPath(); + ctx.rect(0, paddleY, paddleWidth, paddleHeight); + ctx.fillStyle = "#333"; + ctx.fill(); + ctx.closePath(); +} + +function drawBricks() { + var brickColor = ["#0095DD", "#0088BB", "#007799", "#006677", "#005555", "#004433", "#003311", "#002200"]; + for(var c=0; c canvas.width-ballRadius) { + dx = -dx; + } + else if (x + dx < ballRadius) { + if (y > paddleY && y < paddleY + paddleHeight) { + dx = -dx; + } + else { + //alert("GAME OVER"); + document.location.reload(); + } + } + if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) { + dy = -dy; + } + if(downPressed && paddleY < canvas.height-paddleHeight) { + paddleY += 4; + } + else if(upPressed && paddleY > 4) { + paddleY -= 4; + } + + x += dx; + y += dy; + requestAnimationFrame(draw); +} + +document.addEventListener("keydown", keyDownHandler, false); +document.addEventListener("keyup", keyUpHandler, false); +document.addEventListener("mousemove", mouseMoveHandler, false); +draw(); \ No newline at end of file