MakeCode Arcade Explorer Help

I’m new to MakeCode Arcade and I don’t really know how the explorer works. I want to use it so I can have two code files in one project and be able to switch between them. Is that possible and can someone explain how the file explorer works?
Thanks

1 Like

Welcome, @Mastermind !

For those of you who are unfamiliar with what @Mastermind is describing, the explorer in MakeCode appears below the simulator when you switch from Blocks to a typing language (i.e., JavaScript or Python).

The explorer shows all of the files that are included in your project, including any extensions that you’ve added to your project. It also gives you the ability to update and remove extensions, which you can’t do in Blocks.

The explorer also allows you to add code files to your project if you want to split your code into multiple files, rather than having all of your code in a single file. It helps to organize your code a bit more when your projects become really complex.

@Mastermind, splitting your code into multiple files does not allow you to turn code “on” and “off.” It’s just a way of organizing your code. In the next two posts, I’ll show you two different ways to turn code “on” and “off.” It depends a little bit on what you’re asking, whether you’re asking to disable code completely or if you want to disable and enable some code in the middle of a game.

2 Likes

Now, if you want to disable sections of code completely, you can do that … whether your code is in a single file or in multiple files.

In JavaScript, use the block comment markers, /* and */. Here is an example:

// The following code will run.
let mySprite: Sprite = sprites.create(img`.`, SpriteKind.Player)
mySprite.x = 50
mySprite.y = 50

// The following code will not run.
/*
for (let x: number = 0; x < 100; x++) {
    game.splash("x = " + x)
}
*/

In Python, use triple-quotes to disable code. Here is the same example in Python:

my_sprite: Sprite = sprites.create(img("""."""), SpriteKind.player)
my_sprite.x = 50
my_sprite.y = 50

# The following code will not run.
"""
for x in range(100):
    game.splash("x = " + str(x))
"""
1 Like

If, instead, you want to disable blocks of code “on demand,” then the easiest way to do that is to put the blocks of code into functions, and then just call those different functions when you want the code to run. It doesn’t work for all code, but it does for a lot of it!

You could do this if you have several mini games and want to switch among them.

For example:

game1.js

function startGame1() {
    // Put your main game code for Game 1 here.
    let heroSprite: Sprite = sprites.create(assets.image`duck`, SpriteKind.Player)
    heroSprite.setFlag(SpriteFlag.StayInScreen, true)
    controller.moveSprite(heroSprite, 25, 25)
    game.onUpdateInterval(3000, createFood)
    sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, onOverlapPlayerEnemy)
    sprites.onOverlap(SpriteKind.Player, SpriteKind.Food, onOverlapPlayerFood)
    info.setScore(0)
    info.setLife(3)
    info.startCountdown(120)
}

function endGame1() {
    heroSprite.destroy()
    heroSprite = null
    for (let e of sprites.allOfKind(SpriteKind.Enemy)) {
        e.destroy()
    }
    for (let f of sprites.allOfKind(SpriteKind.Food)) {
        f.destroy()
    }
}

Notice the endGame1() function. You’ll want to clean up the screen and destroy any existing sprites before switching games.

You’d do something similar for each of your mini games.

Then, in main.ts, you could do something like this:

main.ts

const SWITCH_GAMES_TIME_MINUTES = 5
const SWITCH_GAMES_TIME_MILLISECONDS = SWITCH_GAMES_TIME_MINUTES * 60 * 1000

startGame1()
// Get the "game time" when we start the game.
let timeStart: number = game.runtime()

game.onUpdate(function() {
    if (game.runtime() - timeStart > SWITCH_GAMES_TIME_MILLISECONDS) {
        endGame1()
        startGame2()
    }
})

This is just one way to have multiple games stored in one project with the ability to switch from one to another. There are other ways to accomplish something similar.

Feel free to ask any other questions that you may have!

Thanks for the response!
I guess I should have been a little bit more specific. I’m mainly confused about how to access code in a file other than main.py. For example, if I have a function defined in a second file, is there a way to access it from the main file? Or is there a way to select which file to run at a certain time?

In the end, I want to have two separate files, each containing a different game, and allowing the user to select which game they would like to play and then run that file.

Here’s an example for the JavaScripters out there:

Here’s the start of a similar project in Python:

It’s late and Python is being finicky. I’ll try again tomorrow when I’m less sleepy. :smile:

Thank you for all your effort!
I think my confusion might be coming from trying to use Python. Does MakeCode Arcade just have less Python support?

1 Like

Python is the second language that was implemented in the MakeCode editors, and some of the complex items are not yet implemented.

I’m going to create some new threads on Monday to ask some questions of the developers. You’ll see links to those new threads here. I’ll be back when I can get more information and put a response together. Thanks for your patience, @Mastermind !

1 Like

OK; let’s try this again. :laughing:

Here’s an example for the JavaScripters out there:

And here’s an example for the Pythoners:

Multi-file projects are not yet working in Python, so all of your code needs to go in the main.py file for the time being. When you’re developing multiple games, though, you could create extra files to move your code “out of the way” so that you can focus your attention on just one of the games. The Python code in those extra files will be disabled and will not run. (Again, this is how it works now. Future versions of MakeCode Arcade may work differently.)

@Mastermind , be sure to ask any questions that you still have. Thanks again for your patience!

1 Like

Ok, sounds good. Thanks for all the help!

1 Like