Main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. MWF.xApplication.Snake.Main = new Class({
  2. Extends: MWF.xApplication.Common.Main,
  3. Implements: [Options, Events],
  4. options: {
  5. "style": "default",
  6. "name": "Snake",
  7. "icon": "icon.png",
  8. "width": "400",
  9. "height": "500",
  10. "isResize": false,
  11. "isMax": false,
  12. "title": MWF.xApplication.Snake.LP.title
  13. },
  14. onQueryLoad: function(){
  15. this.lp = MWF.xApplication.Snake.LP;
  16. },
  17. createNode: function(){
  18. this.content.setStyle("overflow", "hidden");
  19. this.node = new Element("div", {
  20. "styles": this.css.contentNode
  21. }).inject(this.content);
  22. },
  23. loadApplication: function(callback){
  24. this.createNode();
  25. var logoNode = new Element("div", {
  26. "styles": this.css.logoNode
  27. });
  28. logoNode.inject(this.node);
  29. this.beginNode = new Element("div", {
  30. "styles": this.css.beginNode,
  31. "text": "Begin"
  32. }).inject(this.node);
  33. this.beginNode.addEvent("click", function(){
  34. this.beginGame();
  35. }.bind(this));
  36. this.addEvents({
  37. "uncurrent": function(){
  38. if (this.interval){
  39. window.clearInterval(this.interval);
  40. delete this.interval;
  41. this.createPauseNode();
  42. }
  43. }.bind(this)
  44. });
  45. },
  46. createPauseNode: function(){
  47. this.pauseMarkNode = new Element("div", {
  48. "styles": this.css.pauseMarkNode
  49. }).inject(this.gameNode);
  50. this.playNode = new Element("div", {
  51. "styles": this.css.playNode
  52. }).inject(this.pauseMarkNode);
  53. this.playNode.position({
  54. relativeTo: this.gameNode,
  55. position: 'center',
  56. edge: 'center'
  57. });
  58. this.playNode.addEvents({
  59. "click": function(){
  60. this.replay();
  61. }.bind(this),
  62. "mouseover": function(e){
  63. this.setStyle("background-image", "url("+"/x_component_Snake/$Main/default/play.png)");
  64. },
  65. "mouseout": function(e){
  66. this.setStyle("background-image", "url("+"/x_component_Snake/$Main/default/pause.png)");
  67. }
  68. });
  69. },
  70. replay: function(){
  71. if (this.pauseMarkNode){
  72. this.pauseMarkNode.destroy();
  73. delete this.pauseMarkNode;
  74. }
  75. if (this.playNode){
  76. this.playNode.destroy();
  77. delete this.playNode;
  78. }
  79. this.interval = window.setInterval(function(){
  80. this.move();
  81. }.bind(this), this.speed);
  82. },
  83. beginGameTimer: function(){
  84. var timerNode = new Element("div", {
  85. "styles": this.css.timerNode
  86. }).inject(this.gameNode);
  87. timerNode.position({
  88. relativeTo: this.gameNode,
  89. position: 'center',
  90. edge: 'center'
  91. });
  92. var i=3;
  93. timerNode.set("text", i);
  94. window.setTimeout(function(){
  95. this.setTimerNodeText(i, timerNode);
  96. }.bind(this), 1000);
  97. },
  98. setTimerNodeText: function(i, timerNode){
  99. i--;
  100. if (i>0){
  101. timerNode.set("text", i);
  102. window.setTimeout(function(){
  103. this.setTimerNodeText(i, timerNode);
  104. }.bind(this), 1000);
  105. }else{
  106. this.begin();
  107. }
  108. },
  109. beginGame: function(){
  110. this.node.empty();
  111. this.createInforNode();
  112. this.createGameNode();
  113. this.beginGameTimer();
  114. this.responseKeyboard();
  115. },
  116. createInforNode: function(){
  117. this.inforNode = new Element("div", {
  118. "styles": this.css.inforNode
  119. }).inject(this.node);
  120. this.scoreNode = new Element("div", {
  121. "styles": this.css.scoreNode
  122. }).inject(this.inforNode);
  123. this.scoreNode.set("text", "score: 0");
  124. this.levelNode = new Element("div", {
  125. "styles": this.css.levelNode
  126. }).inject(this.inforNode);
  127. this.levelNode.set("text", "level: 1");
  128. },
  129. responseKeyboard: function(){
  130. this.keyboardEvents = new Keyboard({
  131. defaultEventType: 'keyup',
  132. events: {
  133. 'up': function(){this.turn("up");}.bind(this),
  134. 'down': function(){this.turn("down");}.bind(this),
  135. 'left': function(){this.turn("left");}.bind(this),
  136. 'right': function(){this.turn("right");}.bind(this)
  137. }
  138. });
  139. this.keyboardEvents.activate();
  140. },
  141. turn: function(direction){
  142. this.turns.push(direction);
  143. // var snakeHead = this.snakePoints[0];
  144. // switch (direction) {
  145. // case "right":
  146. // if (snakeHead.direction=="left") return false;
  147. // break;
  148. // case "left":
  149. // if (snakeHead.direction=="right") return false;
  150. // break;
  151. // case "up":
  152. // if (snakeHead.direction=="down") return false;
  153. // break;
  154. // case "down":
  155. // if (snakeHead.direction=="up") return false;
  156. // break;
  157. // }
  158. // this.snakePoints.each(function(point, idx){
  159. // // if (idx==0){
  160. // // point.direction = direction;
  161. // // }else{
  162. // point.turns.push({"position": Object.clone(snakeHead.point), "direction": direction});
  163. // // }
  164. // }.bind(this));
  165. },
  166. createGameNode: function(){
  167. this.gameNode = new Element("div", {
  168. "styles": this.css.gameNode
  169. }).inject(this.node);
  170. },
  171. begin: function(){
  172. this.gameNode.empty();
  173. this.initGameParameters();
  174. this.drawEatPoint();
  175. this.beginMove();
  176. },
  177. beginMove: function(){
  178. this.interval = window.setInterval(function(){
  179. this.move();
  180. }.bind(this), this.speed);
  181. },
  182. move: function(){
  183. this.snakePoints.each(function(point){
  184. point.move();
  185. }.bind(this));
  186. },
  187. drawEatPoint: function(){
  188. var left = this.eatPoint.x*10;
  189. var top = this.eatPoint.y*10;
  190. this.eatPointNode = new Element("div", {
  191. "styles": this.css.eatPointNode
  192. }).inject(this.gameNode);
  193. this.eatPointNode.setPosition({
  194. "x": this.relativePosition.x+left+this.border.x,
  195. "y": this.relativePosition.y+top+this.border.y
  196. });
  197. },
  198. initGameParameters: function(){
  199. this.score = 0;
  200. this.level = 1;
  201. this.levelUpCount = 8;
  202. this.direction = "right";
  203. this.speed = 200;
  204. this.maxX = 35;
  205. this.maxY = 41;
  206. this.turns = [];
  207. this.relativePosition = this.gameNode.getPosition(this.gameNode.getOffsetParent());
  208. this.border = {"x": this.gameNode.getStyle("border-left-width").toInt(), "y": this.gameNode.getStyle("border-top-width").toInt()};
  209. //this.eatPoint = {"x": 1, "y": 1};
  210. this.snakePoints = [new MWF.xApplication.SnakePoint({"x": 8,"y": 0}, 0, "right", this),
  211. new MWF.xApplication.SnakePoint({"x": 7,"y": 0}, 1, "right", this),
  212. new MWF.xApplication.SnakePoint({"x": 6,"y": 0}, 2, "right", this),
  213. new MWF.xApplication.SnakePoint({"x": 5,"y": 0}, 3, "right", this),
  214. new MWF.xApplication.SnakePoint({"x": 4,"y": 0}, 4, "right", this),
  215. new MWF.xApplication.SnakePoint({"x": 3,"y": 0}, 5, "right", this),
  216. new MWF.xApplication.SnakePoint({"x": 2,"y": 0}, 6, "right", this),
  217. new MWF.xApplication.SnakePoint({"x": 1,"y": 0}, 7, "right", this),
  218. new MWF.xApplication.SnakePoint({"x": 0,"y": 0}, 8, "right", this)];
  219. this.eatPoint = this.getEatPoint();
  220. },
  221. getEatPoint: function(){
  222. var x = (Math.random()*this.maxX).toInt();
  223. var y = (Math.random()*this.maxY).toInt();
  224. var isAvailable = true;
  225. for (var i=0; i<this.snakePoints.length; i++){
  226. var point = this.snakePoints[i];
  227. if (x == point.point.x && y == point.point.y){
  228. isAvailable = false;
  229. break;
  230. };
  231. }
  232. if (isAvailable){
  233. return {"x": x, "y": y};
  234. }else{
  235. return this.getEatPoint();
  236. }
  237. },
  238. lengthen: function(){
  239. var index = this.snakePoints.length;
  240. var tail = this.snakePoints[this.snakePoints.length-1];
  241. var direction = tail.direction;
  242. var point = Object.clone(tail.point);
  243. // switch (tail.direction){
  244. // case "right":
  245. // point.x--;
  246. // if (point.x<0) point.x = this.snake.maxX;
  247. // break;
  248. // case "left":
  249. // point.x++;
  250. // if (point.x>this.snake.maxX) point.x = 0;
  251. // break;
  252. // case "up":
  253. // point.y++;
  254. // if (point.y>this.snake.maxY) point.y = 0;
  255. // break;
  256. // case "down":
  257. // point.y--;
  258. // if (point.y<0) point.x = this.snake.maxY;
  259. // break;
  260. // }
  261. var newPoint = new MWF.xApplication.SnakePoint(point, index, direction, this);
  262. tail.turns.each(function(turn){
  263. var newTurn = {
  264. "position": {
  265. "x": turn.position.x,
  266. "y": turn.position.y
  267. },
  268. "direction": turn.direction
  269. };
  270. newPoint.turns.push(newTurn);
  271. });
  272. this.snakePoints.push(newPoint);
  273. this.eatPointNode.destroy();
  274. this.eatPoint = this.getEatPoint();
  275. this.drawEatPoint();
  276. this.score = this.score+(100*this.level);
  277. this.scoreNode.set("text", "score: "+this.score);
  278. if (this.level<10){
  279. this.levelUpCount--;
  280. if (this.levelUpCount<=0) this.levelUp();
  281. }
  282. },
  283. levelUp: function(){
  284. this.levelUpCount = 8;
  285. this.level++;
  286. this.speed = this.speed-16;
  287. this.levelNode.set("text", "level: "+this.level);
  288. window.clearInterval(this.interval);
  289. this.interval = window.setInterval(function(){
  290. this.move();
  291. }.bind(this), this.speed);
  292. },
  293. gameOver: function(){
  294. window.clearInterval(this.interval);
  295. delete this.interval;
  296. this.createGameOverNode();
  297. },
  298. createGameOverNode: function(){
  299. this.gemeOverMarkNode = new Element("div", {
  300. "styles": this.css.gemeOverMarkNode
  301. }).inject(this.gameNode);
  302. this.gameOverNode = new Element("div", {
  303. "styles": this.css.gameOverNode,
  304. "text": "Game Over"
  305. }).inject(this.gameNode);
  306. this.gameOverNode.position({
  307. relativeTo: this.gameNode,
  308. position: 'center',
  309. edge: 'center',
  310. offset: {x: 0, y:-80}
  311. });
  312. this.replayNode = new Element("div", {
  313. "styles": this.css.replayNode,
  314. "text": "Replay"
  315. }).inject(this.gameNode);
  316. this.replayNode.position({
  317. relativeTo: this.gameNode,
  318. position: 'center',
  319. edge: 'center',
  320. offset: {x: 0, y:40}
  321. });
  322. this.replayNode.addEvent("click", function(){
  323. this.beginGame();
  324. }.bind(this));
  325. }
  326. });
  327. MWF.xApplication.SnakePoint = new Class({
  328. initialize: function(point, idx, direction, snake){
  329. this.snake = snake;
  330. this.point = point;
  331. this.index = idx;
  332. this.direction = direction;
  333. this.turns = [];
  334. this.draw();
  335. },
  336. draw: function(){
  337. this.node = new Element("div", {
  338. "styles": this.snake.css.snakePointNode
  339. }).inject(this.snake.gameNode);
  340. var left = this.point.x*10;
  341. var top = this.point.y*10;
  342. this.node.setPosition({
  343. "x": this.snake.relativePosition.x+left+this.snake.border.x,
  344. "y": this.snake.relativePosition.y+top+this.snake.border.y
  345. });
  346. },
  347. checkPointDirection: function(){
  348. turn = this.turns[0];
  349. if (this.point.x==turn.position.x && this.point.y==turn.position.y){
  350. this.direction = turn.direction;
  351. this.turns.shift();
  352. }
  353. },
  354. checkSnakeDirection: function(){
  355. if (this.snake.turns.length){
  356. var direction = this.snake.turns.shift();
  357. switch (direction){
  358. case "right":
  359. if (this.direction=="right" || this.direction=="left") return false;
  360. break;
  361. case "left":
  362. if (this.direction=="right" || this.direction=="left") return false;
  363. break;
  364. case "up":
  365. if (this.direction=="up" || this.direction=="down") return false;
  366. break;
  367. case "down":
  368. if (this.direction=="up" || this.direction=="down") return false;
  369. break;
  370. }
  371. this.direction = direction;
  372. this.snake.snakePoints.each(function(point){
  373. if (point!=this){
  374. point.turns.push({"position": Object.clone(this.point), "direction": this.direction});
  375. }
  376. }.bind(this));
  377. }
  378. },
  379. move: function(){
  380. if (this.turns.length){
  381. this.checkPointDirection();
  382. }else{
  383. this.checkSnakeDirection();
  384. }
  385. switch (this.direction) {
  386. case "right":
  387. this.point.x++;
  388. if (this.point.x>this.snake.maxX) this.point.x = 0;
  389. break;
  390. case "left":
  391. this.point.x--;
  392. if (this.point.x<0) this.point.x = this.snake.maxX;
  393. break;
  394. case "up":
  395. this.point.y--;
  396. if (this.point.y<0) this.point.y = this.snake.maxY;
  397. break;
  398. case "down":
  399. this.point.y++;
  400. if (this.point.y>this.snake.maxY) this.point.y = 0;
  401. break;
  402. }
  403. if (this.checkOver()){
  404. this.checkLengthen();
  405. this.reDraw();
  406. }else{
  407. this.snake.gameOver();
  408. }
  409. },
  410. checkLengthen: function(){
  411. if (this.index==0){
  412. if (this.point.x == this.snake.eatPoint.x && this.point.y == this.snake.eatPoint.y){
  413. this.snake.lengthen();
  414. }
  415. }
  416. },
  417. checkOver: function(){
  418. if (this.index!=0) return true;
  419. for (var i=1; i<this.snake.snakePoints.length; i++){
  420. var point = this.snake.snakePoints[i];
  421. if (this.point.x == point.point.x && this.point.y == point.point.y) return false;
  422. }
  423. return true;
  424. },
  425. reDraw: function(){
  426. var left = this.point.x*10;
  427. var top = this.point.y*10;
  428. this.node.setPosition({
  429. "x": this.snake.relativePosition.x+left+this.snake.border.x,
  430. "y": this.snake.relativePosition.y+top+this.snake.border.y
  431. });
  432. }
  433. // turn: function(direction){
  434. // this.turns.push({
  435. // "times": this.index,
  436. // "direction": direction
  437. // });
  438. // }
  439. });