diff --git a/games/Amazing_Mazes.js b/games/Amazing_Mazes.js new file mode 100644 index 0000000000..470799cf15 --- /dev/null +++ b/games/Amazing_Mazes.js @@ -0,0 +1,260 @@ +/* +First time? Check out the tutorial game: +https://sprig.hackclub.com/gallery/getting_started +*/ + +const player = "p" +const wall = "c" +const goal = "e" +const background = "b" +const melody = tune` +112.78195488721805: E4-112.78195488721805 + G4-112.78195488721805, +112.78195488721805, +112.78195488721805: C5-112.78195488721805 + E5-112.78195488721805, +112.78195488721805: G4-112.78195488721805 + B4-112.78195488721805, +112.78195488721805: C5-112.78195488721805 + E5-112.78195488721805, +112.78195488721805: E5-112.78195488721805 + G5-112.78195488721805, +112.78195488721805, +112.78195488721805: C5-112.78195488721805 + E5-112.78195488721805, +112.78195488721805, +112.78195488721805: G4-112.78195488721805 + B4-112.78195488721805, +112.78195488721805, +112.78195488721805: E5-112.78195488721805 + G5-112.78195488721805, +112.78195488721805: B4-112.78195488721805 + D5-112.78195488721805, +112.78195488721805: E5-112.78195488721805 + G5-112.78195488721805, +112.78195488721805: G5-112.78195488721805 + B5-112.78195488721805, +112.78195488721805, +112.78195488721805: E5-112.78195488721805 + G5-112.78195488721805, +112.78195488721805, +112.78195488721805: C4-112.78195488721805 + E4-112.78195488721805, +112.78195488721805, +112.78195488721805: A4-112.78195488721805 + C5-112.78195488721805, +112.78195488721805: G4-112.78195488721805 + E4-112.78195488721805, +112.78195488721805: A4-112.78195488721805 + C5-112.78195488721805, +112.78195488721805: C5-112.78195488721805 + E5-112.78195488721805, +112.78195488721805: B4-112.78195488721805 + D5-112.78195488721805 + G5-112.78195488721805 + B5-112.78195488721805, +112.78195488721805: A4-112.78195488721805 + C5-112.78195488721805 + F5-112.78195488721805 + A5-112.78195488721805, +112.78195488721805: G5-112.78195488721805 + E5-112.78195488721805 + B4-112.78195488721805 + G4-112.78195488721805 + B5-112.78195488721805, +112.78195488721805: F5-112.78195488721805 + D5-112.78195488721805 + A4-112.78195488721805 + F4-112.78195488721805 + A5-112.78195488721805, +112.78195488721805: E5-112.78195488721805 + C5-112.78195488721805 + G4-112.78195488721805 + E4-112.78195488721805 + G5-112.78195488721805, +112.78195488721805: D5-112.78195488721805 + B4-112.78195488721805 + F4-112.78195488721805 + D4-112.78195488721805 + F5-112.78195488721805, +112.78195488721805: C5-112.78195488721805 + A4-112.78195488721805 + E4-112.78195488721805 + C4-112.78195488721805 + E5-112.78195488721805, +112.78195488721805: D5-112.78195488721805 + B4-112.78195488721805 + G4-112.78195488721805 + D4-112.78195488721805` +const move = tune` +37.5: B4-37.5, +37.5: D5-37.5, +37.5: G4-37.5, +1087.5` + + +setLegend([ player, bitmap` +..000000000000.. +..000000000000.. +0066666666666600 +0066006666006600 +0066006666006600 +0066006666006600 +0066006666006600 +0066666666666600 +0066666666666600 +0066666666666600 +0060666666660600 +0060066666600600 +0066000000006600 +0066666666666600 +..000000000000.. +..000000000000..` ], [ wall, bitmap` +3333303333303333 +3333303333303333 +0000000000000000 +3303333303333303 +3303333303333303 +0000000000000000 +3333303333303333 +3333303333303333 +0000000000000000 +3303333303333303 +3303333303333303 +0000000000000000 +3333303333303333 +3333303333303333 +0000000000000000 +3303333333303333` ], [ goal, bitmap` +CC000222000222.. +CC000222000222.. +CC000222000222.. +CC222000222000.. +CC222000222000.. +CC222000222000.. +CC000222000222.. +CC000222000222.. +CC000222000222.. +CC.............. +CC.............. +CC.............. +CC.............. +CC.............. +CC.............. +CC..............` ], [ background, bitmap` +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111 +1111111111111111`]) + + +setSolids([player, wall]) + +let level = 0 +const levels = [ + map` +cpcccccccc +c....c...c +c.cccc.c.c +c......c.c +c.c.cccc.c +c.c.ccc..c +c.c...c.cc +c.ccc.c..c +c...c.cc.c +ccccccccec`, + map` +cccccccpccccccc +c.....c.......c +c.ccc.ccccccc.c +c.c.c.c.....c.c +c.c.c.c.ccc.c.c +c.c.c.c.c.....c +c.c.ccc.ccccccc +c.c...........c +c.ccc.ccccccc.c +c.....c.....c.c +c.ccccc.c.c.c.c +c.c.....c.c.c.c +c.ccc.ccccc.c.c +c...c...c.....c +ccccccceccccccc`, + map` +ccccccccccpccccccccc +c...c....c.........c +c.c.c.cc.ccccccccc.c +c.c.c.c.....c....c.c +c.c.c.c.c.c.c.cccc.c +c.c.c.c.c.c.c......c +c.c...c.c.c.c.cccccc +c.ccccc.c.c...c....c +c.....c.c.ccc.cccc.c +c.ccc.c.c...c......c +c.c...c.ccc.cccccc.c +c.c.c.c...c......c.c +c.c.ccccc.cc.c.c.c.c +c.c.c...c..c.c.c.c.c +c.c...c.cccccc.c.c.c +c.ccc.c.c....c.c.c.c +c.....c.cccc.c.c.c.c +ccccccc.cccc.c.c.c.c +c..........c.c.c...c +cccccccccceccccccccc`, + map` +cccccccccccccccccccc +c........c.........c +c.cccc.cccccc.cccc.c +c.c....c......cccc.c +c.c.cc.ccccccccccc.c +c.c.c...c.ccc.cccc.c +c.c.c.c.c.....cccc.c +c.c.c.ccccccc.cccc.c +c.c.c.........cccc.c +c.c.ccccccccc.cccc.c +c.c...c.......cccc.c +c.ccccccccccc.cccc.c +c.c.........c.cccc.c +c.c.ccc.c.c.c.cccc.c +c.c...c.c.c.c......c +e.c.c.c.c.c.cccccc.c +c.c.c.c.c.c........c +c.c.ccc.c.cccccccc.c +c.c.....c..........p +cccccccccccccccccccc`, + map` +cccccccccccccccccccc +c...c....c.........c +c.c.c.cc.ccccccccc.c +c.c.c.c.....c....c.c +c.c.c.c.c.c.c.cccc.c +c.c.c.c.c.c.c......p +c.c...c.c.c.c.cccccc +c.ccccc.c.c...c....c +c.....c.c.ccc.cccc.c +c.ccc.c.c...c......c +c.c...c.ccc.cccccc.c +c.c.c.c...c......c.c +c.c.ccccc.cc.c.c.c.c +c.c.c...c..c.c.c.c.c +c.c...c.cc.ccc.c.c.c +c.ccc.c.c....c.c.c.c +c.....c.cccc.c.c.c.c +ccccccc.cccc.c.c.c.c +c........c...c.c...c +cccccccccceccccccccc`, +] +setBackground(background) +setMap(levels[level]) + + +setPushables({ + [ player ]: [] +}) + +onInput("w", () => { + getFirst(player).y += -1 + playTune(move) +}) +onInput("a", () => { + getFirst(player).x += -1 + playTune(move) +}) +onInput("s", () => { + getFirst(player).y += 1 + playTune(move) +}) +onInput("d", () => { + getFirst(player).x += 1 + playTune(move) +}) + +afterInput(() => { + // count the number of tiles with goals + const targetNumber = tilesWith(goal).length; + + // count the number of tiles with goals and boxes + const numberCovered = tilesWith(goal, player).length; + + if (numberCovered === targetNumber) { + // increase the current level number + level = level + 1; + + const currentLevel = levels[level]; + + // make sure the level exists and if so set the map + if (currentLevel !== undefined) { + setMap(currentLevel); + } else { + addText("you win!", { y: 4, color: color`2` }); + playTune(melody, 3) + } + } +}); + + diff --git a/games/chain_remove.js b/games/chain_remove.js new file mode 100644 index 0000000000..7377d93f17 --- /dev/null +++ b/games/chain_remove.js @@ -0,0 +1,198 @@ +/* +First time? Check out the tutorial game: +https://sprig.hackclub.com/gallery/getting_started +*/ + +const red="r",blue="b",green="g",cursor="c",bomb="*"; + +setLegend( + [ red, bitmap` +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333 +3333333333333333` ], + [ blue, bitmap` +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555 +5555555555555555` ], + [ green, bitmap` +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD +DDDDDDDDDDDDDDDD` ], + [ bomb, bitmap` +2222222222622262 +2222222222266622 +2222222220006622 +2222222200266622 +2222222002622262 +2222200000022222 +2222000000002222 +2222000000002222 +2222000000002222 +2222000000002222 +2222000000002222 +2222000000002222 +2222200000022222 +2222222222222222 +2222222222222222 +2222222222222222` ], + [ cursor, bitmap` +................ +................ +................ +........0....... +.......0.0...... +......0...0..... +.....0..0..0.... +........0....... +........0....... +........0....... +........0....... +........0....... +........0....... +................ +................ +................` ], +) + +setSolids([]) + +function arrayMap(map,pos){ + let m=``; + for(let i in map){ + for(let j in map[i]){ + if(i==pos.x&&j==pos.y)m+="c"; + else m+=map[i][j]; + } + m+="\n"; + } + //console.log(m); + setMap(m); +} +function restrictPos(pos){ + pos.x=pos.x<0?0:pos.x; + pos.x=pos.x>=20?19:pos.x; + pos.y=pos.y<0?0:pos.y; + pos.y=pos.y>=20?19:pos.y; +} +function checkFloating(grid){ + let flag=false; + for(let i in grid[0]){ + for(let j = grid.length-2;j>=0;j--){ + if(grid[j+1][i]=="."&&grid[j][i]!="."){ + grid[j+1][i]=grid[j][i]; + grid[j][i]="."; + //console.log(j+1,i,j,i); + flag=true; + } + } + } + if(flag)return checkFloating(grid) +} +function checkMissing(grid){ + for(let i in grid){ + for(let j in grid[i]){ + if(grid[i][j]=="."){ + if(Math.random()<0.02)grid[i][j]="*"; + else grid[i][j]=["r","g","b"][Math.floor(Math.random()*3)]; + } + } + } +} +function rootBlock(grid,pos){ + try{ + grid[pos.x][pos.y][0] + }catch(e){ + //out of bounds,ignore + return + } + removeBlock(grid,pos,grid[pos.x][pos.y]); +} +function removeBlock(grid,pos,type){ + //console.log(pos.x,pos.y); + if(type==".")return + grid[pos.x][pos.y]="."; + for(let i of [[-1,0],[1,0],[0,1],[0,-1]]){ + if(grid[pos.x+i[0]] && grid[pos.x+i[0]][pos.y+i[1]]==type){ + removeBlock(grid,{x:pos.x+i[0],y:pos.y+i[1]},type); + }else if(grid[pos.x+i[0]] && grid[pos.x+i[0]][pos.y+i[1]]==bomb){ + for(let xo=-1;xo<=1;xo++){ + for(let yo=-1;yo<=1;yo++){ + //console.log(pos.x+i[0]+xo,pos.y+i[1]+yo); + if(xo!=0||yo!=0)rootBlock(grid,{x:pos.x+i[0]+xo,y:pos.y+i[1]+yo}); + else grid[pos.x+i[0]][pos.y+i[1]]="."; + } + } + } + } +} +const pos={x:0,y:0}; +const grid=Array(20).fill(0).map(e=>Array(20).fill(".")); +checkMissing(grid); +arrayMap(grid,pos); + +setPushables({ + [ red ]: [] +}) + +onInput("w", () => { + pos.x--; +}) +onInput("s", () => { + pos.x++; +}) +onInput("a", () => { + pos.y--; +}) +onInput("d", () => { + pos.y++; +}) +onInput("k", () => { + rootBlock(grid,pos); +}) + +afterInput(() => { + restrictPos(pos); + checkFloating(grid); + checkMissing(grid); + //console.log(grid); + arrayMap(grid,pos); +}) diff --git a/games/img/Amazing_Mazes.png b/games/img/Amazing_Mazes.png new file mode 100644 index 0000000000..f26c80a6a2 Binary files /dev/null and b/games/img/Amazing_Mazes.png differ diff --git a/games/maise.js b/games/maise.js new file mode 100644 index 0000000000..944028511d --- /dev/null +++ b/games/maise.js @@ -0,0 +1,245 @@ +/* +@title: Maise +@author: UrMma +Instructions: +1. Use WASD keys to navigate around mases to reach the PR pad. +2. Stay away from fires otherwise you shall get burnt and the level will restart +3. Press i to reset +*/ +const player = "p"; +const endpad = "e"; +const wall = "a"; +const obs = "o"; +const bgmusic = tune` +114.50381679389314: E4^114.50381679389314 + B4^114.50381679389314, +114.50381679389314: G4^114.50381679389314, +114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314: D5^114.50381679389314, +114.50381679389314, +114.50381679389314: D5^114.50381679389314, +114.50381679389314: C5^114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314: A4^114.50381679389314, +114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314, +114.50381679389314: D5^114.50381679389314, +114.50381679389314: C5^114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314: A4^114.50381679389314, +114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314, +114.50381679389314: D5^114.50381679389314, +114.50381679389314: C5^114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314: A4^114.50381679389314, +114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314: D5^114.50381679389314, +114.50381679389314: C5^114.50381679389314, +114.50381679389314: B4^114.50381679389314, +114.50381679389314: A4^114.50381679389314, +114.50381679389314: G4^114.50381679389314, +114.50381679389314`; +const objhit = tune` +16000`; +setLegend( + [ player, bitmap` +......333....... +......333....... +......333....... +.......3........ +....3333333..... +...3..333..3.... +..3...333...3... +.3....333....3.. +.3....333....3.. +.....33333...... +.....3...3...... +.....3...3...... +.....3...3...... +.....3...3...... +.....3...3...... +.....3...3......`], +[endpad, bitmap` +4444444444444444 +4444444444444444 +4444433333334444 +4444366666663444 +4443363333363344 +4433363333363334 +4433363333363334 +4433366666663334 +4433363333333334 +4433363666663334 +4433363633363334 +4433363633663334 +4443333666633344 +4444333663333444 +4444433636334444 +4444444644644444`], +[wall, bitmap` +0000000000000000 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0777777777777770 +0000000000000000`], + [obs, bitmap` +......6..6...... +.....6.66..6.... +.....6666.6..... +...6666666.6.... +....6666666..... +...669999666.... +...669999666.... +...66999996..... +....6999996..... +....6633366..... +.....CCCCC...... +....C.C.C.C..... +...C.C.C.C.C.... +..C.C.C.C.C.C... +................ +................`] +); +const playback = playTune(bgmusic, Infinity); +setSolids([player,wall]); +let level = 0; +const levels = [ + map` +a.....a..ap +a.aaa.aoaa. +a.a.a.a.a.. +a.a.a.a...a +e.o.a...aaa`, + map` +p.a......a.o.aa +.aa.oaa.aa....a +..a..a.....aa.. +a.aaaa..a..a... +a.......a..a.o. +aa.o.aaaaaaaa.. +a.o.....ao.a... +aaaa.......a.oe`, + map` +aa.aaa....o....ap. +..o....a....oa.ao. +o...aaaaaaaaaa.aa. +aa.oa.aaaa.o...a.. +o...a.aaaa...aaa.. +aaa.a...........a. +aaa.aaaaa.ao...a.. +a...a.....a..aaa.a +a.aaa.aaaa..oa...o +a...a.....a..a.aaa +aaa.aaaa..aaa..aaa +ae..a...o......o.a`, + map` +....oa.a.aa..aaaa....p +.oo......ooa.aaaaao.oa +.aaaao.aa.aaaoaaa.a.aa +..oaao....a.....aa.... +o..oaaaaa...a.a.o..... +aa..a..a.aaaa.a..a.oo. +.o...o....a..a.o...aaa +.a.a.aa.a..a.a..aa.oaa +ao.a.aaao..aa.aaa..aaa +ooao.o.a.a....a.....oa +e....aoaaao..o...oa...`, + map` +e.aaaaaaaaaaaaop +a..aa...aooaaao. +ao.a....aoooaa.. +aa...aa.....ao.o +aa.oa.a.aaa.aa.. +aaooo.....o..aa. +aooooo.a...a.... +oooooooaaaaaaaaa`, + map` +aao...o...o...o... +aao.o.o.o.o.o.o.o. +aao.o.o.o.o.o.o.o. +aao.o.o.o.o.o.o.o. +aao.o.o.o.o.o.o.o. +aao.o.o.o.o.o.o.o. +....o.o.o.o.o.o.o. +.oo.o.o.o.o.o.o.o. +eoooo...o...o...op`, + map` +eooooooooooooooo +..oooooooooo.... +o..oooooooo..oo. +oo..oo...oo.ooo. +ooo....o....ooo. +ooooooooooooooo. +ooooooooooooooop +oooooooooooooooo`, + map` +e.oooooooooooooooooo +o.oooooooooooooo.... +o..oooooooooooo..oo. +oo..oooooooooo..ooo. +ooo..oooooooo..oooo. +oooo..oooooo..ooooo. +ooooo..oooo..oooooo. +oooooo..oo..ooooooo. +ooooooo....oooooooo. +ooooooooooooooooooop` +]; + +setMap(levels[level]); +let score = 0; +onInput("a", () => { + getAll(player).forEach(s => s.x -= 1) +}); +onInput("d", () => { + getAll(player).forEach(s => s.x += 1) +}); +onInput("w", () => { + getAll(player).forEach(s => s.y -= 1) +}); +onInput("s", () => { + getAll(player).forEach(s => s.y += 1) +}); +onInput("i", () => { + level = 0; + setMap(levels[level]); +}); +afterInput(() => { + clearText(); +const targetNum = tilesWith(endpad).length; +const numCov = tilesWith(endpad, player).length; + if (numCov === targetNum) { + level = level + 1; + if (level in levels) setMap(levels[level]); + else { + addText("YOU WIN!", { y: 6, color: color`0` }); + addText("Press i to restart",{y: 8, color:`0` }); + } + } + if(tilesWith(player, obs).length > 0){ + playTune(objhit); + addText("Try Again!",{y: 4, color: `0`}); + setMap(levels[level]); + } +}); +function wlevel() { + addText(`${level += 1}`, { + y: 1, + x: 9, + color: color`4` + }); +} diff --git a/games/metadata.json b/games/metadata.json index 37f251c33b..460b360b28 100644 --- a/games/metadata.json +++ b/games/metadata.json @@ -2790,5 +2790,29 @@ "img": "", "tags": ["WIP"], "added on": "2023-07-18" + }, + { + "filename": "run", + "title": "run", + "author": "Nguyễn Gia Bách", + "img": "", + "tags": ["logic"], + "added on": "2023-07-20" + }, + { + "filename": "chain_remove", + "title": "chain_remove", + "author": "imcute", + "img": "", + "tags": ["endless"], + "added on": "2023-07-22" + }, + { + "filename": "Amazing_Mazes", + "title": "Amazing_Mazes", + "author": "DLSFG", + "img": "", + "tags": ["sokoban-style"], + "added on": "2023-07-25" } ] diff --git a/games/run.js b/games/run.js new file mode 100644 index 0000000000..36ad4a17e3 --- /dev/null +++ b/games/run.js @@ -0,0 +1,276 @@ +/* +First time? Check out the tutorial game: +https://sprig.hackclub.com/gallery/getting_started +*/ + +const player = "p" + +var score = 0 + +var hit1 = false +var hit2 = false +var hit3 = false +var hit4 = false + +const rocket1 = "r" +const d1 = "left" + +const rocket2 = "s" +const d2 = "right" + +const rocket3 = "t" +const d3 = "up" + +const rocket4 = "u" +const d4 = "down" + +setLegend( + [ player, bitmap` +................ +.....777777..... +....77777777.... +....77777777.... +....77777777.... +....77777777.... +....77777777.... +....77777777.... +.....777777..... +.......77....... +.....777777..... +....7..77..7.... +...7..7..7..7... +......7..7...... +......7..7...... +................` ], + [ rocket1, bitmap` +................ +................ +.....333333..... +...3333333333... +...3333333333... +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +...3333333333... +...3333333333... +.....333333..... +................ +................`], + [ rocket2, bitmap` +................ +................ +.....333333..... +...3333333333... +...3333333333... +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +...3333333333... +...3333333333... +.....333333..... +................ +................`], + [ rocket3, bitmap` +................ +................ +.....333333..... +...3333333333... +...3333333333... +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +...3333333333... +...3333333333... +.....333333..... +................ +................`], + [ rocket4, bitmap` +................ +................ +.....333333..... +...3333333333... +...3333333333... +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +..333333333333.. +...3333333333... +...3333333333... +.....333333..... +................ +................`] +) + +setSolids([]) + +let level = 0 +const levels = [ + map` +r.......u +......... +......... +......... +....p.... +......... +......... +......... +t.......s`, + map` +.... +.... +.... +....`, + map` +.... +.... +.... +....` +] + +setMap(levels[level]) + +setPushables({ + [ player ]: [] +}) + +onInput("s", () => { + if (level == 0) + getFirst(player).y += 1 +}) + +onInput("w", () => { + if (level == 0) + getFirst(player).y -= 1 +}) + +onInput("a", () => { + if (level == 0) + getFirst(player).x -= 1 +}) + +onInput("d", () => { + if (level == 0) + getFirst(player).x += 1 +}) + +afterInput(() => { + if (level == 1) { + level = 0 + clearText() + setMap(levels[level]) + setTimeout(update,500) + } + if (level == 2) { + level = 0 + clearText() + setMap(levels[level]) + setTimeout(update,500) + } +}) + +level = 2 +setMap(levels[level]) +addText('Press any button',{x: 2, y: 7, color: color`3`}) +addText('to start',{x: 6, y: 8, color: color`3`}) + +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +function die() { + level = 1 + setMap(levels[level]) + clearText() + addText('Your score is:'+score,{x: 2, y: 6, color: color`3`}) + addText('Press any button',{x: 2, y: 7, color: color`3`}) + addText('to play again',{x: 4, y: 8, color: color`3`}) + score = 0 +} + +function update() { + if (hit1){ + getFirst(rocket1).x = 0 + getFirst(rocket1).y = getRandomInt(1,height()-1) + hit1 = false + } + + if (hit2){ + getFirst(rocket2).x = width()-1 + getFirst(rocket2).y = getRandomInt(1,height()-1) + hit2 = false + } + + if (hit3){ + getFirst(rocket3).y = height()-1 + getFirst(rocket3).x = getRandomInt(1,width()-1) + hit3 = false + } + + if (hit4){ + getFirst(rocket4).y = 0 + getFirst(rocket4).x = getRandomInt(1,width()-1) + hit4 = false + } + + getFirst(rocket1).x += 1 + if (getFirst(rocket1).x == width()-1) { + hit1 = true + } + + getFirst(rocket2).x -= 1 + if (getFirst(rocket2).x == 0) { + hit2 = true + } + + getFirst(rocket3).y -= 1 + if (getFirst(rocket3).y == 0) { + hit3 = true + } + + getFirst(rocket4).y += 1 + if (getFirst(rocket4).y == height()-1) { + hit4 = true + } + + if (getFirst(player).x == getFirst(rocket4).x + && getFirst(player).y == getFirst(rocket4).y) { + die() + return + } + + if (getFirst(player).x == getFirst(rocket3).x + && getFirst(player).y == getFirst(rocket3).y) { + die() + return + } + + if (getFirst(player).x == getFirst(rocket2).x + && getFirst(player).y == getFirst(rocket2).y) { + die() + return + } + + if (getFirst(player).x == getFirst(rocket1).x + && getFirst(player).y == getFirst(rocket1).y) { + die() + return + } + + score += 1 + addText(''+score) + + setTimeout(update,500) +}