This is a SCRIPT-8 cassette.
Created
April 20, 2019 11:05
-
-
Save tvytlx/638cca29c9b5c356c7571936286bd195 to your computer and use it in GitHub Desktop.
SCRIPT-8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// title: Snake Game | |
init = (state) => { | |
state.speed = 22; | |
state.totalElapsed = 0; | |
state.lastElapsed = 0; | |
state.score = 0; | |
state.direction = "left"; | |
state.snakeBody = [ | |
[20, 20], | |
[21, 20], | |
[22, 20], | |
[23, 20], | |
[24, 20], | |
]; | |
state.food = { | |
x: 20, | |
y: 30 | |
}; | |
state.box = { | |
size: 100, | |
x: 5, | |
y: 13 | |
}; | |
} | |
var directionOpposingMap = { | |
up: "down", | |
down: "up", | |
left: "right", | |
right: "left" | |
} | |
update = (state, input, elapsed) => { | |
// speed control | |
state.totalElapsed += elapsed; | |
if (state.totalElapsed - state.lastElapsed <= 1000 / state.speed) { | |
return; | |
} | |
state.lastElapsed = state.totalElapsed; | |
// variables | |
console.log(state); | |
var head = state.snakeBody[0]; | |
var food = state.food; | |
const { box } = state; | |
var nextNode = null; | |
// speed custom setting | |
if (input.a) { state.speed += 1 }; | |
if (input.b) { state.speed -= 1 }; | |
// direction changing | |
for (var key in input) { | |
if (key in directionOpposingMap && | |
input[key] === true && | |
key != state.direction && | |
key != directionOpposingMap[state.direction]) { | |
state.direction = key; | |
break; | |
} | |
} | |
if (state.direction === "up") { nextNode = [head[0], head[1] - 1] }; | |
if (state.direction === "down") { nextNode = [head[0], head[1] + 1] }; | |
if (state.direction === "left") { nextNode = [head[0] - 1, head[1]] }; | |
if (state.direction === "right") { nextNode = [head[0] + 1, head[1]] }; | |
// box detect | |
if (nextNode[0] <= box.x) { | |
nextNode[0] = box.x + box.size - 1; | |
} else if (nextNode[0] >= (box.x + box.size - 1)) { | |
nextNode[0] = box.x + 1; | |
} else if (nextNode[1] <= box.y) { | |
nextNode[1] = box.y + box.size - 1; | |
} else if (nextNode[1] >= (box.y + box.size - 1)) { | |
nextNode[1] = box.y + 1; | |
} | |
// snake body changing | |
state.snakeBody.unshift(nextNode); | |
if (head[0] == state.food.x && head[1] == state.food.y) { | |
var randomNum = Math.floor(Math.random() * Math.floor(box.size - 1)); | |
food.x = box.x + 1 + randomNum; | |
food.y = box.y + 1 + randomNum; | |
state.score += 10; | |
return; | |
} | |
state.snakeBody.pop(); | |
} | |
draw = (state) => { | |
clear(); | |
const { box, food } = state; | |
print(0, 0, `score: ${state.score}`); | |
print(76, 0, `speed: ${state.speed}`); | |
print(0, 118, "press a, b to change speed"); | |
circStroke(food.x, food.y, 1); | |
state.snakeBody.forEach(node => { | |
var x = node[0], y = node[1]; | |
circStroke(x, y, 1); | |
}); | |
rectStroke(box.x, box.y, box.size, box.size); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"iframeVersion": "0.1.253", | |
"lines": [ | |
104, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0 | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment