Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gravity Simulation Game #2355

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Gravity Simulation Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="style.css">
</head>
<body >
<h1>Enjoy Gravity ! </h1>
<h3>Select a Celestial Body from below </h3>
<div id="dd">
<input type="radio" name="opt">Sun
<input type="radio" name="opt">Mercury
<input type="radio" name="opt">Venus
<input type="radio" name="opt">Earth
<input type="radio" name="opt" >Moon
<input type="radio" name="opt" >Mars
<input type="radio" name="opt">Jupiter
<input type="radio" name="opt">Saturn
<input type="radio" name="opt">Uranus
<input type="radio" name="opt">Neptune
<input type="radio" name="opt">Pluto
</div>
<br>
<button id="b">Click Here To Start !</button>
<br>
<button id="bt">Click Here To Stop Current Simulation !</button><br>
<button id="btn">Reset Canvas !</button>
<br><br>
<canvas></canvas>
<script src="script.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions Gravity Simulation Game/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"manifest_version": 3,
"name": "Gravity Simulation",
"description": "Gravity Simulation Game",
"version": "1.0",
"permissions": [
"storage",
"declarativeNetRequest",
"declarativeNetRequestWithHostAccess",
"tabs"
],
"host_permissions": [
"*://*/*"
],
"action": {
"default_popup": "index.html"
}
}
153 changes: 153 additions & 0 deletions Gravity Simulation Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Learn to code this at:
// https://www.youtube.com/watch?v=3b7FyIxWW94

// Initial Setup
document.getElementById('bt').addEventListener('click', function(){
location.reload();
})
document.getElementById('btn').addEventListener('click', function(){
location.reload();
})
document.getElementById('b').addEventListener('click', function(){
var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');

canvas.width = innerWidth;
canvas.height = innerHeight/2;


// Variables
var mouse = {
x: innerWidth / 2,
y: innerHeight / 2
};

var colors = [
'rgb(235, 1, 40)',
'white',
'black',
'pink'
];

var gravity = 0.2;
var x = document.getElementsByName('opt');
var friction = 0.98;
if(x[0].checked)
gravity=27.4;
if(x[1].checked)
gravity=0.37;
if(x[2].checked)
gravity=0.89;
if(x[3].checked)
gravity=0.98;
if(x[4].checked)
gravity=0.162;
if(x[5].checked)
gravity=0.37;
if(x[6].checked)
gravity=2.31;
if(x[7].checked)
gravity=0.9;
if(x[8].checked)
gravity=0.87;
if(x[9].checked)
gravity=1.1;
if(x[10].checked)
gravity=0.07;

document.getElementById('dd').style.display='none';
addEventListener("mousemove", function(event) {
mouse.x = event.clientX;
mouse.y = event.clientY;
});

addEventListener("resize", function() {
canvas.width = innerWidth;
canvas.height = innerHeight;
init();
});

addEventListener("click", function(event) {
init();
});


// Utility Functions
function randomIntFromRange(min,max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

function randomColor(colors) {
return colors[Math.floor(Math.random() * colors.length)];
}


// Objects
function Ball(x, y, dx, dy, radius, color) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.radius = radius;
this.color = color;

this.update = function() {
if (this.y + this.radius + this.dy> canvas.height) {
this.dy = -this.dy;
this.dy = this.dy * friction;
this.dx = this.dx * friction;
} else {
this.dy += gravity;
}

if (this.x + this.radius >= canvas.width || this.x - this.radius <= 0) {
this.dx = -this.dx * friction;
}

this.x += this.dx;
this.y += this.dy;
this.draw();
};

this.draw = function() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.stroke();
c.closePath();
};
}


// Implementation
var ballArray = [];

function init() {
ballArray = [];

for (let i = 0; i < 600; i++) {
var radius = randomIntFromRange(8, 20);
var x = randomIntFromRange(radius, canvas.width - radius);
var y = randomIntFromRange(0, canvas.height - radius);
var dx = randomIntFromRange(-3, 3)
var dy = randomIntFromRange(-2, 2)
ballArray.push(new Ball(x, y, dx, dy, radius, randomColor(colors)));
}
}

// Animation Loop
function animate() {
requestAnimationFrame(animate);

c.clearRect(0, 0, canvas.width, canvas.height);

for (let i = 0; i < ballArray.length; i++) {
ballArray[i].update();
}
}

init();
animate();
}
)
78 changes: 78 additions & 0 deletions Gravity Simulation Game/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* body {
margin: 0;
overflow: hidden;
} */

@media only screen and (min-width: 600px) {
canvas{
margin: auto;
}
h1,h3{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
text-align: center;
color: rgb(243, 63, 63);
}
#dd{
color: rgb(245, 53, 53);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-size: 1em;
padding: 0px 260px;
}
input{
accent-color: rgb(240, 61, 91);
}


button{
display: block;
margin: auto;
margin-bottom: 0px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
border: 2px solid rgb(235, 1, 40);
background-color: white;
cursor: pointer;
transition-duration: 0.5s;
}

button:hover{
background-color: rgb(236, 36, 36);
color: white;
}
}

@media only screen and (max-width: 600px) {
canvas{
margin: auto;
}
h1,h3{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
text-align: center;
color: rgb(243, 63, 63);
}
#dd{
color: rgb(245, 53, 53);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
font-size: 1em;
padding: 0px;
}
input{
accent-color: rgb(240, 61, 91);
}


button{
display: block;
margin: auto;
margin-bottom: 0px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
border: 2px solid rgb(235, 1, 40);
background-color: white;
cursor: pointer;
transition-duration: 0.5s;
}

button:hover{
background-color: rgb(236, 36, 36);
color: white;
}
}
Loading