-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
143 lines (120 loc) · 3.03 KB
/
main.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use strict";
var m,
bg,
input,
mouse,
game,
menu,
snake,
food,
portal,
canvas,
ctx;
function setup() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
m = new measureObject();
bg = new backgroundObject();
input = new inputObject();
mouse = new mouseObject();
game = new gameObject();
menu = new menuObject();
game.start();
}
function measureObject() {
this.cell = 32;
this.window = [window.innerWidth,
window.innerHeight];
this.grid = [Math.floor(this.window[0] / this.cell),
Math.floor(this.window[1] / this.cell)];
this.gmin = Math.min(this.grid[0],this.grid[1]);
this.gmax = Math.max(this.grid[0],this.grid[1]);
this.pixel = [this.grid[0] * this.cell,
this.grid[1] * this.cell];
this.border = [Math.floor((this.window[0] % this.cell) / 2),
Math.floor((this.window[1] % this.cell) / 2)];
this.center = [this.window[0] / 2,
this.window[1] / 2];
}
function gameObject() {
this.paused = false;
this.pausedBy;
this.pauseText = "PAUSED";
this.start = function() {
this.create();
this.draw();
}
this.create = function() {
snake = new snakeObject();
portal = new portalObject();
food = new foodObject();
food.move();
}
this.pause = function(flag) {
this.paused = !this.paused;
switch (this.paused) {
case true:
menu.draw(m.center[0], m.center[1], this.pauseText, m.pixel[1]/8);
break;
case false:
if (!snake) this.create();
this.draw();
break;
}
}
this.draw = function() {
bg.draw();
food.draw();
snake.draw();
portal.draw();
}
}
function menuObject() {
this.draw = function(x,y,string,height) {
var half = height/2,
width = string.length*(half),
xcorner = x - (width/2),
ycorner = y - (height/2);
//bg.draw();
this.box(xcorner, ycorner, width, height);
this.text(x, y, string, half);
}
this.box = function(x,y,width,height) {
ctx.strokeStyle = toRGBA(settings.color.menu.stroke);
ctx.lineWidth = height/10;
ctx.lineJoin = "round";
ctx.fillStyle = toRGBA(settings.color.menu.fill);
ctx.fillRect(x, y, width, height);
ctx.strokeRect(x, y, width, height);
}
this.text = function (x,y,string,size) {
ctx.fillStyle = toRGBA(settings.color.menu.text);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = size + "px Courior New";
ctx.fillText(string,x,y);
}
}
function backgroundObject() {
this.draw = function() {
ctx.fillStyle = toRGBA(settings.color.game.stroke);
ctx.fillRect(0,0,m.window[0],m.window[1]);
ctx.fillStyle = toRGBA(settings.color.game.fill);
ctx.fillRect(m.border[0], m.border[1], m.pixel[0], m.pixel[1]);
}
}
function getRandomCell() {
var x = Math.floor(Math.random() * m.grid[0]),
y = Math.floor(Math.random() * m.grid[1])
return new cellObject([x, y]);
}
function toRGBA(array) {
var output = "rgba(";
for (var i=0;i<3;i++) {
output += array[i] + ",";
}
output += array[3] + ")";
return output;
}