Copy template

Goal: Create a copy of the project template
  1. Click this link to make a remix: PROJECT LINK

Set Up the Game

Goal: Create the title screen and initialize the game variables.
  1. Place a "set background to" block under the "when run" block and select a background image.
  2. Add a "show title screen" block under the background block.
  3. Set the title to "Crossbow defence" and the subtitle to "Press "A" to start".
  4. Drag out a "set hasStartGame to" block to the bottom of the "when run" block and set the value to "false".

Create the Crossbow

Goal: Set the crossbow sprite and its movement speed.
  1. Add a "createCrossbow" function block.
  2. Place a "make new" sprite block at location (203, 101).
  3. Add a "set" sprite "speed" to 12 block.

Define Enemy Movement

Goal: Define how a sprite moves downward across the screen.
  1. Add a "move enemy south" function block that contains a "move" this sprite "speed" pixels "South" block.
  2. Add an "if" block checking if "y position" is less than -10.
  3. Inside the "if" block, add a "remove" "this sprite" block.

Spawn Enemies

Goal: Define how enemies appear and start moving.
  1. Add a "spawnEnemy" function block.
  2. Place a "make new" enemy sprite at location x: "random integer from 20 to 380" and y: 400.
  3. Add a "set" enemy sprite "speed" to "enemySpeed" variable.
  4. Set the "size" of the enemy sprite to 50.
  5. Add a "sprite" begins "move enemy south" block.

Handle Start Input

Goal: Create an event that starts the game when the "A" key is pressed.
  1. Drag out a "when" block, change the input to "a", and set it to "pressed".
  2. Add an "if" block inside the event.
  3. Place a "hasStartGame = false" block into the "if" condition.
  4. Add a "startGame" block inside the "do" section of the "if" block.

Start Game Logic

Goal: Set up the game environment and variables when the game starts.
  1. Add a "startGame" function block.
  2. Place a "hide variable" block for "score".
  3. Add a "hide title screen" block.
  4. Place a "set background to" block.
  5. Add a "make new" sprite block at location (197, 82).
  6. Set the size of the new sprite to 400.
  7. Add a "createCrossbow" block.
  8. Set the variables "arrows" to 10, "wall hp" to 3, "score" to 0, and "enemySpeed" to 3.
  9. Add "show variable" blocks for "arrows" at (29, 9) and "wall hp" at (369, 13).
  10. Place a "spawnEnemy" block.
  11. Set "hasStartGame" to "true".

Move the Crossbow

Goal: Make the player able to move the crossbow left and right.
  1. Create the "moveCrossbowLeft" and "moveCrossbowRight" functions.
  2. Add two "while" blocks to your workspace.
  3. Change the first "while" block to "left" and "pressed".
  4. Attach a "moveCrossbowLeft" block inside the "left" while block.
  5. Change the second "while" block to "right" and "pressed".
  6. Attach a "moveCrossbowRight" block inside the "right" while block.
  7. Add a "moveCrossbowLeft" function block that contains a "move" this sprite "speed" pixels "West" block and an "edges block" from moving block.

Create the Shoot Function

Goal: Define how an arrow is created and fired from the crossbow.
  1. Add a "shootArrow" function block.
  2. Add an "if" block checking if "hasStartGame" is "true".
  3. Inside the "if" block, add another "if" block checking if "arrows" is greater than 0.
  4. Inside the second "if" block:
    • Place a "make new" arrow sprite at the "location of" the crossbow sprite.
    • Set the "size" of the arrow sprite to 50.
    • Set the "speed" of the arrow sprite to 30.
    • Add a "sprite" begins "moving north" block.
    • Add a "play sound" block and set it to "Projectile: retro_game_weapon_-slinky_laser_3".
    • Add a "change" "arrows" block by "-1".
  5. In the "else" section of the "arrows > 0" if block:
    • Add a "say" block for the crossbow sprite to say "No more arrows!" for 2 seconds.
    • Add a "play sound" block and set it to "Alerts: playful_game_error_sound_4".

Shoot Arrows

Goal: Allow the player to shoot arrows by pressing the spacebar.
  1. Drag out a "when" block, set it to "space" and "pressed".
  2. Add the "shootArrow" function block inside the event.

Monsters Attack

Goal: Handle what happens when a monster touches the wall.
  1. Add a "when" block set to when an orange monster sprite "touches" the wall.
  2. Place a "remove" "subject" block (the monster).
  3. Add a "spawnEnemy" block.
  4. Add a "play sound" block and set it to "Explosion: playful_game_explosion_5".
  5. Add a "change" "wall hp" block by "-1".
  6. Add an "if" block with a condition "wall hp = 0".
  7. Create a Game over function
  8. Inside the "if" block, add the "gameOver" function.

Arrow Hits Monster

Goal: Handle what happens when an arrow hits a monster.
  1. Add a "when" block set to when an arrow "touches" an orange monster.
  2. Place a "play sound" block and set it to "Hits: retro_game_simple_impact_3".
  3. Add two "remove" blocks: one for the "object" (the monster) and one for the "subject" (the arrow).
  4. Add a "spawnEnemy" block.
  5. Add a "change" "arrows" block by "2".
  6. Add a "change" "enemySpeed" block by "1".
  7. Add a "change" "score" block by "1".

🎉 Congratulations!

Congratulations! You've built your own Crossbow Defence game.
🏆

Next challenge!

Next challenge ideas:
  1. Add different types of monsters with different speeds.
  2. Create a power-up that makes the player invincible for a few seconds.
  3. Create a heart item that increases player health when shot.
🚀