Skip to content

Commit

Permalink
11.19
Browse files Browse the repository at this point in the history
  • Loading branch information
koodin9 committed Nov 19, 2015
1 parent d5e4f88 commit 2558884
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 44 deletions.
Binary file added 11.19/game-thu.zip
Binary file not shown.
110 changes: 66 additions & 44 deletions 11.19/game-thu/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,99 +6,121 @@ var trapBlock;
var targetTimer;
var trapTimer;
var instantTimer;
var point = 0;

document.observe('dom:loaded', function(){
$("start").observe("click", function(){
console.log(targetBlocks);
$("state").textContent = "Ready!";
$("score").textContent = point;
clearTimeout(targetTimer);
clearTimeout(trapTimer);
$("score").textContent = 0;
clearInterval(targetTimer);
clearInterval(trapTimer);
clearTimeout(instantTimer);
targetBlocks = [];
var allblocks = $$(".block");
for(var i=0;i<numberOfBlocks;i++){
allblocks[i].removeClassName("target");
allblocks[i].removeClassName("trap");
allblocks[i].addClassName("normal");
}
setTimeout(startGame, 3000);
});
$("stop").observe("click", stopGame);
var allblocks = $$("#blocks");
for(var i=0;i<numberOfBlocks;i++){
allblocks[i].observe("click",function(){
if(allblocks[i].hasClassName("normal")){
point = point - 10;
allblocks[i].addClassName("wrong");
setTimeout(afterRemove, 100, i);
} else if (allblocks[i].hasClassName("target")){
point = point + 20;
allblocks[i].removeClassName("target");
} else if (allblocks[i].hasClassName("trap")){
point = point - 30;
allblocks[i].removeClassName("trap");
}
$("score").textContent = point;
});
}
});

function startGame(){
numberOfBlocks = 9;
$("state").textContent = "Ready!";
for(var i=0;i<numberOfBlocks;i++){
$$(".block")[i].stopObserving("click");
}
targetBlocks = [];
trapBlock = 0;
clearTimeout(targetTimer);
clearTimeout(trapTimer);
trapBlock = null;
clearInterval(targetTimer);
clearInterval(trapTimer);
clearTimeout(instantTimer);
startToCatch();
}

function stopGame(){
numberOfBlocks = 9;
targetBlocks = [];
trapBlock = 0;
clearTimeout(targetTimer);
clearTimeout(trapTimer);
for(var i=0;i<numberOfBlocks;i++){
$$(".block")[i].stopObserving("click");
}
clearInterval(targetTimer);
clearInterval(trapTimer);
clearTimeout(instantTimer);
targetBlocks = [];
trapBlock = null;
$("state").textContent = "Stop";
}

function startToCatch(){
$("state").textContent = "Catch!";
targetTimer = setInterval(showTargetBlock, 1000);
trapTimer = setInterval(showTrapBlock, 3000);
var allblocks = $$(".block");
for(var i=0;i<numberOfBlocks;i++){
allblocks[i].observe("click",function(){
var point = parseInt($("score").textContent);
if(this.hasClassName("normal")){
$("score").textContent = point - 10;
this.addClassName("wrong");
setTimeout(afterRemove, 100, this);
} else if (this.hasClassName("target")){
$("score").textContent = point + 20;
this.removeClassName("target");
this.addClassName("normal");
targetBlocks.pop();
} else if (this.hasClassName("trap")){
$("score").textContent = point - 30;
this.removeClassName("trap");
this.addClassName("normal");
}
});
}
}

function showTargetBlock(){
console.log(targetBlocks);
var allblocks = $$(".block");
var randomNum = Math.floor(Math.random() * numberOfBlocks);
while(1){
if(allblocks[randomNum].hasClassName("normal")){
allblocks[randomNum].addClassName("target");
targetBlocks.push(randomNum);
allblocks[randomNum].removeClassName("normal");
allblocks[randomNum].addClassName("target");
break;
} else {
randomNum = Math.floor(Math.random() * numberOfBlocks);
}
}

if(targetBlocks.length > 4){
clearTimeout(targetTimer);
clearTimeout(trapTimer);
clearTimeout(instantTimer);
alert("you Lose!");
for(var i=0;i<numberOfBlocks;i++){
allblocks[i].stopObserving("click");
}
stopGame();
}
}

function showTrapBlock(){
var allblocks = $$(".block");
var randomNum = Math.floor(Math.random() * numberOfBlocks);
targetBlocks.push(randomNum);
allblocks[randomNum].addClassName("trap");
setTimeout(reset, 2000, randomNum);
while(1){
if(allblocks[randomNum].hasClassName("normal")){
trapBlock = randomNum;
allblocks[randomNum].addClassName("trap");
allblocks[randomNum].removeClassName("normal");
break;
} else {
randomNum = Math.floor(Math.random() * numberOfBlocks);
}
}
instantTimer = setTimeout(reset, 2000);
}
function reset(num) {
function reset() {
var allblocks = $$(".block");
allblocks[num].removeClassName("wrong");
allblocks[trapBlock].removeClassName("trap");
allblocks[trapBlock].addClassName("normal");
}
function afterRemove(num) {
function afterRemove(victim) {
var allblocks = $$(".block");
allblocks[num].removeClassName("wrong");
victim.removeClassName("wrong");
victim.addClassName("normal");
}

0 comments on commit 2558884

Please sign in to comment.