{"id":106494,"student_id":37,"content":"var grid = game(32, 24); // 初始化地圖\nwhen('keyup', 'space', grid.pause); // 空白鍵開始、暫停\n\nfunction update () {\n var nexGrid = [];\n for (var x = 0; x \u003c 32; x++) {\n nexGrid[x] = [];\n for (var y = 0; y \u003c 24; y++) {\n var survive = calc(x, y);\n // print(survive, x*20, y*20, \"red\");\n if (grid[x][y] == true \u0026\u0026 survive \u003c 2) {\n nexGrid[x][y] = false;\n }\n if (grid[x][y] == true \u0026\u0026 survive \u003e 3) {\n nexGrid[x][y] = false;\n }\n if (grid[x][y] == true \u0026\u0026 survive == 2) {\n nexGrid[x][y] = false;\n }\n if (survive == 3) {\n nexGrid[x][y] = true;\n }\n }\n }\n for (var i = 0; i \u003c 32; i++) {\n for (var j = 0; j \u003c 24; j++) {\n grid[i][j] = nexGrid[i][j];\n }\n\n }\n\n\n}\nfunction calc (x, y) {\n var count = 0;\n if (grid[x-1][y] == true) {\n count ++;\n }\n if (grid[x-1][y-1] == true) {\n count ++;\n }\n if (grid[x-1][y+1] == true) {\n count ++;\n }\n if (grid[x][y-1] == true) {\n count ++;\n }\n if (grid[x][y+1] == true) {\n count ++;\n }\n if (grid[x+1][y] == true) {\n count ++;\n }\n if (grid[x+1][y+1] == true) {\n count ++;\n }\n if (grid[x+1][y-1] == true) {\n count ++;\n }\n return count;\n}","created_at":"2019-11-10T10:08:40.675+08:00","updated_at":"2019-11-10T11:21:33.904+08:00","name":"生命遊戲 副本","language":"javascript","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/106494/a6c5e04d37f8cb3483e09214c2c4a62e.jpg"},"parent_id":33937,"plugin":"// 為了讓 print 正常在 update 中順利印出數字\nstopRendering();\n\nvar game = function (w, h) {\n \n var grid = {}\n var width = 640/w;\n var height = 480/h;\n var run = false; // 開始 or 暫停\n\n for (var x=-1; x \u003c w+1; x++) {\n grid[x] = {};\n for (var y=-1; y \u003c h+1; y++) {\n grid[x][y] = false;\n }\n }\n \n var clock = 0;\n forever(function() {\n clock++;\n if (clock % 20 != 0) return; // 每秒更新 3 次\n\n for (var x = 0; x \u003c w; x++) {\n for (var y = 0; y \u003c h; y++) {\n pen.size = 1;\n pen.fillColor = grid[x][y] ? 'black': 'white';\n pen.drawRect(x*width, y*height, width, height);\n }\n }\n\n if (run) update();\n });\n\n when(\"click\", function() {\n var x = Math.floor(cursor.x/(640/w));\n var y = Math.floor(cursor.y/(480/h));\n grid[x][y] = !grid[x][y];\n });\n\n grid.pause = function () {\n run = !run;\n };\n\n return grid;\n}","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":53,"hashid":"meyszkk9","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦