Welcome to our exciting coding lesson! Today, we are going to create a fun game called 'Avoid the Enemy'. In this game, you will control a character that needs to avoid enemies appearing on the screen. We will learn how to create and control characters, called sprites, and how to make them interact with each other. Let's dive in and start coding our game!
Open the MakeCode Arcade website using the link below and create a new project. You can call the project whatever you want.
Create a new Arcade project using the makecode.com website.
Next, you will create your player sprite. This is the character that you will control in the game. Add the following code:
let mySprite = sprites.create(img`...`, SpriteKind.Player)
Then click on the grey box and choose a sprite character from the gallery. In our example we will choose a duck.
let mySprite = sprites.create(img` . . . . . . . . . . . . . . . . . . . . . . . . . . b 5 b . . . . . . . . . . . . b 5 b . . . . . . . . . . b b b b b b . . . . . . . . . b b 5 5 5 5 5 b . . . . b b b b b 5 5 5 5 5 5 5 b . . . b d 5 b 5 5 5 5 5 5 5 5 b . . . . b 5 5 b 5 d 1 f 5 d 4 f . . . . b d 5 5 b 1 f f 5 4 4 c . . b b d b 5 5 5 d f b 4 4 4 4 4 b b d d c d 5 5 b 5 4 4 4 4 4 b . c d d d c c b 5 5 5 5 5 5 5 b . c b d d d d d 5 5 5 5 5 5 5 b . . c d d d d d d 5 5 5 5 5 d b . . . c b d d d d d 5 5 5 b b . . . . . c c c c c c c c b b . . . `, SpriteKind.Player)
Click on the gray box in the sprite block to open the Editor. You can choose a sprite from the Gallery or you can paint your own sprite using the Editor.
Now, let's make your sprite move using the joystick (or the arrow keys on your keyboard).
Add the following new code:
let mySprite = sprites.create(img` . . . . . . . . . . . . . . . . . . . . . . . . . . b 5 b . . . . . . . . . . . . b 5 b . . . . . . . . . . b b b b b b . . . . . . . . . b b 5 5 5 5 5 b . . . . b b b b b 5 5 5 5 5 5 5 b . . . b d 5 b 5 5 5 5 5 5 5 5 b . . . . b 5 5 b 5 d 1 f 5 d 4 f . . . . b d 5 5 b 1 f f 5 4 4 c . . b b d b 5 5 5 d f b 4 4 4 4 4 b b d d c d 5 5 b 5 4 4 4 4 4 b . c d d d c c b 5 5 5 5 5 5 5 b . c b d d d d d 5 5 5 5 5 5 5 b . . c d d d d d d 5 5 5 5 5 d b . . . c b d d d d d 5 5 5 b b . . . . . c c c c c c c c b b . . . `, SpriteKind.Player) controller.moveSprite(mySprite)
To stop your sprite from moving off the screen, add the following new code:
let mySprite = sprites.create(img` . . . . . . . . . . . . . . . . . . . . . . . . . . b 5 b . . . . . . . . . . . . b 5 b . . . . . . . . . . b b b b b b . . . . . . . . . b b 5 5 5 5 5 b . . . . b b b b b 5 5 5 5 5 5 5 b . . . b d 5 b 5 5 5 5 5 5 5 5 b . . . . b 5 5 b 5 d 1 f 5 d 4 f . . . . b d 5 5 b 1 f f 5 4 4 c . . b b d b 5 5 5 d f b 4 4 4 4 4 b b d d c d 5 5 b 5 4 4 4 4 4 b . c d d d c c b 5 5 5 5 5 5 5 b . c b d d d d d 5 5 5 5 5 5 5 b . . c d d d d d d 5 5 5 5 5 d b . . . c b d d d d d 5 5 5 b b . . . . . c c c c c c c c b b . . . `, SpriteKind.Player) controller.moveSprite(mySprite) mySprite.setStayInScreen(true)