JavaScript Snake のフルバージョンは完全にアノテーションが付けられており、オブジェクト指向です
次のようにコードをコピーします。
<html>
<頭>
<title>スネーク v2.4</title>
<スタイル>
体{
フォントサイズ:9pt;
}
テーブル{
境界崩壊: 崩壊;
ボーダー:ソリッド #333 1px;
}
td{
高さ: 10ピクセル;
幅: 10px;
フォントサイズ: 0px;
}
.filled{
背景色:青;
}
</スタイル>
</head>
<スクリプト>
関数 $(id){return document.getElementById(id);}
/************************************************ ***** *************
* JavaScript スネーク v2.4<br />
* v2.4 は、ヘビが前進するにつれてヘビの体の色が動くように修正しました。
************************************************* * ************/
//貪欲なヘビ
var スネーク = {
表: null、
/**
* body: スネーク本体、スネークの各セクションが配列に配置され、
* データ構造 {x:x0, y:y0, color:color0},
※x、yは座標、colorは色を表します
**/
体: []、
//現在の移動方向、値 0、1、2、3 はそれぞれ上、右、下、左を表し、キーボードの方向キーを押して変更します
方向: 0、
// タイマー
タイマー: null、
//スピード
スピード: 250、
//一時停止されているかどうか
一時停止: true、
//行数
行数: 30、
//列数
列数: 30、
//初期化
初期化: function(){
var Colors = ['red','orange',' yellow','green','blue','purple','#ccc'];
this.tbl = $("メイン");
var x = 0;
変数y = 0;
var colorIndex = 0;
//初期移動方向を生成
this.direction = Math.floor(Math.random()*4);
//テーブルを構築する
for(var row=0;row<this.rowCount;row++){
var tr=this.tbl.insertRow(-1);
for(var col=0;col<this.colCount;col++) {
var td=tr.insertCell(-1);
}
}
// 20 個のルース ノードを生成する
for(var i=0; i<10; i++){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = color[colorIndex];
}
}
//スネークヘッドを生成する
while(true){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = "黒";
this.body.push({x:x,y:y,color:'black'});
壊す;
}
}
this.paused = true;
//キーボードイベントを追加
document.onkeydown= function(e){
if (!e)e=window.event;
switch(e.keyCode | e.that | e.charCode){
ケース 13: {
if(スネーク.一時停止){
スネーク.move();
Snake.paused = false;
}
それ以外{
//一時停止がない場合は移動を停止します
Snake.pause();
Snake.paused = true;
}
壊す;
}
ケース 37:{//左
//ヘビが後ろ向きに歩くのを止めます
if(Snake.direction==1){
壊す;
}
Snake.direction = 3;
壊す;
}
ケース 38:{//上
//ここではショートカットキーが機能します
if(event.ctrlKey){
Snake.speedUp(-20);
壊す;
}
if(Snake.direction==2){//ヘビが後ろ向きに歩くのを防ぎます
壊す;
}
Snake.direction = 0;
壊す;
}
ケース 39:{//右
if(Snake.direction==3){//ヘビが後ろ向きに歩くのを防ぎます
壊す;
}
Snake.direction = 1;
壊す;
}
ケース 40:{//下
if(event.ctrlKey){
Snake.speedUp(20);
壊す;
}
if(Snake.direction==0){//ヘビが後ろ向きに歩くのを防ぎます
壊す;
}
Snake.direction = 2;
壊す;
}
}
}
}、
//動く
移動: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
スネークペイント();
}, this.speed);
}、
//体の一部を移動します
moveOneStep: function(){
if(this.checkNextStep()==-1){
clearInterval(this.timer);
alert("ゲームオーバー!/n続行するには再起動を押してください。");
戻る;
}
if(this.checkNextStep()==1){
var _point = this.getNextPos();
var _x = _point.x;
var _y = _point.y;
var _color = this.getColor(_x,_y);
this.body.unshift({x:_x,y:_y,color:_color});
// ある食べ物が食べられると、別の食べ物が生成される
this.generateDood();
戻る;
}
//window.status = this.toString();
var point = this.getNextPos();
//最初のセクションの色を維持します
var color = this.body[0].color;
//色が先に進みます
for(var i=0; i<this.body.length-1; i++){
this.body[i].color = this.body[i+1].color;
}
//ヘビの尻尾から 1 つのセクションを減算し、ヘビの尻尾に 1 つのセクションを追加して、ヘビが前進する効果を示します。
this.body.pop();
this.body.unshift({x:point.x,y:point.y,color:color});
//window.status = this.toString();
}、
// 次にどこに行くかを検討します
一時停止: function(){
clearInterval(Snake.timer);
this.paint();
}、
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
var color = this.body[0].color;
//上
if(this.direction==0){
y--;
}
//右の方へ
else if(this.direction==1){
x++;
}
//下
else if(this.direction==2){
y++;
}
//左
それ以外{
×--;
}
//座標を返す
{x:x,y:y} を返します。
}、
//次に進むべきステップを確認する
checkNextStep: function(){
var point = this.getNextPos();
var x = ポイント.x;
var y = point.y;
if(x<0||x>=this.colCount||y<0||y>=this.rowCount){
return -1;//境界線に触れるとゲーム終了
}
for(var i=0; i<this.body.length; i++){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;//自分の体に触れたらゲーム終了
}
}
if(this.isCellFilled(x,y)){
return 1;//何かあります
}
return 0;//オープンスペース
}、
// ヘビの本体を消去
消去: function(){
for(var i=0; i<this.body.length; i++){
this.eraseDot(this.body[i].x, this.body[i].y);
}
}、
// ヘビの胴体を描画します
ペイント: function(){
for(var i=0; i<this.body.length; i++){
this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color);
}
}、
// セクションを消去
EraseDot: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor = "";
}、
ペイントドット: function(x,y,color){
this.tbl.rows[y].cells[x].style.backgroundColor = カラー;
}、
// 座標の色を取得します
getColor: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor を返します。
}、
//デバッグ用
toString: function(){
var str = "";
for(var i=0; i<this.body.length; i++){
str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - ";
}
文字列を返します。
}、
// 座標点が塗りつぶされているかどうかを確認する
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
false を返します。
}
true を返します。
}、
//再起動
再起動: function(){
if(this.timer){
clearInterval(this.timer);
}
for(var i=0; i<this.rowCount;i++){
this.tbl.deleteRow(0);
}
this.body = [];
this.init();
this.speed = 250;
}、
//加速する
スピードアップ: 関数(時間){
if(!this.paused){
if(this.speed+time<10||this.speed+time>2000){
戻る;
}
this.speed += 時間;
this.pause();
this.move();
}
}、
//食べ物を生産します。
generateDood: function(){
var Colors = ['red','orange',' yellow','green','blue','purple','#ccc'];
var x = Math.floor(Math.random()*this.colCount);
var y = Math.floor(Math.random()*this.rowCount);
var colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = color[colorIndex];
}
}
};
</script>
<body onload="Snake.init();">
/************************************************ ***** ***************<br />
* JavaScript スネーク v2.4<br />
************************************************* * ************/<br />
<table id="main" cellpacing="0" cellpadding="0"></table>
<input type="button" id="btn" value="開始/一時停止" />左ボタンをクリックするか Enter キーを押してゲームを開始/一時停止します<br />
<input type="button" id="reset" value="最初からやり直す" /><br />
<input type="button" id="upSpeed" value="加速" />左ボタンをクリックするか、Ctrl + ↑を押して加速します<br />
<input type="button" id="downSpeed" value="速度を下げる" />速度を下げるには、左ボタンをクリックするか、Ctrl + ↓を押します
<スクリプト>
$('btn').onclick = function(){
if(スネーク.一時停止){
スネーク.move();
Snake.paused = false;
}
それ以外{
Snake.pause();
Snake.paused = true;
}
};
$("リセット").onclick = function(){
スネーク.restart();
this.blur();
};
$("アップスピード").onclick = function(){
Snake.speedUp(-20);
};
$("ダウンスピード").onclick = function(){
Snake.speedUp(20);
};
</script>
</body>
</html>