Skip to content

Instantly share code, notes, and snippets.

@icarito
Created April 30, 2020 06:50
Show Gist options
  • Save icarito/4ba95da36bfc254406062568238c3bde to your computer and use it in GitHub Desktop.
Save icarito/4ba95da36bfc254406062568238c3bde to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Tron't
const right = 0
const down = 1
const left = 2
const up = 3
init = (state) => {
Object.assign(state, {
x: 15,
y: 15,
dir: right,
speed: 0.5,
trail: [],
t: 0,
sensor: false,
agents: [
{
x: 128 - 15,
y: 128 - 15,
dir: left,
speed: 0.5,
trail: []
},
{
x: 15,
y: 128 - 15,
dir: up,
speed: 0.5,
trail: []
},
{
x: 128 -15,
y: 15,
dir: down,
speed: 0.5,
trail: []
}
]
})
}
update = (state, input) => {
if (input.start) {
state.x = state.y = 15
state.t = state.dir = 0
state.speed = 0.5
state.trail = []
state.agents = [
{
x: 128 - 15,
y: 128 - 15,
dir: left,
speed: 0.5,
trail: []
},
{
x: 15,
y: 128 - 15,
dir: up,
speed: 0.5,
trail: []
},
{
x: 128 -15,
y: 15,
dir: down,
speed: 0.5,
trail: []
}
]
clear()
startAgents(state)
}
state.t += 1
if (state.speed > 0) state.trail.push([state.x, state.y])
if (state.trail.length > 16) state.trail.shift()
if (state.dir == right) {
state.x += state.speed
state.sensor = getPixel(state.x + 1, state.y)
}
if (state.dir == left) {
state.x -= state.speed
state.sensor = getPixel(state.x - 1, state.y)
}
if (state.dir == up) {
state.y -= state.speed
state.sensor = getPixel(state.x, state.y - 1)
}
if (state.dir == down) {
state.y += state.speed
state.sensor = getPixel(state.x, state.y + 1)
}
if (state.sensor != 7) {
state.speed = 0
stopAgents(state)
}
if (input.rightPressed) state.dir = (state.dir + 1) % 4
if (input.leftPressed) state.dir = (state.dir + 3) % 4
updateAgents(state)
}
draw = state => {
if (state.t == 1) {
clear()
}
rectStroke(0, 0, 128, 128)
setPixel(state.x, state.y)
for (let step of state.trail) {
setPixel(step[0], step[1])
}
if (state.trail.length > 0) {
let last = state.trail[0]
setPixel(last[0], last[1], 3)
}
drawAgents(state)
//rectFill(1,1,12,12,7)
//print(2,2,state.sensor)
if (state.speed == 0) {
sprite(43,50,0)
sprite(50,50,1)
sprite(57,50,2)
sprite(64,50,3)
sprite(73,50,4)
rectFill(43,78, 46, 9, 1)
print(45, 80, 'Press Start', state.t * 7)
}
}
function updateAgents(state) {
for (let agent of state.agents) {
let sensor = 7
if (agent.dir == left) sensor = getPixel(agent.x - 1, agent.y)
if (agent.dir == right) sensor = getPixel(agent.x + 1, agent.y)
if (agent.dir == up) sensor = getPixel(agent.x, agent.y - 1)
if (agent.dir == down) sensor = getPixel(agent.x, agent.y + 1)
if (agent.speed > 0) agent.trail.push([agent.x, agent.y])
if (agent.trail.length > 16) agent.trail.shift()
if (sensor != 7) {
agent.speed = 0
} else {
let dice = random(0, 180)
if (dice == 1) agent.dir = (agent.dir + 1) % 4
if (dice == 2) agent.dir = (agent.dir + 3) % 4
}
let sensor2 = 7
if (agent.dir == left) sensor2 = getPixel(agent.x - 2, agent.y)
if (agent.dir == right) sensor2 = getPixel(agent.x + 2, agent.y)
if (agent.dir == up) sensor2 = getPixel(agent.x, agent.y - 2)
if (agent.dir == down) sensor2 = getPixel(agent.x, agent.y + 2)
if (sensor2 != 7) {
let dice = random(0, 2)
if (dice == 0) agent.dir = (agent.dir + 1) % 4
if (dice == 1) agent.dir = (agent.dir + 3) % 4
}
if (agent.dir == left) agent.x -= agent.speed
if (agent.dir == right) agent.x += agent.speed
if (agent.dir == up) agent.y -= agent.speed
if (agent.dir == down) agent.y += agent.speed
}
}
function drawAgents(state) {
for (let agent of state.agents) {
setPixel(agent.x, agent.y, 2)
for (let step of agent.trail) {
setPixel(step[0], step[1], 2)
}
if (agent.trail.length > 0) {
let last = agent.trail[0]
setPixel(last[0], last[1], 4)
}
}
}
function stopAgents(state) {
for (let agent of state.agents) {
agent.speed = 0
}
}
function startAgents(state) {
for (let agent of state.agents) {
agent.speed = 0.5
}
}
{
"iframeVersion": "0.1.280",
"lines": [
133,
61,
0,
0,
0,
0,
0,
0
]
}
{
"0": [
" ",
" 000000 ",
" 00 ",
" 00 ",
" 00 ",
" 00 ",
" 00 ",
" "
],
"1": [
" 0 ",
" 00000 ",
" 00 00 ",
" 00 00 ",
" 00000 ",
" 00 00 ",
" 00 00 ",
" 0 "
],
"2": [
" 000 ",
" 000 00 ",
" 00 0 ",
" 00 0 ",
" 00 0 ",
" 000 00 ",
" 0000 ",
" "
],
"3": [
" 00 00",
" 00 0 ",
" 000 0 ",
" 0000 0 ",
" 00 0 0 ",
" 00 00 ",
" 00 00 ",
" 00 00"
],
"4": [
" 0 00000",
"00 00000",
"0 00 ",
" 00 ",
" 00 ",
" 00 ",
" 00 ",
" 00 "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment