diff --git a/First.js b/First.js new file mode 100644 index 0000000000..2cb752d78f --- /dev/null +++ b/First.js @@ -0,0 +1,186 @@ +/* +@title: getting_started +@tags: ['beginner', 'tutorial'] +@addedOn: 2022-07-26 +@author: leo, edits: samliu, belle, kara + +Check the tutorial in the bottom right, the run button is in the top right. +Make sure to remix this tutorial if you want to save your progress! +*/ + +// define the sprites in our game +const player = "p"; +const box = "b"; +const goal = "g"; +const wall = "w"; + +// assign bitmap art to each sprite +setLegend( + [ player, bitmap` +................ +................ +.....8.......... +.....880CC...... +...88CC.CCC..... +....8C...CC0.... +....000.00.0.... +....0.0.0..0.... +....0......0.... +....00....0C.... +...C.C00000C.C.. +....CC0...0CC... +....000...000... +................ +................ +................`], + [ box, bitmap` +................ +................ +................ +...HH8887888HH.. +...H....7....H.. +...8....7....8.. +...8....7....8.. +...8....7....8.. +...77777677777.. +...8....7....8.. +...8....7....8.. +...8....7....8.. +...H....7....H.. +...HH8887888HH.. +................ +................`], + [ goal, bitmap` +................ +................ +................ +....777777...... +...77....77..... +...7......7..... +...7..7.7..7.... +...7.77777.7.... +...7..777..7.... +...77..7...7.... +....7......7.... +....77....77.... +.....777777..... +................ +................ +................`], + [ wall, bitmap` +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000 +0000000000000000`] +); + +// create game levels +let level = 0; // this tracks the level we are on +const levels = [ + map` +..p. +.b.g +....`, + map` +p.. +.b. +..g`, + map` +p.wg +.b.. +.... +..w.`, + map` +p... +...b +...b +.bbg`, + map` +... +.p. +...`, + map` +p.w. +.bwg +.... +..bg` +]; + +// set the map displayed to the current level +const currentLevel = levels[level]; +setMap(currentLevel); + +setSolids([ player, box, wall ]); // other sprites cannot go inside of these sprites + +// allow certain sprites to push certain other sprites +setPushables({ + [player]: [box], + [box]: [box] +}); + +// inputs for player movement control +onInput("s", () => { + getFirst(player).y += 1; // positive y is downwards +}); + +onInput("d", () => { + getFirst(player).x += 1; +}); + +onInput("a", () => { + getFirst(player).x += -1; +}); + +onInput("w", () => { + getFirst(player).y -= 1; +}); + +// input to reset level +onInput("j", () => { + const currentLevel = levels[level]; // get the original map of the level + + // make sure the level exists before we load it + if (currentLevel !== undefined) { + clearText(""); + setMap(currentLevel); + } +}); + +// these get run after every input +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, box).length; + + // if the number of goals is the same as the number of goals covered + // all goals are covered and we can go to the next level + 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 + // otherwise, we have finished the last level, there is no level + // after the last level + if (currentLevel !== undefined) { + setMap(currentLevel); + } else { + addText("you win!", { y: 4, color: color`D` }); + } + } +});