{"id":298475,"student_id":10,"content":"Game.set({ width: 1200, height: 900 })\nsetBackdrop('black')\n\nvar numbers = []\nvar grid = []\nlet clock = 0\nlet food = {}\nlet direction = 90\nlet bodies = [\n { x: 14, y: 10 },\n { x: 13, y: 10 },\n { x: 12, y: 10 },\n { x: 11, y: 10 },\n { x: 10, y: 10 },\n]\n\nfood.x = Math.floor(Math.random() * 40)\nfood.y = Math.floor(Math.random() * 30)\n\nfor (let x = 0; x \u003c 40; x++) {\n grid[x] = []\n numbers[x] = []\n for (let y = 0; y \u003c 30; y++) {\n grid[x][y] = ''\n numbers[x][y] = -1\n }\n}\n\non('keydown', 'space', expend)\n\nfunction gameloop () {\n \n if (key.up) direction = 0\n if (key.down) direction = 180\n if (key.left) direction = 270\n if (key.right) direction = 90\n \n clock++\n bodies.forEach(b =\u003e {\n pen.fillColor = 'green'\n pen.drawRect(b.x * 30, b.y * 30, 30, 30)\n })\n \n pen.fillColor = 'red'\n pen.drawRect(food.x * 30, food.y * 30, 30, 30)\n \n if (clock % 1 == 0) {\n let tail = bodies.pop()\n let head = bodies[0]\n \n if (grid[head.x][head.y] !== '') direction = grid[head.x][head.y]\n \n let headNumber = numbers[head.x][head.y]\n let tailNumber = numbers[tail.x][tail.y]\n let foodNumber = numbers[food.x][food.y]\n \n if (head.y \u003e 0) {\n let nextNum = numbers[head.x][head.y - 1]\n if (isValid(headNumber, tailNumber, foodNumber, nextNum)) {\n direction = 0\n }\n }\n \n if (head.y \u003c 29) {\n let nextNum = numbers[head.x][head.y + 1]\n if (isValid(headNumber, tailNumber, foodNumber, nextNum)) {\n direction = 180\n }\n }\n \n if (head.x \u003e 0) {\n let nextNum = numbers[head.x - 1][head.y]\n if (isValid(headNumber, tailNumber, foodNumber, nextNum)) {\n direction = 270\n }\n }\n \n if (head.x \u003e 39) {\n let nextNum = numbers[head.x + 1][head.y]\n if (isValid(headNumber, tailNumber, foodNumber, nextNum)) {\n direction = 90\n }\n }\n \n \n tail.x = bodies[0].x\n tail.y = bodies[0].y\n \n if (direction === 0) tail.y -= 1\n if (direction === 90) tail.x += 1\n if (direction === 180) tail.y += 1\n if (direction === 270) tail.x -= 1\n bodies.unshift(tail)\n \n if (tail.x === food.x \u0026\u0026 tail.y === food.y) {\n bodies.push({ x: food.x, y: food.y })\n food.x = Math.floor(Math.random() * 38) + 1\n food.y = Math.floor(Math.random() * 28) + 1\n \n // AI()\n }\n }\n \n for (let x = 0; x \u003c 40; x++) {\n for (let y = 0; y \u003c 30; y++) {\n pen.color = 'white'\n if (grid[x][y] === 0) pen.drawLine(x * 30 + 15, y * 30 + 15, x * 30 + 15, y * 30 - 15 )\n if (grid[x][y] === 90) pen.drawLine(x * 30 + 15, y * 30 + 15, x * 30 + 45, y * 30 + 15)\n if (grid[x][y] === 180) pen.drawLine(x * 30 + 15, y * 30 + 15, x * 30 + 15, y * 30 + 45)\n if (grid[x][y] === 270) pen.drawLine(x * 30 + 15, y * 30 + 15, x * 30 - 15, y * 30 + 15)\n \n\n pen.drawText(numbers[x][y], x*30, y*30, 10, 'red')\n }\n }\n\n}\n\nforever(gameloop)\n\n// AI()\nexpend()\n\nfunction isValid (headNum, tailNum, foodNum, nextNum) {\n if (headNum \u003e tailNum) {\n if (nextNum \u003c headNum \u0026\u0026 nextNum \u003e tailNum) return false\n } else {\n if (nextNum \u003c headNum || nextNum \u003e tailNum) return false\n }\n \n if (headNum \u003e foodNum) {\n if (nextNum \u003c headNum \u0026\u0026 nextNum \u003e foodNum) return false\n } else {\n if (nextNum \u003c headNum || nextNum \u003e foodNum) return false\n }\n \n return true\n}\n\n\nfunction AI () {\n \n for (let x = 0; x \u003c 40; x++) {\n for (let y = 0; y \u003c 30; y++) {\n grid[x][y] = ''\n }\n }\n var arr = [food]\n \n grid[food.x][food.y] = 0\n \n while (arr.length \u003e 0) {\n var pos = arr.shift()\n \n if (pos.y \u003c 29 \u0026\u0026 grid[pos.x][pos.y + 1] === '' \u0026\u0026 !bodies.some(p =\u003e p.x === pos.x \u0026\u0026 p.y === pos.y + 1)) {\n grid[pos.x][pos.y + 1] = 0\n arr.push({ x: pos.x, y: pos.y + 1 })\n }\n if (pos.y \u003e 0 \u0026\u0026 grid[pos.x][pos.y - 1] === '' \u0026\u0026 !bodies.some(p =\u003e p.x === pos.x \u0026\u0026 p.y === pos.y - 1)) {\n arr.push({ x: pos.x, y: pos.y - 1 })\n grid[pos.x][pos.y - 1] = 180\n }\n if (pos.x \u003c 39 \u0026\u0026 grid[pos.x + 1][pos.y] === '' \u0026\u0026 !bodies.some(p =\u003e p.x === pos.x + 1 \u0026\u0026 p.y === pos.y)) {\n arr.push({ x: pos.x + 1, y: pos.y})\n grid[pos.x + 1][pos.y] = 270\n }\n if (pos.x \u003e 0 \u0026\u0026 grid[pos.x - 1][pos.y] === '' \u0026\u0026 !bodies.some(p =\u003e p.x === pos.x - 1 \u0026\u0026 p.y === pos.y)) {\n arr.push({ x: pos.x - 1, y: pos.y})\n grid[pos.x - 1][pos.y] = 90\n }\n \n }\n}\n\n\n\n// grid[0][0] = 90\n// grid[1][0] = 180\n// grid[1][1] = 270\n// grid[0][1] = 0\n\n\n\n\nfunction expend () {\n for (let x = 0; x \u003c 40; x++) {\n for (let y = 0; y \u003c 30; y++) {\n if (y === 0) {\n if (x === 39) grid[x][y] = 180\n else grid[x][y] = 90\n } else {\n if (x % 2 == 0) grid[x][y] = 0\n else grid[x][y] = 180\n }\n \n if (y == 1 \u0026\u0026 x % 2 == 0 \u0026\u0026 x != 0) grid[x][y] = 270\n if (y == 29 \u0026\u0026 x % 2 == 1 \u0026\u0026 x != 0) grid[x][y] = 270\n }\n }\n \n let pos = { x: 0, y: 0 }\n for (let i = 0; i \u003c 30*40; i++) {\n numbers[pos.x][pos.y] = i\n pos = nextBlock(pos.x, pos.y)\n }\n}\n\n\nfunction nextBlock (x, y) {\n let p = grid[x][y]\n if (p === 0) return { x, y: y - 1 }\n if (p === 90) return { x: x + 1, y }\n if (p === 180) return { x, y: y + 1 }\n if (p === 270) return { x: x - 1, y }\n}\n\n","created_at":"2021-09-19T23:13:11.415+08:00","updated_at":"2024-01-15T23:16:15.095+08:00","name":"snakeAI","language":"javascript","screenshot":{"url":"/uploads/project/screenshot/298475/cddb66bf088c39d6d57b79fba6e13b4b.jpg"},"parent_id":2,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":355,"hashid":"kdms64y5j","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":6024948,"file_name":"arrow.png","project_id":298475,"asset_id":418378,"created_at":"2021-09-24T13:45:52.434+08:00","updated_at":"2021-09-24T13:45:52.434+08:00"}]
Orange Apple Learning Platform
My Projects
View Project Page
Export
Remix
Import
Delete
Download Android APP (APK)
Screenshot
繁中
简中
English
日本語
1:1:1
1:1
Full Width
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer
Scan the QRCode below with your phone to install
Or you can
download APK
to this computer