Skip to content

Commit 304901f

Browse files
committed
Run Scotty
Browser-based JavaScript game featuring the beloved mascot of CMU running from a bunch of crazy MIT students. Coded with Billy Zhu, Teddy Ding and Reid Long @ HackCMU
1 parent d4f2183 commit 304901f

31 files changed

+997
-0
lines changed

AIInterface.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Highest Level
2+
function think(grid, v, dt) {
3+
//Move Triangles
4+
moveTriangles(grid, v, dt);
5+
6+
7+
}
8+
9+
function moveTriangles (grid, v, dt) {
10+
//for each Triangle in grid calculate new location
11+
var triangles = grid.getAllTriangles();
12+
for(var i = 0; i < triangles.length; i++) {
13+
processTriangle(grid, triangles[i],v,dt);
14+
}
15+
}
16+
17+
18+
19+

AIInterfaceTest.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
5+
<p id="output"></p>
6+
7+
<script src="Actors.js"></script>
8+
9+
<script>
10+
11+
console.log("Running code");
12+
var grid = new Grid();
13+
grid.add_actor(new Actors(10, 10, 10, 10, 42));
14+
grid.add_actor(new Pitt_Student(40, 40, 10, 10, 15122));
15+
var triangles = grid.getAllTriangles();
16+
17+
document.getElementById("output").innerHTML = "Triangles: " + triangles;
18+
19+
</script>
20+
21+
</body>
22+
</html>

Actor.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function Actor(x,y,height,width,id){
2+
this.x=x;
3+
this.y=y;
4+
this.id=id;
5+
this.height=height;
6+
this.width=width;
7+
}
8+
9+
Actor.prototype.get_x=function(){
10+
return this.x;
11+
}
12+
13+
Actor.prototype.get_y=function(){
14+
return this.y;
15+
}
16+
17+
Actor.prototype.get_id=function(){
18+
return this.id;
19+
}
20+
21+
Actor.prototype.get_height=function(){
22+
return this.height;
23+
}
24+
25+
Actor.prototype.get_width=function(){
26+
return this.width;
27+
}
28+
29+
Actor.prototype.move=function(a,b){
30+
if(a >= 0 && a <= windowWidth - this.width) {
31+
this.x=a;
32+
}
33+
if(b >= 0 && b <= windowHeight - this.height) {
34+
this.y=b;
35+
}
36+
}
37+
38+
Actor.prototype.draw=function(){}

Actors.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
function Actors(x,y,height,width,id){
2+
this.x=x;
3+
this.y=y;
4+
this.id=id;
5+
this.height=height;
6+
this.width=width;
7+
}
8+
9+
Actors.prototype.get_x=function(){
10+
return this.x;
11+
}
12+
13+
Actors.prototype.get_y=function(){
14+
return this.y;
15+
}
16+
17+
Actors.prototype.get_id=function(){
18+
return this.id;
19+
}
20+
21+
Actors.prototype.get_height=function(){
22+
return this.height;
23+
}
24+
25+
Actors.prototype.get_width=function(){
26+
return this.width;
27+
}
28+
29+
Actors.prototype.move=function(a,b){
30+
this.x=a;
31+
this.y=b
32+
}
33+
34+
Actors.prototype.draw=function(){
35+
36+
}
37+
38+
function Pitt_Student(x,y,height,width,id){
39+
Actors.call(this,x,y,height,width,id);
40+
}
41+
42+
Pitt_Student.prototype=Object.create(Actors.prototype);
43+
Pitt_Student.prototype.construtor = Pitt_Student;
44+
45+
Pitt_Student.prototype.draw=function(context){
46+
context.beginPath();
47+
context.moveTo(this.x, this.y + this.height);
48+
context.lineTo(this.x + this.width / 2, this.y);
49+
context.lineTo(this.x + this.width, this.y + this.height);
50+
context.closePath();
51+
context.fill();
52+
}
53+
54+
function Scotty(x,y,height,width,id){
55+
Actors.call(this,x,y,height,width,id);
56+
}
57+
58+
Scotty.prototype=Object.create(Actors.prototype);
59+
Scotty.prototype.constructor=Scotty;
60+
61+
Scotty.prototype.draw=function(context){
62+
context.beginPath();
63+
context.arc(this.x + this.width / 2, this.y + this.height / 2, this.height / 2, 0, 2 * Math.PI, true);
64+
context.closePath();
65+
context.fill();
66+
}
67+
68+
function Fence(x,y,height,width,id){
69+
Actors.call(this,x,y,height,width,id);
70+
}
71+
72+
Fence.prototype=Object.create(Actors.prototype);
73+
Fence.prototype.constructor=Fence;
74+
75+
Fence.prototype.draw=function(context){
76+
context.fillRect(this.x, this.y, this.width, this.height);
77+
}
78+
79+
function Grid(){
80+
var grid=new Array();
81+
this.grid=grid;
82+
}
83+
84+
Grid.prototype.add_actor=function(a){
85+
this.grid.push(a);
86+
}
87+
88+
Grid.prototype.rmv_actor=function(a){
89+
for(var i=0;i<grid.length;i++){
90+
if (this.grid[i].get_id == a.get_id){
91+
array.splice(i,1);
92+
}
93+
}
94+
}
95+
96+
Grid.prototype.getAllTriangles = function() {
97+
var output = new Array();
98+
for(var i = 0; i<this.grid.length; i++) {
99+
if(this.grid[i].construtor === Pitt_Student) {
100+
output.push(this.grid[i]);
101+
}
102+
}
103+
return output;
104+
}
105+

Circle.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function Circle(x,y,height,width,id){
2+
Actor.call(this,x,y,height,width,id);
3+
}
4+
5+
Circle.prototype=Object.create(Actor.prototype);
6+
Circle.prototype.constructor=Circle;
7+
8+
Circle.prototype.draw=function(context){
9+
// context.beginPath();
10+
// context.arc(this.x + this.width / 2, this.y + this.height / 2, this.height / 2, 0, 2 * Math.PI, true);
11+
// context.closePath();
12+
// context.fill();
13+
14+
context.drawImage(dog, this.x, this.y, this.width, this.height);
15+
}
16+
17+
Circle.prototype.move=function(a,b){
18+
// checks for collision with squares
19+
var squares = myGrid.getAllSquares();
20+
var okx = true;
21+
var oky = true;
22+
23+
if(a < 0 || a > windowWidth - this.width) {
24+
okx = false;
25+
}
26+
if(b < 0 || b > windowHeight - this.height) {
27+
oky = false
28+
}
29+
30+
for(var i = 0; i < squares.length; i++) {
31+
if(didCollide(a, this.y, squares[i].get_x(), squares[i].get_y(), 40)) {
32+
okx = false;
33+
}
34+
if(didCollide(this.x, b, squares[i].get_x(), squares[i].get_y(), 40)) {
35+
oky = false;
36+
}
37+
}
38+
39+
if(okx) {
40+
this.x = a;
41+
}
42+
if(oky) {
43+
this.y = b;
44+
}
45+
}

Collision.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function didCollideWithAnyPoint(x1, y1, listOfPoints, r) {
2+
for (var i = listOfPoints.length - 1; i >= 0; i--) {
3+
var x2 = listOfPoints[i].get_x();
4+
var y2 = listOfPoints[i].get_y();
5+
if(didCollide(x1, y1, x2, y2, r)) {
6+
console.log("Collision with: " + x1 + "-" + y1 + " :: "+ x2 + "-" + y2 + " :: " + r);
7+
return true;
8+
}
9+
}
10+
return false;
11+
}
12+
13+
function didCollide( x1, y1, x2, y2, sideLength) {
14+
var body1 = makeBody(x1, y1, sideLength);
15+
var body2 = makeBody(x2, y2, sideLength);
16+
var LEFT = 0;
17+
var RIGHT = 1;
18+
var TOP = 2;
19+
var BOT = 3;
20+
21+
22+
//var xCollide = body1[LEFT] < body2[LEFT] && body2[LEFT] < body1[RIGHT] || body2[LEFT] < body1[LEFT] && body1[LEFT] < body2[RIGHT];
23+
//var yCollide = body1[TOP] < body2[TOP] && body2[TOP] < body1[BOT] || body2[TOP] < body1[TOP] && body1[TOP] < body2[BOT];
24+
25+
var dH = Math.max(body1[TOP], body1[BOT], body2[TOP], body2[BOT]) - Math.min(body1[TOP], body1[BOT], body2[TOP], body2[BOT]);
26+
var dW = Math.max(body1[LEFT], body1[RIGHT], body2[LEFT], body2[RIGHT]) - Math.min(body1[LEFT], body1[RIGHT], body2[LEFT], body2[RIGHT]);
27+
28+
var yCollide = dH < 2 * sideLength;
29+
var xCollide = dW < 2 * sideLength;
30+
31+
return xCollide && yCollide;
32+
}
33+
34+
35+
function makeBody (x, y, sideLength) {
36+
return [x, x + sideLength, y , y + sideLength];
37+
}

CollisionJS.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
<!DOCTYPE html>
3+
<html>
4+
<body>
5+
6+
7+
8+
<p id="results"></p>
9+
10+
<p id="listResults"></p>
11+
12+
<script src="Actors.js"></script>
13+
<script src="Collision.js"></script>
14+
15+
<script>
16+
17+
var LEFT = 0;
18+
var RIGHT = 1;
19+
var TOP = 2;
20+
var BOT = 3;
21+
22+
23+
var result = new String();
24+
var data = [10, 10, 15, 5, 7];
25+
result += testCollision(data) + "<br>";
26+
data = [100, 100, 50, 50, 7];
27+
result += testCollision(data) + "<br>";
28+
29+
document.getElementById("results").innerHTML = result;
30+
31+
var point = [10, 10];
32+
var radius = 7;
33+
var testPoints = [[100, 100], [150, 150], [10,20], [25, 10], [15, 30]];
34+
35+
var listResult = new String();
36+
listResult += testCollisionList(point, testPoints, radius) + "<br>";
37+
testPoints.push([5, 5]);
38+
listResult += testCollisionList(point, testPoints, radius) + "<br>";
39+
40+
document.getElementById("listResults").innerHTML = listResult;
41+
42+
43+
function testCollisionList(point, list, r) {
44+
return list.toString() + " " + didCollideWithAnyPoint(point[0], point[1], list, r);
45+
}
46+
47+
function testCollision(data) {
48+
return data.toString() + " "+ didCollide(data[0], data[1], data[2], data[3], data[4]);
49+
}
50+
51+
52+
53+
54+
55+
</script>
56+
</body>
57+
</html>

CollisionTest.class

1.01 KB
Binary file not shown.

CollisionTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import static java.lang.System.*;
2+
3+
public class CollisionTest {
4+
public static void main(String[] args) {
5+
//Write Unit Tests
6+
int x1 = 10;
7+
int y1 = 10;
8+
int x2 = 15;
9+
int y2 = 5;
10+
int radius = 7;
11+
out.println(didCollide(x1, y1, x2, y2, radius));
12+
out.println(didCollide(x2, y2, x1, y1, radius));
13+
x1 = 100;
14+
y1 = 100;
15+
x2 = 50;
16+
y2 = 50;
17+
out.println(didCollide(x1, y1, x2, y2, radius));
18+
out.println(didCollide(x2, y2, x1, y1, radius));
19+
20+
}
21+
public static boolean didCollide(int x1, int y1, int x2, int y2, int r) {
22+
int left1 = x1 - r;
23+
int right1 = x1 + r;
24+
int top1 = y1 + r;
25+
int bot1 = y1 - r;
26+
27+
int left2 = x2 - r;
28+
int right2 = x2 + r;
29+
int top2 = y2 + r;
30+
int bot2 = y2 - r;
31+
32+
boolean xCollide = left1 < left2 && left2 < right1 || left2 < left1 && left1 < right2;
33+
34+
boolean yCollide = top1 > top2 && top2 > bot1 || top2 > top1 && top1 > bot2;
35+
36+
out.printf("%b - %b", xCollide, yCollide);
37+
return xCollide && yCollide;
38+
}
39+
}

0 commit comments

Comments
 (0)