Contenido del Curso
Ninja Game
Ninja Game
Nested Loops
In this chapter, we will explore the concept of nested loops. Nested loops are loops within loops, and they are a powerful tool in programming that allows us to perform repeated actions in a structured way. By using nested loops, we can efficiently navigate and manipulate complex structures, such as grids, which is exactly what our Ninja will be doing in this chapter.
Imagine our Ninja moving through a grid, collecting sushi. The grid is made up of rows and columns, and our goal is to collect all the sushi scattered across this grid. To achieve this, we can use nested loops: one loop to move across the rows and another loop to move through each column.
Let's take a look at an example to understand how nested loops work in the context of our Ninja Game:
ninja.js
index.html
preset.js
In the example above, we have two functions: ninjaController
and lootColumn
. The ninjaController
function uses a while
loop to move the Ninja across the grid from left to right. For each column, it calls the lootColumn
function, which contains another while
loop to move the Ninja upwards, collecting sushi as it goes. Once the Ninja reaches the top of the column, it uses a for
loop to move back down to its original position.
By nesting the while
loop inside the lootColumn
function within the while
loop of the ninjaController
function, we can efficiently collect all the sushi in each column before moving to the next one.
Swipe to show code editor
Goal: Collect all sushi on the map.
Use nested loops to navigate through the grid and collect all the sushi.
Solución
Starter Map
¡Gracias por tus comentarios!
ninja.js
index.html
preset.js
Nested Loops
In this chapter, we will explore the concept of nested loops. Nested loops are loops within loops, and they are a powerful tool in programming that allows us to perform repeated actions in a structured way. By using nested loops, we can efficiently navigate and manipulate complex structures, such as grids, which is exactly what our Ninja will be doing in this chapter.
Imagine our Ninja moving through a grid, collecting sushi. The grid is made up of rows and columns, and our goal is to collect all the sushi scattered across this grid. To achieve this, we can use nested loops: one loop to move across the rows and another loop to move through each column.
Let's take a look at an example to understand how nested loops work in the context of our Ninja Game:
ninja.js
index.html
preset.js
In the example above, we have two functions: ninjaController
and lootColumn
. The ninjaController
function uses a while
loop to move the Ninja across the grid from left to right. For each column, it calls the lootColumn
function, which contains another while
loop to move the Ninja upwards, collecting sushi as it goes. Once the Ninja reaches the top of the column, it uses a for
loop to move back down to its original position.
By nesting the while
loop inside the lootColumn
function within the while
loop of the ninjaController
function, we can efficiently collect all the sushi in each column before moving to the next one.
Swipe to show code editor
Goal: Collect all sushi on the map.
Use nested loops to navigate through the grid and collect all the sushi.
Solución
Starter Map
¡Gracias por tus comentarios!