mirror of
https://github.com/icecoder/ICEcoder.git
synced 2026-03-03 07:13:59 +01:00
Remove Snake game
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 1.5 KiB |
@@ -4599,44 +4599,6 @@ var ICEcoder = {
|
||||
// Reset the auto-logout timer
|
||||
this.resetAutoLogoutTimer();
|
||||
|
||||
// Detect if we type s,n,a,k,e keys with content saved, if so start snake game
|
||||
if (!this.last5Keys) {this.last5Keys = [];}
|
||||
this.last5Keys.push(key);
|
||||
if (this.last5Keys.length == 6) {
|
||||
this.last5Keys.shift();
|
||||
}
|
||||
if (this.last5Keys.join() == "83,78,65,75,69") {
|
||||
setTimeout(function(ic) {
|
||||
// Undo back to pre 'snake' word
|
||||
cM = ic.getcMInstance();
|
||||
var undoCounts = 0;
|
||||
var startCG = cM.changeGeneration();
|
||||
while (cM.changeGeneration() > startCG-5) {
|
||||
cM.undo();
|
||||
undoCounts++;
|
||||
}
|
||||
// If we have content saved
|
||||
if (ic.savedPoints[ic.selectedTab-1] == cM.changeGeneration()) {
|
||||
// Start snake game
|
||||
ic.startSnake();
|
||||
// If we don't, redo snake word
|
||||
} else {
|
||||
for (var i=1; i<=undoCounts; i++) {
|
||||
cM.redo();
|
||||
}
|
||||
}
|
||||
},0,this);
|
||||
}
|
||||
|
||||
// Detect arrow keys if playing snake
|
||||
if (this.snakePlaying) {
|
||||
if (key==37) {this.snakeDir = 'left'}
|
||||
if (key==39) {this.snakeDir = 'right'}
|
||||
if (key==38) {this.snakeDir = 'up'}
|
||||
if (key==40) {this.snakeDir = 'down'}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mac command key handling (224 = Moz, 91/93 = Webkit Left/Right Apple)
|
||||
if (key==224 || key==91 || key==93) {
|
||||
this.cmdKey = true;
|
||||
@@ -5181,143 +5143,4 @@ var ICEcoder = {
|
||||
get("infoMessage").innerHTML = '<div class="title">' + steps[step].title + '</div>' + steps[step].message + '<br><br><div class="button" onclick="ICEcoder.viewTutorial(' + (step + 1) + ')">' + steps[step].button + '</div>';
|
||||
},
|
||||
|
||||
// Snart snake
|
||||
startSnake: function() {
|
||||
this.snakePlaying = true;
|
||||
this.showHide('show',get('blackMask'));
|
||||
get('mediaContainer').innerHTML = '<span style="font-size: 14px">Let\'s play<br><img src="'+this.iceLoc+'/assets/images/snake.png" alt="snake"><br><br><br>Use arrow keys to eat your code<br><br>(it returns afterwards of course) :-)</span>';
|
||||
setTimeout(function(ic) {
|
||||
ic.showHide('hide',get('blackMask'));
|
||||
get('mediaContainer').innerHTML = '';
|
||||
ic.playSnake();
|
||||
},2000,this);
|
||||
},
|
||||
|
||||
// Play snake
|
||||
playSnake: function() {
|
||||
var cM;
|
||||
|
||||
cM = this.getcMInstance();
|
||||
cM.setOption('readOnly', 'nocursor');
|
||||
cM.focus();
|
||||
|
||||
// Get state of editor at present
|
||||
this.snakePreHistory = cM.getHistory();
|
||||
this.snakePreContent = cM.getValue();
|
||||
this.snakePreCursor = cM.getCursor();
|
||||
|
||||
// Pick a random point for snake to come in and set head and 4 body parts off screen
|
||||
var randPos = Math.floor(Math.random()*50);
|
||||
this.snakePos = [
|
||||
[randPos,0],
|
||||
[randPos,-1],
|
||||
[randPos,-2],
|
||||
[randPos,-3],
|
||||
[randPos,-4]
|
||||
];
|
||||
|
||||
// Show game layer, set direction and do 1st frame of snake
|
||||
this.content.contentWindow.document.getElementById('game').style.display = 'block';
|
||||
this.snakeDir = "down";
|
||||
this.doSnake();
|
||||
|
||||
// Every 0.1s, move snake
|
||||
this.snakeInt = setInterval(function(ic) {
|
||||
// Set new head X & Y pos according to direction
|
||||
var newHead = [];
|
||||
newHead[0] = ic.snakePos[0][0]+(ic.snakeDir == "right" ? 1 : ic.snakeDir == "left" ? -1 : 0);
|
||||
newHead[1] = ic.snakePos[0][1]+(ic.snakeDir == "down" ? 1 : ic.snakeDir == "up" ? -1 : 0);
|
||||
// Add new head and remove tail
|
||||
ic.snakePos.unshift(newHead);
|
||||
ic.snakePos.pop();
|
||||
// Do next frame of snake
|
||||
ic.doSnake();
|
||||
},100,this);
|
||||
},
|
||||
|
||||
doSnake: function() {
|
||||
var cM, cW, cH, newInnerHTML, lineData, lineContent, spaceReplaceChars, collision, scrollInfo;
|
||||
|
||||
// Get CodeMirror instance, plus char width and height
|
||||
cM = this.getcMInstance();
|
||||
cW = cM.defaultCharWidth();
|
||||
cH = cM.defaultTextHeight();
|
||||
|
||||
// Clear content of game layer
|
||||
this.content.contentWindow.document.getElementById('game').innerHTML = "";
|
||||
// Start a new set of contents
|
||||
newInnerHTML = "";
|
||||
// For every part of snake, draw it's block in position
|
||||
for (var i=0; i<this.snakePos.length; i++) {
|
||||
newInnerHTML += '<div style="position: absolute; diplay: inline-block; width: '+cW+'px; height: '+cH+'px; top: '+((this.snakePos[i][1]*cH)+4)+'px; left: '+((this.snakePos[i][0]*cW)+60)+'px; background: #fff"></div>';
|
||||
}
|
||||
// Set new content in game layer
|
||||
this.content.contentWindow.document.getElementById('game').innerHTML = newInnerHTML;
|
||||
|
||||
// Get line & ch value under snake head then line content
|
||||
lineData = cM.coordsChar({top: ((this.snakePos[0][1]*cH)+4), left: ((this.snakePos[0][0]*cW)+60)});
|
||||
lineContent = cM.getLine(lineData.line);
|
||||
|
||||
// If not the last char on the line
|
||||
if (this.snakePos[0][0]-1 <= lineContent.length-2) {
|
||||
spaceReplaceChars = "";
|
||||
// If char under snake head is a tab, replace string contains spaces of same width
|
||||
if (lineContent.substr(lineData.ch,1) === "\t") {
|
||||
for (var i=0; i<cM.getOption('tabSize'); i++) {
|
||||
spaceReplaceChars += " ";
|
||||
}
|
||||
// Else replace string is a single space
|
||||
} else {
|
||||
spaceReplaceChars = " ";
|
||||
}
|
||||
// Push a duplicate of tail onto end, to increase snake length by 1 block
|
||||
this.snakePos.push([this.snakePos[this.snakePos.length-1][0],this.snakePos[this.snakePos.length-1][1]]);
|
||||
// Replace char under head with nothing if end of line, else with our replacement string
|
||||
cM.doc.replaceRange(this.snakePos[0][0]-1 == lineContent.length-2 ? "" : spaceReplaceChars,lineData,{line: lineData.line, ch: lineData.ch+1}, "+input");
|
||||
// Remove any trailing space at end
|
||||
if (this.snakePos[0][0]-1 == lineContent.length-2) {
|
||||
cM.doc.replaceRange(cM.getLine(lineData.line).replace(/[ \t]+$/,''),{line: lineData.line, ch: 0},{line: lineData.line, ch: 1000000}, "+input");
|
||||
}
|
||||
} else {
|
||||
// Reduce snake length if over 5 chars and not on content
|
||||
if (this.snakePos.length >= 5) {
|
||||
this.snakePos.pop();
|
||||
}
|
||||
}
|
||||
// Detect if snake head has collided into itself
|
||||
collision = false;
|
||||
for (var i=1; i<this.snakePos.length; i++) {
|
||||
if (this.snakePos[i][0] == this.snakePos[0][0] && this.snakePos[i][1] == this.snakePos[0][1]) {
|
||||
collision = true;
|
||||
}
|
||||
}
|
||||
// Get scroll info to get width and height of editor area shown
|
||||
scrollInfo = cM.getScrollInfo();
|
||||
if (
|
||||
// If snake out of bounds or a collision, game over!
|
||||
this.snakePos[0][0] < 0 || this.snakePos[0][1] < 0 ||
|
||||
((this.snakePos[0][0]*cW)+60) > scrollInfo.clientWidth || ((this.snakePos[0][1]*cH)+4) > scrollInfo.clientHeight ||
|
||||
collision
|
||||
) {
|
||||
// Clear interval and hide game layer
|
||||
clearInterval(this.snakeInt);
|
||||
this.content.contentWindow.document.getElementById('game').style.display = 'none';
|
||||
// Set content, saved point, saved contents and history back to what they were pre game
|
||||
cM.setValue(this.snakePreContent);
|
||||
this.savedPoints[this.selectedTab-1] = cM.changeGeneration();
|
||||
this.savedContents[this.selectedTab-1] = this.snakePreContent;
|
||||
cM.setHistory(this.snakePreHistory);
|
||||
// Redo changes indicator in title tag and tab highlight save indicator also to what they are now (pre game state)
|
||||
this.indicateChanges();
|
||||
this.redoTabHighlight(this.selectedTab);
|
||||
// Set editor to be editable again
|
||||
cM.setOption('readOnly', false);
|
||||
// Set cursor back to what it was pre game and focus on editor
|
||||
cM.setCursor(this.snakePreCursor);
|
||||
cM.focus();
|
||||
// State we are no longer playing snake
|
||||
this.snakePlaying = false;
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user