云梦引

Codream's Homepage

Conway's Game of Life for HTML5 Canvas Conway's Game of Life for HTML5 Canvas Conway's Game of Life for HTML5 Canvas Conway's Game of Life for HTML5 Canvas Conway's Game of Life for HTML5 Canvas Conway's Game of Life for HTML5 Canvas
阅读全文 »

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

Snake

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var FPS = 10;
var gLoop;
var lastX;
var lastY;

var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
var info = document.getElementById('i');
var wrapper = document.getElementById('w');
var classic = document.getElementById('classic');

canvas.width = 500;
canvas.height = 300;
classic.addEventListener('click', init, false);
wrapper.style.width = canvas.width + 'px';
info.style.width = canvas.width + 'px';

document.onkeydown = function(e) {
snake.keyDown(e);
};

function getRandomInt(min, max) {
return ~~(Math.random() * (max - min + 1)) + min;
}

function drawBox(x, y, color) {
ctx.fillStyle = color;
ctx.fillRect(x + 1, y + 1, 8, 8);
}

function clear() {
ctx.fillStyle = '#eee';
ctx.beginPath();
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.closePath();
ctx.fill();

function grid(increment, color) {
ctx.fillStyle = color;
for (var i = 0; i < canvas.width + 1; i += increment) {
ctx.fillRect(i - 1, 0, 2, canvas.height);
}
for (var i = 0; i < canvas.height + 1; i += increment) {
ctx.fillRect(0, i - 1, canvas.width, 2);
}
}
grid(10, '#ddd');
grid(100, '#ccc');
}

var food = new function() {
this.x = 10 * getRandomInt(0, (canvas.width - 10) / 10);
this.y = 10 * getRandomInt(0, (canvas.height - 10) / 10);
this.color = '#87af00';
this.draw = function() {
drawBox(this.x, this.y, this.color);
};
this.eaten = function() {
this.x = 10 * getRandomInt(0, (canvas.width - 10) / 10);
this.y = 10 * getRandomInt(0, (canvas.height - 10) / 10);
};
}

var snake = new function() {
this.x = canvas.width / 2;
this.y = canvas.height / 2;
this.direction = '';
this.color = '#268bd2';
this.tail = [];
this.dead = true;

this.eat = function() {
food.eaten();
this.tail.unshift([this.x, this.y]);
};

this.keyDown = function(e) {
if (this.dead) {
return;
}
switch (e.keyCode) {
case 38: // Up
e.preventDefault();
if (this.direction != 'down') {
this.direction = 'up'
}
break
case 40: // Down
e.preventDefault();
if (this.direction != 'up') {
this.direction = 'down'
}
break
case 37: // Left
e.preventDefault();
if (this.direction != 'right') {
this.direction = 'left'
}
break
case 39: // Right
e.preventDefault();
if (this.direction != 'left') {
this.direction = 'right'
}
break
}
};
this.update = function() {
if (!this.dead) {
if (this.direction == 'up') {
this.y -= 10;
}
if (this.direction == 'down') {
this.y += 10;
}
if (this.direction == 'left') {
this.x -= 10;
}
if (this.direction == 'right') {
this.x += 10;
}
} else {
this.direction = '';
}

// Colision detection
if ((this.x == food.x) && (this.y == food.y)) {
this.eat();
}

for (var i = 1; i < this.tail.length; i++) {
if ((this.x == this.tail[i][0]) && (this.y == this.tail[i][1])) {
this.dead = true;
}
}
if (this.x > canvas.width - 10 ||
this.y > canvas.height - 10 ||
this.x < 0 ||
this.y < 0) {
this.dead = true;
}

};
this.draw = function() {
drawBox(this.x, this.y, this.color);
for (var i = 0; i < this.tail.length; i++) {
drawBox(this.tail[i][0], this.tail[i][1], this.color);
}

if (!this.dead) {
lastX = this.x;
lastY = this.y;
this.tail.unshift([lastX, lastY]);
this.tail.pop();
}
};
}

function gameLoop() {
// Update
snake.update();

// Draw
clear();
food.draw();
snake.draw();

// Loop
gLoop = setTimeout(gameLoop, 1000 / FPS);
}

function init() {
info.style.display = 'none';
snake.dead = false;
gameLoop();
}
clear();

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

0%