-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
104 lines (83 loc) · 2.81 KB
/
draw.js
File metadata and controls
104 lines (83 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function init_draw(myGrid, context) {
TimeControl(myGrid, context);
}
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function TimeControl(myGrid, context) {
// var squares = myGrid.getAllSquares();
// for(var i = 0; i < squares.length; i++) {
// squares[i].dropTime();
// }
var allBehind = true;
refresh(context);
if(((circle.get_x() + circle.get_width()) >= (canvas.width - 10)) && ((circle.get_y() + circle.get_height()) >= (canvas.height / 2 - 28)) && ((circle.get_y()) <= (canvas.height / 2 + 28))) {
console.log("wins");
done = true;
win(context);
}
var triangles = myGrid.getAllTriangles();
for(var i = 0; i < triangles.length; i++) {
if(didCollide(circle.get_x(), circle.get_y(), triangles[i].get_x(), triangles[i].get_y(), 40)) {
done = true;
fail(context);
}
if(!triangles[i].behind) {
allBehind = false;
}
}
if(allBehind) {
think(myGrid, 7, 1);
} else {
if(circle.get_x() > canvas.width / 4) {
think(myGrid, 6, 1);
} else {
think(myGrid, 3, 1);
}
}
if(!done) {
requestAnimFrame(function() {
TimeControl(myGrid, context);
});
}
}
function refresh(context) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.globalAlpha = 0.6;
ctx.drawImage(bg, 0, 0, canvas.width, canvas.height);
context.globalAlpha = 1;
var randomR = randomColor();
var randomG = randomColor();
var randomB = randomColor();
var goalString = "rgb(" + randomR + ", " + randomG + ", " + randomB + ")";
context.fillStyle = goalString;
context.fillRect(canvas.width - 10, canvas.height / 2 - 30, 10, 60);
for(var i = 0; i < myGrid.grid.length; i++) {
myGrid.grid[i].draw(context);
}
// console.log("refreshing...");
}
function win(context) {
context.fillStyle = "rgba(0,0,0,0.6)";
context.fillRect(0,0,canvas.width,canvas.height);
context.fillStyle = "rgba(255,255,255,1)";
context.font="50px Georgia";
context.fillText("Congratulations!",canvas.width / 2 - 200, 300);
context.font="20px Georgia";
context.fillText("Press [Enter] to Restart",canvas.width / 2 - 200, canvas.height - 50);
}
function fail(context) {
context.fillStyle = "rgba(0,0,0,0.6)";
context.fillRect(0,0,canvas.width,canvas.height);
context.fillStyle = "rgba(255,255,255,1)";
context.font="50px Georgia";
context.fillText("Oops, You Lost!",canvas.width / 2 - 200, 300);
context.font="20px Georgia";
context.fillText("Press [Enter] to Restart",canvas.width / 2 - 200, canvas.height - 50);
}
function randomColor() {
return Math.floor(Math.random() * 256);
}