Skip to content

Commit

Permalink
Reorganise
Browse files Browse the repository at this point in the history
  • Loading branch information
codeincontext committed Feb 5, 2011
1 parent c092de5 commit 0677857
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 53 deletions.
19 changes: 0 additions & 19 deletions balls.html

This file was deleted.

3 changes: 3 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
canvas {
border: 10px solid #999;
}
21 changes: 21 additions & 0 deletions game.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Balls with canvas</title>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
</head>
<body>
<section class="content">

<h2>Hit some balls!</h2>
<canvas id="myCanvas" width="500" height="500">
</canvas>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="js/cue.js"></script>
<script src="js/game.js"></script>

</section>
</body>
</html>
Empty file added js/comms.js
Empty file.
40 changes: 40 additions & 0 deletions js/cue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Cue (x, y) {
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.move=move;
function move(){
friction = 0.995
this.vx *= friction;
this.vy *= friction;
if (this.vy < 0.1 && this.vy > -0.1 && this.vx < 0.1 && this.vx > -0.1) {this.vx = 0; this.vy = 0;}
// displace the ball
if( this.x<20 || this.x>width-20) this.vx=-this.vx;
if( this.y<20 || this.y>height-20) this.vy=-this.vy;
this.x+=this.vx;
this.y+=this.vy;
};
this.checkCollision = false;
}

function calculateCuePull(){
// dx and dy are the x and y components of the cue vector
var distX = mx - cue.x;
var distY = my - cue.y;

// h is the distance of the cue pull
var h = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));

// If the cue is being pulled further than the maxCuePull
if (h>maxCuePull){
// scaleMultiplier is the amount that the x and y coordinates must scale down by to make the pull equal the maxCuePull
var scaleMultiplier = maxCuePull/h;
// Scale the x and y components of the pull down
distX = distX*scaleMultiplier;
distY = distY*scaleMultiplier;
}

mx = (cue.x+distX);
my = (cue.y+distY);
}
44 changes: 10 additions & 34 deletions balls.js → js/game.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var cue = new Cue(100,100);

var context;
var mx;
var my;
Expand All @@ -6,29 +8,22 @@ var shooting = false;
var maxCuePull = 125;
var cueSpeedMultiplier = 0.05;

function Cue (x, y) {
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.move = false;
this.checkCollision = false;
}
var cue = new Cue(100,100);
var width = 500;
var height = 500;

// var balls = new Array();
// balls[0] = new Ball;

function init(){
context= myCanvas.getContext('2d');
context = myCanvas.getContext('2d');
setInterval(tick,10);
}
function tick(){
draw();
if( cue.x<50 || cue.x>250) cue.vx=-cue.vx;
if( cue.y<50 || cue.y>250) cue.vy=-cue.vy;
cue.x+=cue.vx;
cue.y+=cue.vy;
cue.move();
}
function draw(){
context.clearRect(0,0, 500,500);
context.clearRect(0,0, width,height);

if (shooting) {
context.strokeStyle = '#f00';
Expand All @@ -55,26 +50,7 @@ function dist(x, y, X, Y){
if (dist1<0) {dist1 = -dist1;}
return dist1;
}
function calculateCuePull(){
// dx and dy are the x and y components of the cue vector
var distX = mx - cue.x;
var distY = my - cue.y;

// h is the distance of the cue pull
var h = Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));

// If the cue is being pulled further than the maxCuePull
if (h>maxCuePull){
// scaleMultiplier is the amount that the x and y coordinates must scale down by to make the pull equal the maxCuePull
var scaleMultiplier = maxCuePull/h;
// Scale the x and y components of the pull down
distX = distX*scaleMultiplier;
distY = distY*scaleMultiplier;
}

mx = (cue.x+distX);
my = (cue.y+distY);
}
$('canvas').mousedown(function(e){
mx = e.pageX - offset.left
my = e.pageY - offset.top
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 0677857

Please sign in to comment.