Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

Camp Imagination Videogame Design Arena Shooter: Sound

Sound Effects

This lesson picks up where the Arena Shooter: Text lesson left off. It's time to add some sound to the game.

Play Audio Action
  1. Right-click on Sounds in the resource tree.
  2. Choose "Create" to open the Sound Editor.
  3. Name your sound "sndDestroy."
  4. Import the "explosion" sound from the "TutorialResources" folder.
  5. Open the Step event of the enemy object.
  6. Under the Set Score action, add a Play Audio action.
  7. Choose the sound file you created.
  8. Before the Play Audio action, add a Set Audio Pitch action.
  9. Select the sound, and set the pitch to "random_range(0.8, 1.2)

In general, you want to use .wav files for sound effects and .mp3 files for music. The random_range will set the pitch to a random number between 0.8 and 1.2 so the sound doesn't always sound the same.

Title Screen

Scrolling Background

Let's create a title screen that will appear before we start the game.

  1. Create a new sprite called "sprTitleBackground."
  2. Import the "bg_dark_tile" image from the "TutorialResources" folder.
  3. Create another new sprite called "sprTitleScreen."
  4. Import the "titlescreen" image from the "TutorialResources" folder.
  5. Change the origin of this sprite to "Middle Centre."
  6. Right-click on Rooms in the resource tree.
  7. Choose "Create" to open the Room Editor.
  8. Name your room "roomTitle."
  9. In the resource tree, drag the new room above "mainRoom" so it loads first.
  10. Select the "Background" layer of the new room and open the "Background Properties" section.
  11. Choose the "sprTitleBackground" sprite for the background.
  12. Check the horizontal and vertical tile checkboxes.
  13. Set the "Horizontal Speed" to "-2" to make the background scroll to the left.

If you run your game you should see the scrolling background. Now we need to create an object for the title screen text.

Set Instance Alpha Action
  1. Create a new object called "objTitle" and select the "sprTitleScreen" sprite.
  2. Add the new object to the center of the room.
  3. Add a Create event to the title object.
  4. Add a Set Instance Alpha action to the new event.
  5. Set the alpha to "0" to make the title start invisible.
  6. To make sure the title is in the exact center, add a Jump To Point action and set "X" to "room_width/2" and "Y" to "room_height/2."
  7. Add a Step event to the title object.
  8. Add a Set Instance Alpha action and set the alpha to relative .02.
  9. Add an If Variable action and check if "image_alpha" is greater than "1."
  10. Attach a Set Instance Alpha action to the if and set the alpha to "1."

The title should now fade in. Finally, we want to move to the next room when the "Enter" key is pressed.

  1. Add a Key Up event for the "Enter" key.
  2. Add the Go To Room action.
  3. Choose "roomMain" for the action.

Spawning Enemies

Set Instance Scale Action

Right now the game only has the few enemies you placed in the room and when you destroy them nothing happens. Let's make it saw enemies randomly spawn in the room.

  1. Remove the enemies from your main room.
  2. Create a new instance layer called "Enemies" in the main room and drag it below the "Instances" layer.
  3. Create a new object called "objEnemySpawn" and assign it the enemy sprite.
  4. In the Create event of this new object, add a Set Instance Scale action.
  5. Set the horizontal and vertical scales to "0.1."
  6. Create a Step event for this new object.
  7. Add a Set Instance Scale action to the Step event.
  8. Change the horizontal and vertical scales to relative "0.02"
  9. Add an If Variable action and check if "image_x" is greater than "1."
  10. To the right of the If Variable action, add a Set Instance Scale action and set both scales to "1."
  11. Also to the right of the If Variable action, add a Change Instance action and select the original enemy object.
  12. Go to the main room, and drag as many enemy spawners into the room as you want. This will represent the maximum number of enemies that can be in the room at once.

If you run your game, the enemies should spawn, but once you destroy them they are gone. Follow the steps below to make the enemies respawn.Get Random Number Action

  1. Create an object called "objSpawner."
  2. In the new object's Create event, add an Assign Variable action.
  3. Name the variable "spawnRate" and give it a value of "60."
  4. Also add a Set Alarm Countdown action and set alarm 0 to a countdown of "spawnRate."

An alarm runs when it counts down to 0. Now we need to add an event for alarm 0 to cause the enemies to spawn at a random location.

  1. Add an event for Alarm 0.
  2. Add two Get Random Number actions from the "Random" section.
  3. Call one random number "rndX" and set the maximum to "room_width."
  4. Call the other random number "rndY" and set the maximum to "room_height."
  5. Check "Temp" for both random numbers.
  6. Add a Create Instance action and choose "objEnemySpawn."
  7. Set "X" to "rndX" and "Y" to "rndY" on the "Enemies" layer.
  8. Add a Set Alarm Countdown action for alarm 0 with a countdown of "spawnRate."
  9. Remove the "objEnemySpawn" objects from the room and add a single "objSpawner" object.