{"id":49509,"student_id":10,"content":"var car = new Car();\ncar.reset(50, 50, 90);\ncar.speed = 0;\ncar.addSensor(-30);\ncar.addSensor(0);\ncar.addSensor(30);\n\nforever(function(){\n print(car.sensorsData);\n if (key.right) car.direction += 3;\n if (key.left) car.direction -= 3;\n if (key.up) car.speed += 0.05;\n if (key.space) car.speed -= 0.05;\n});\n\n\nwhen('keyup', 'r', function () {\n car.reset(50, 50, 90); \n car.speed = 0;\n});\n\n// 當所有車子都壞掉時,會觸發這個函式\nfunction nextGeneration () {\n console.log('產生下一代車子...');\n car.reset(50, 50, 90);\n}\n\n// 產生 dna1 dna2 的下一代基因\nfunction crossover (a, b) {\n \n}\n\n// length 要產生的 dna 長度\nfunction randomDNA (length) {\n \n}\n\n// input*5 hidden*4 output*2\nfunction NN (inputs, weights) {\n var i = inputs;\n var w = outputs;\n}","created_at":"2018-11-19T15:58:37.620+08:00","updated_at":"2019-10-19T04:10:56.875+08:00","name":"學生預設專案","language":"javascript","screenshot":{"url":"https://cdn0.koding.school/uploads/project/screenshot/49509/89edcce60b8890776f43b51e69f48ea4.jpg"},"parent_id":49205,"plugin":"function Point (x, y) {\n this.x = x || 0;\n this.y = y || 0;\n}\n\nfunction Line (p1, p2, size, color) {\n\n this.p1 = p1;\n this.p2 = p2;\n this.size = size || 1;\n this.color = color || 'black';\n this.a = 0;\n this.b = 0;\n \n // y = ax + b\n // a:斜率 b:常數\n this.update = function () {\n // 計算斜率\n this.a = (this.p1.y - this.p2.y) / (this.p1.x - this.p2.x);\n \n // 這招真的很無恥,斜率 1000 畫面上分辨不出是否垂直線\n // 在計算交會點的時候就很方便,不用再判斷是否為垂直線\n if (Math.abs(this.a) === Infinity) this.a = 1000;\n \n // 計算常數項\n this.b = this.p1.y - this.p1.x*this.a;\n }\n this.update(); // 初始化更新\n \n \n // 取得兩線斷相交的座標,如果沒有相交就回傳 undefined\n this.touched = function (line) {\n\n if (!this.isCrossWith(line)) return; // undefined\n \n let x = (line.b - this.b)/(this.a - line.a);\n let y = this.a*x + this.b;\n return {x: x, y: y};\n }\n \n // 兩線斷是否相交\n this.isCrossWith = function (line) {\n return line.calc(this.p1.x, this.p1.y) * line.calc(this.p2.x, this.p2.y) \u003c 0 \u0026\u0026\n this.calc(line.p1.x, line.p1.y) * this.calc(line.p2.x, line.p2.y) \u003c 0\n }\n\n this.calc = function (x, y) {\n if (Math.abs(this.a) == Infinity) return x - this.b;\n return this.a*x + this.b - y;\n }\n\n this.render = function () {\n pen.size = this.size;\n pen.color = this.color;\n pen.drawLine(this.p1.x, this.p1.y, this.p2.x, this.p2.y);\n }\n \n var self = this;\n forever(function(){\n self.update();\n self.render();\n });\n}\n\n\nvar walls = []; // 存放賽道線條\nvar cars = []; // 存放所有製造出來的車子\n\n\nfunction Car (x, y) {\n \n let width = 20; // 車體中心到四個角的距離\n let sensorLength = 1000; // 感測器的長度距離\n\n let center = createSprite('dot.jpg');\n center.x = x || 320;\n center.y = y || 240;\n center.speed = 1; // 預設車速\n center.status = 'running'; // running, broken\n \n // 車體的四個點\n let p1 = new Point();\n let p2 = new Point();\n let p3 = new Point();\n let p4 = new Point();\n center.corner = [p1, p2, p3, p4];\n \n // 感測器的點\n // let a = center.a = new Point();\n // let b = center.b = new Point();\n // let c = center.c = new Point();\n // let d = center.d = new Point();\n // let e = center.e = new Point();\n // center.censorPoints = [a, b, c, d, e];\n center.censorPoints = [];\n \n // 感測器的線段\n center.sensors = [\n // new Line(center, a, 0, '#00000000'),\n // new Line(center, b, 0, '#00000000'),\n // new Line(center, c, 0, '#00000000'),\n // new Line(center, d, 0, '#00000000'),\n // new Line(center, e, 0, '#00000000'),\n ]\n \n // 感測器讀取的資料\n center.sensorsData = [];\n \n // 車體\n center.border = [\n new Line(p1, p2, 2),\n new Line(p2, p3, 2),\n new Line(p3, p4, 2),\n new Line(p4, p1, 2),\n ];\n \n center.addSensor = function (direction) {\n let point = new Point();\n let line = new Line(center, point, 0, '#00000000');\n line.direction = direction;\n center.censorPoints.push(point);\n center.sensors.push(line);\n }\n \n center.updateLines = function () {\n for (var i=0; i\u003c4; i++) {\n this.border[i].update();\n }\n for (var i=0; i\u003cthis.sensors.length; i++) {\n this.sensors[i].update();\n }\n }\n \n center.update = function () {\n var self = this;\n this.border.forEach((line) =\u003e {\n if (self.status == 'broken') line.color = 'gray';\n else line.color = 'black';\n });\n \n // 更新車體的四個點座標\n var offset = [30, 150, -150, -30];\n for (var i=0; i\u003c4; i++) {\n this.direction += offset[i];\n this.stepForward(width);\n this.corner[i].x = this.x;\n this.corner[i].y = this.y;\n this.stepForward(-width);\n this.direction -= offset[i];\n }\n \n // 更新感測器的線\n // var offset = [-45, -22.5, 0, 22.5, 45];\n // var offset = [-60, 0, 60];\n for (var i=0; i\u003ccenter.sensors.length; i++) {\n var offset = center.sensors[i].direction;\n center.direction += offset;\n center.stepForward(sensorLength);\n center.censorPoints[i].x = center.x;\n center.censorPoints[i].y = center.y;\n center.stepForward(-sensorLength);\n center.direction -= offset;\n }\n \n this.updateLines();\n };\n \n center.turn = function (direction) {\n if (this.status == 'broken') return;\n this.direction += direction;\n }\n \n center.reset = function (x, y, direction) {\n this.x = x;\n this.y = y;\n this.direction = direction;\n this.update();\n this.status = 'running';\n }\n \n forever(function () {\n if (center.speed \u003c 1) center.speed = 1;\n if (center.speed \u003e 10) center.speed = 10;\n if (center.status == 'running') {\n center.stepForward(center.speed); \n }\n center.update();\n });\n \n center.update(); // 初始化更新\n \n cars.push(center);\n return center;\n}\n\nforever(function(){\n \n // 檢查車子是否碰到賽道邊界,碰到則將車子狀態改成 broken\n cars.forEach(function (car) {\n for (var x=0; x\u003ccar.border.length; x++) {\n for (var y=0; y\u003cwalls.length; y++) {\n if (car.border[x].touched(walls[y])) {\n car.status = 'broken';\n return;\n }\n }\n }\n });\n \n // 更新車子感測器的數值\n cars.forEach(function (car) {\n if (car.status == 'broken') return;\n for (var x=0; x\u003ccar.sensors.length; x++) {\n \n var shortest = Infinity;\n var target = {};\n \n for (var y=0; y\u003cwalls.length; y++) {\n let pos = car.sensors[x].touched(walls[y]);\n if (pos) {\n let length = Math.sqrt((pos.x - car.x)**2 + (pos.y - car.y)**2);\n if (length \u003c shortest) {\n shortest = Math.round(length);\n target = pos;\n }\n }\n \n }\n \n car.sensorsData[x] = shortest;\n pen.color = '#aaa';\n pen.size = 1;\n pen.drawLine(car.x, car.y, target.x, target.y);\n pen.fillColor = 'red';\n pen.drawCircle(target.x, target.y, 3);\n }\n });\n})\n\n\nforever(function(){\n for (var i=0; i\u003ccars.length; i++) {\n if (cars[i].status == 'running') return;\n }\n if (window.nextGeneration) nextGeneration();\n});\n\n\n\nlet map = [[{\"x\":21,\"y\":64},{\"x\":21,\"y\":64},{\"x\":21,\"y\":44},{\"x\":36,\"y\":28},{\"x\":54,\"y\":17},{\"x\":79,\"y\":12},{\"x\":206,\"y\":12},{\"x\":324,\"y\":13},{\"x\":456,\"y\":13},{\"x\":548,\"y\":13},{\"x\":569,\"y\":19},{\"x\":588,\"y\":31},{\"x\":601,\"y\":47},{\"x\":611,\"y\":69},{\"x\":612,\"y\":88},{\"x\":612,\"y\":113},{\"x\":613,\"y\":403},{\"x\":609,\"y\":426},{\"x\":598,\"y\":446},{\"x\":581,\"y\":461},{\"x\":559,\"y\":470},{\"x\":524,\"y\":470},{\"x\":342,\"y\":470},{\"x\":272,\"y\":470},{\"x\":257,\"y\":464},{\"x\":247,\"y\":449},{\"x\":243,\"y\":432},{\"x\":243,\"y\":413},{\"x\":244,\"y\":393},{\"x\":253,\"y\":378},{\"x\":262,\"y\":364},{\"x\":275,\"y\":349},{\"x\":394,\"y\":197},{\"x\":397,\"y\":182},{\"x\":393,\"y\":169},{\"x\":376,\"y\":165},{\"x\":362,\"y\":166},{\"x\":214,\"y\":166},{\"x\":206,\"y\":171},{\"x\":203,\"y\":182},{\"x\":202,\"y\":197},{\"x\":204,\"y\":395},{\"x\":202,\"y\":417},{\"x\":194,\"y\":435},{\"x\":180,\"y\":451},{\"x\":161,\"y\":466},{\"x\":134,\"y\":469},{\"x\":107,\"y\":468},{\"x\":55,\"y\":469},{\"x\":37,\"y\":464},{\"x\":27,\"y\":451},{\"x\":19,\"y\":431},{\"x\":20,\"y\":411},{\"x\":19,\"y\":268},{\"x\":19,\"y\":102},{\"x\":21,\"y\":64}],\n[{\"x\":123,\"y\":84},{\"x\":123,\"y\":84},{\"x\":205,\"y\":83},{\"x\":331,\"y\":84},{\"x\":461,\"y\":85},{\"x\":517,\"y\":85},{\"x\":529,\"y\":92},{\"x\":530,\"y\":103},{\"x\":529,\"y\":374},{\"x\":526,\"y\":388},{\"x\":513,\"y\":394},{\"x\":372,\"y\":394},{\"x\":359,\"y\":385},{\"x\":363,\"y\":370},{\"x\":447,\"y\":253},{\"x\":474,\"y\":217},{\"x\":482,\"y\":197},{\"x\":488,\"y\":178},{\"x\":485,\"y\":151},{\"x\":478,\"y\":128},{\"x\":462,\"y\":106},{\"x\":444,\"y\":95},{\"x\":413,\"y\":95},{\"x\":341,\"y\":94},{\"x\":216,\"y\":95},{\"x\":189,\"y\":95},{\"x\":161,\"y\":100},{\"x\":144,\"y\":113},{\"x\":133,\"y\":125},{\"x\":125,\"y\":140},{\"x\":122,\"y\":157},{\"x\":122,\"y\":176},{\"x\":121,\"y\":188},{\"x\":120,\"y\":381},{\"x\":116,\"y\":394},{\"x\":104,\"y\":395},{\"x\":99,\"y\":386},{\"x\":99,\"y\":371},{\"x\":97,\"y\":122},{\"x\":98,\"y\":101},{\"x\":123,\"y\":84}],\n[{\"x\":110,\"y\":82},{\"x\":110,\"y\":82}]]\n\nmap.forEach(function (points) {\n for (var i=1; i\u003cpoints.length; i++) {\n let p1 = new Point(points[i - 1].x, points[i - 1].y);\n let p2 = new Point(points[i].x, points[i].y);\n let l =new Line(p1, p2, 3);\n walls.push(l)\n }\n});\n\n\n\n","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":301,"hashid":"meysg5ge","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":887652,"file_name":"dot.jpg","project_id":49509,"asset_id":96139,"created_at":"2018-11-19T15:58:37.630+08:00","updated_at":"2018-11-19T15:58:37.630+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
下載 Android APP (APK)
截圖
1:1:1
1:1
full
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦