-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5900359
Showing
9 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en-US"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width"> | ||
<title>Bouncing balls</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
|
||
<body> | ||
<h1>bouncing balls</h1> | ||
<canvas></canvas> | ||
|
||
<script src="main-finished.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// set up canvas | ||
|
||
const canvas = document.querySelector("canvas"); | ||
const ctx = canvas.getContext("2d"); | ||
|
||
const width = (canvas.width = window.innerWidth); | ||
const height = (canvas.height = window.innerHeight); | ||
|
||
// function to generate random number | ||
|
||
function random(min, max) { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
|
||
// function to generate random RGB color value | ||
|
||
function randomRGB() { | ||
return `rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)})`; | ||
} | ||
|
||
class Ball { | ||
constructor(x, y, velX, velY, color, size) { | ||
this.x = x; | ||
this.y = y; | ||
this.velX = velX; | ||
this.velY = velY; | ||
this.color = color; | ||
this.size = size; | ||
} | ||
|
||
draw() { | ||
ctx.beginPath(); | ||
ctx.fillStyle = this.color; | ||
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI); | ||
ctx.fill(); | ||
} | ||
|
||
update() { | ||
if (this.x + this.size >= width) { | ||
this.velX = -Math.abs(this.velX); | ||
} | ||
|
||
if (this.x - this.size <= 0) { | ||
this.velX = Math.abs(this.velX); | ||
} | ||
|
||
if (this.y + this.size >= height) { | ||
this.velY = -Math.abs(this.velY); | ||
} | ||
|
||
if (this.y - this.size <= 0) { | ||
this.velY = Math.abs(this.velY); | ||
} | ||
|
||
this.x += this.velX; | ||
this.y += this.velY; | ||
} | ||
|
||
collisionDetect() { | ||
for (const ball of balls) { | ||
if (!(this === ball)) { | ||
const dx = this.x - ball.x; | ||
const dy = this.y - ball.y; | ||
const distance = Math.sqrt(dx * dx + dy * dy); | ||
|
||
if (distance < this.size + ball.size) { | ||
ball.color = this.color = randomRGB(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const balls = []; | ||
|
||
while (balls.length < 25) { | ||
const size = random(10, 20); | ||
const ball = new Ball( | ||
// ball position always drawn at least one ball width | ||
// away from the edge of the canvas, to avoid drawing errors | ||
random(0 + size, width - size), | ||
random(0 + size, height - size), | ||
random(-7, 7), | ||
random(-7, 7), | ||
randomRGB(), | ||
size | ||
); | ||
|
||
balls.push(ball); | ||
} | ||
|
||
function loop() { | ||
ctx.fillStyle = "rgba(0, 0, 0, 0.25)"; | ||
ctx.fillRect(0, 0, width, height); | ||
|
||
for (const ball of balls) { | ||
ball.draw(); | ||
ball.update(); | ||
ball.collisionDetect(); | ||
} | ||
|
||
requestAnimationFrame(loop); | ||
} | ||
|
||
loop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
html, body { | ||
margin: 0; | ||
} | ||
|
||
html { | ||
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | ||
height: 100%; | ||
} | ||
|
||
body { | ||
overflow: hidden; | ||
height: inherit; | ||
} | ||
|
||
h1 { | ||
font-size: 2rem; | ||
letter-spacing: -1px; | ||
position: absolute; | ||
margin: 0; | ||
top: -4px; | ||
right: 5px; | ||
|
||
color: transparent; | ||
text-shadow: 0 0 4px white; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Marco Taylor | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# AdvancedBouncingBalls | ||
An improved boundingballs game from mdn JavaScript Tutorial |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en-US"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width"> | ||
<title>Bouncing balls</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
|
||
<body> | ||
<h1>bouncing balls</h1> | ||
<canvas></canvas> | ||
|
||
<script src="main-finished.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// set up canvas | ||
|
||
const canvas = document.querySelector("canvas"); | ||
const ctx = canvas.getContext("2d"); | ||
|
||
const width = (canvas.width = window.innerWidth); | ||
const height = (canvas.height = window.innerHeight); | ||
|
||
// function to generate random number | ||
|
||
function random(min, max) { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
|
||
// function to generate random RGB color value | ||
|
||
function randomRGB() { | ||
return `rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)})`; | ||
} | ||
|
||
class Ball { | ||
constructor(x, y, velX, velY, color, size) { | ||
this.x = x; | ||
this.y = y; | ||
this.velX = velX; | ||
this.velY = velY; | ||
this.color = color; | ||
this.size = size; | ||
} | ||
|
||
draw() { | ||
ctx.beginPath(); | ||
ctx.fillStyle = this.color; | ||
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI); | ||
ctx.fill(); | ||
} | ||
|
||
update() { | ||
if (this.x + this.size >= width) { | ||
this.velX = -Math.abs(this.velX); | ||
} | ||
|
||
if (this.x - this.size <= 0) { | ||
this.velX = Math.abs(this.velX); | ||
} | ||
|
||
if (this.y + this.size >= height) { | ||
this.velY = -Math.abs(this.velY); | ||
} | ||
|
||
if (this.y - this.size <= 0) { | ||
this.velY = Math.abs(this.velY); | ||
} | ||
|
||
this.x += this.velX; | ||
this.y += this.velY; | ||
} | ||
|
||
collisionDetect() { | ||
for (const ball of balls) { | ||
if (!(this === ball)) { | ||
const dx = this.x - ball.x; | ||
const dy = this.y - ball.y; | ||
const distance = Math.sqrt(dx * dx + dy * dy); | ||
|
||
if (distance < this.size + ball.size) { | ||
ball.color = this.color = randomRGB(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const balls = []; | ||
|
||
while (balls.length < 25) { | ||
const size = random(10, 20); | ||
const ball = new Ball( | ||
// ball position always drawn at least one ball width | ||
// away from the edge of the canvas, to avoid drawing errors | ||
random(0 + size, width - size), | ||
random(0 + size, height - size), | ||
random(-7, 7), | ||
random(-7, 7), | ||
randomRGB(), | ||
size | ||
); | ||
|
||
balls.push(ball); | ||
} | ||
|
||
function loop() { | ||
ctx.fillStyle = "rgba(0, 0, 0, 0.25)"; | ||
ctx.fillRect(0, 0, width, height); | ||
|
||
for (const ball of balls) { | ||
ball.draw(); | ||
ball.update(); | ||
ball.collisionDetect(); | ||
} | ||
|
||
requestAnimationFrame(loop); | ||
} | ||
|
||
loop(); | ||
|
||
|
||
class Shape{ | ||
constructor(x, y, velX, velY, color, size) { | ||
this.x = x; | ||
this.y = y; | ||
this.velX = velX; | ||
this.velY = velY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
html, body { | ||
margin: 0; | ||
} | ||
|
||
html { | ||
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | ||
height: 100%; | ||
} | ||
|
||
body { | ||
overflow: hidden; | ||
height: inherit; | ||
} | ||
|
||
h1 { | ||
font-size: 2rem; | ||
letter-spacing: -1px; | ||
position: absolute; | ||
margin: 0; | ||
top: -4px; | ||
right: 5px; | ||
|
||
color: transparent; | ||
text-shadow: 0 0 4px white; | ||
} |