网乐原科技

直播中

news center新闻中心
首页 > 资讯中心 > 软件技术

简单的走迷宫游戏

发布时间:2008-12-20     阅读数: 次       来源:网乐原科技
要完成此效果需要两个步骤 第一步:把如下代码加入到区域中



走迷宫
<script> var line = ""; var x = 0; var y = 0; var full="*"; var blank = "."; var wall = "#"; var goal = "$"; var fill = ""; // Functions to create the board function makeboard() { for (var i=1; i<= 10; i++) this[i] = new makeRow(); return this; } function makeRow() { for (var i=1; i<= 10; i++) this[i]=blank; return this; } // Functions to fill & clear the board. function clearBoard (form) { // Clears & resets the board x = 0; y = 0; form.xval.value = 1; form.yval.value = 1; for (var i=1; i<= 10; i++) for (var j=1; j<= 10; j++) theBoard[i][j]=blank; drawMaze(); fillBoard(form); return; } function fillBoard (form) { // Clear board buffer line = ""; form.grid.value = ""; // Fill board buffer for (var i=1; i<= 10; i++) for (var j=1; j<= 10; j++) line += theBoard[i][j]; // Move buffer contents to board form.grid.value=line; } function plot (v, h) { theBoard[v][h] = fill; } function drawMaze() { // Plots the walls of the maze // // Ideally, a function should do this automatically, // or maybe I should write a maze generating function in JS! // Note: This program operates in Y,X co-ordinates (not standard X,Y). theBoard[10][10] = goal; theBoard[1][2] = wall; theBoard[2][2] = wall; theBoard[4][1] = wall; theBoard[4][2] = wall; theBoard[4][3] = wall; theBoard[2][3] = wall; theBoard[5][2] = wall; theBoard[6][2] = wall; theBoard[2][5] = wall; theBoard[4][5] = wall; theBoard[5][5] = wall; theBoard[2][6] = wall; theBoard[2][7] = wall; theBoard[9][10] = wall; theBoard[9][9] = wall; theBoard[8][9] = wall; theBoard[7][9] = wall; theBoard[10][7] = wall; theBoard[9][7] = wall; theBoard[8][7] = wall; theBoard[6][7] = wall; theBoard[9][2] = wall; theBoard[9][3] = wall; theBoard[9][4] = wall; theBoard[8][2] = wall; theBoard[7][4] = wall; theBoard[7][5] = wall; theBoard[6][5] = wall; theBoard[6][6] = wall; theBoard[6][7] = wall; theBoard[6][8] = wall; theBoard[6][9] = wall; theBoard[5][7] = wall; theBoard[5][8] = wall; theBoard[5][9] = wall; theBoard[4][9] = wall; } function update(form) { var horiz = eval(form.xval.value); var vert = eval(form.yval.value); plot(vert,horiz); fillBoard(form); return; } function initBoard() { theBoard = new makeboard(); fill = full; clearBoard(document.board); update(document.board); } // Functions to handle the player piece // // I suppose I could have written one function to handle this, // but it was getting too complex. Feel free to try. :) // function decx(form) { fill = blank; update(form); checkx = eval(form.xval.value - 1); checky = form.yval.value; if (form.xval.value > 1) { if (theBoard[checky][checkx] != wall) { form.xval.value=eval(form.xval.value - 1); } else { alert("THUD!\nYou hit a wall."); } if (theBoard[checky][checkx] == goal) { alert("YOU WIN!"); } } fill = full; update(form); } function incx(form) { fill = blank; update(form); checkx = eval(1 * form.xval.value + 1); checky = form.yval.value; if (form.xval.value < 10) { if (theBoard[checky][checkx] != wall) { form.xval.value=eval(1 * form.xval.value + 1); } else { alert("THUD!\nYou hit a wall."); } if (theBoard[checky][checkx] == goal) { alert("YOU WIN!"); } } fill = full; update(form); } function decy(form) { fill = blank; update(form); checkx = form.xval.value; checky = eval(form.yval.value - 1); if (form.yval.value > 1) { if (theBoard[checky][checkx] != wall) { form.yval.value=eval(form.yval.value - 1); } else { alert("THUD!\nYou hit a wall."); } if (theBoard[checky][checkx] == goal) { alert("YOU WIN!"); } } fill = full; update(form); } function incy(form) { fill = blank; update(form); checkx = form.xval.value; checky = eval(1 * form.yval.value + 1); if (form.yval.value < 10) { if (theBoard[checky][checkx] != wall) { form.yval.value=eval(1 * form.yval.value + 1); } else { alert("THUD!\nYou hit a wall."); } if (theBoard[checky][checkx] == goal) { alert("YOU WIN!"); } } fill = full; update(form); } // Various Functions function cheater (form) { // Refuse to change values manually, and start over. CHEATER! alert("You can't change this value manually.\nPlease use the buttons."); clearBoard(form); update(form); } </script>


<script LANGUAGE="JavaScript"> </script>


第二步:把“onLoad="timerONE=window.setTimeout('scrollit_r2l(100)',500);initBoard();" ”加在标记里

网乐原科技

客服热线:0771-5761507

QQ:53290011

QQ邮箱:53290011@qq.com

工作时间:周一到周五 9:00-18:00

地址:广西南宁市江南万达写字楼C16栋1309室

物联网开发

关注我们

微信小商店 腾讯QQ客服 微信客服