ITP 134 Programming Notes Chapter 8

Total Page:16

File Type:pdf, Size:1020Kb

ITP 134 Programming Notes Chapter 8

ITP 134 Programming Notes – Chapter 8 ITP 134 C++ Programming Notes

Date: Thursday April 1, 2010 Instructions: Use these notes to give you the important points of this chapter. See the book for lots of great programming examples. Chapter 8 Playing Sound Effects and Music

8.1 Introduction We have learned a lot of programming topics in the previous chapters. In this chapter we will put an entire game together including background music, sound effects, a bit of physics and more text effects. (pg 345)

8.2 Playing Sound Effects and Music CONCEPT: The Dark GDK lets you play audio files that have been saved in the WAV, MIDI, or MP3 formats, as well as music tracks on an audio CD. The library provides numerous functions for working with these audio files. (pg 345) This table summarizes the functions.  # means SoundNumber or MusicNumber  Sound functions are used for .wav files.  Music functions are used for .midi and .mp3 files. Max of 32 files loaded into memory for music files. Operation Sound Function Soun Music Function Music d Page Page Load sound or music into dbLoadSound(filename, #) 346 dbLoadMusic(filename, #) 358 memory Play dbPlaySound(#) 347 dbPlayMusic(#) 358 Loop dbLoopSound(#) 350 dbLoopMusic(#) 358 Stop sound or music dbStopSound(#) 352 dbStopMusic(#) 358 Delete sound or music from dbDeleteSound(#) 353 dbDeleteMusic(#) 359 memory Pause dbPauseSound(#) 353 dbPauseMusic(#) 359 Resume dbResumeSound(#) 353 dbResumeMusic(#) 359 Pan dbSetSoundPan(#, pan) 355 n/a -10,000 < pan < 10,000 Set the volume dbSetSoundVolume(#, 354 dbSetMusicVolume(#, 360 volume) volume) 0 < volume < 100 0 < volume < 100 Set the playing speed dbSetSoundSpeed(#,speed) 355 dbSetMusicSpeed(#,speed) 360 100 < speed < 100,000 100 < speed < ? Clone dbCloneSound(#1, #2) 354 n/a Mrs. Eaton Page 1 ITP 134 Programming Notes – Chapter 8

Operation Sound Function Soun Music Function Music d Page Page Load a track from a CD n/a dbLoadCDMusic(track, #) 360 Determine number of n/a dbGetNumberOfCDTracks(#) 361- tracks on CD 362 Determine whether a dbSoundExist(#) 353 dbMusicExist(#) 359 sound or music in memory Determine whether a dbSoundPlaying(#) 352 dbMusicPlaying(#) 358 sound or music is playing Determine whether a dbSoundLooping(#) 352 dbMusicLooping(#) 359 sound or music is looping Determine whether a dbSoundPaused(#) 354 dbMusicPause(#) 359 sound or music is paused Determine the pan value dbSoundPan(#) 357 n/a Determine the playing dbSoundSpeed(#) 355 dbMusicSpeed(#) 360 speed Determine the volume dbSoundVolume(#) 354 dbMusicVolume(#) 360 setting

8.3 Simulating Falling Objects CONCEPT: When an object in the real world falls to Earth, it speed increases as it falls. If you want to write a program that realistically simulates falling objects, you will need to incorporate this acceleration into your program. (pg 364-366) Gravity is the force that attracts objects to one another. Acceleration is an object’s increase in speed as it falls. When an object falls in a vacuum its speed increases at a rate of 9.8 meters per second each section. See Figure 8-2 Speed of a falling brick on page 365 for an example.) This formula is used to calculate the distance that a falling objects falls: Distance = ½ g t2 Where distance is in meters, g is gravity or 9.8, and t is time in seconds In code we are using ACCELERATION = .98 to approximate pixels per second distance = .5 * ACCELERATION * time * time

See Program 8-8 FreeFall.cpp for a simulation of a falling ball. (pg 367-368)

See Program 8-9 DualMotion.cpp for a simulation of a ball that is thrown from the top right corner. (pg 371 – 373) Figure 8.5 shows 4 frames of the program screens. (pg 373)

Sample program called DropTheBall provides a more in-depth example of horizontal and vertical motion. Download this file from the textbook web site.

Mrs. Eaton Page 2 ITP 134 Programming Notes – Chapter 8

Mrs. Eaton Page 3 ITP 134 Programming Notes – Chapter 8

8.4 Text Effects CONCEPT: You can change the font, size and style of text that is displayed by the dbPrint, dbText, and dbCenterText functions. (pg 374)

Functions that change font, style for text. This table summarizes the functions to set text font, size and styles. (pg 374 – 381)  fontName is a string  Text by default is displayed in a rectangular area with a transparent background. (pg 377)  Use the dbInk function to change the foreground and background color (Chapter 3)

Text operation Text Function Page # Change the current font dbSetTextFont(fontName) 374 Change the current text size dbSetTextSize(size) 374 size is in points. 72 points = 1 inch Change to bold font dbSetTextToBold; 375 Change to italic dbSetTextToItalic; 375 Change to bold & italic dbSetTextToBoldItalic; 375 Text back to non-bold and non-italic dbSetTextToNormal; 375 Text on opaque (non-transparent) background dbSetTextOpaque(); 377 Default background is black Use dbInk to set background color Text back to transparent background dbSetTextTransparent(); 378 Default is transparent unless you change it with to opaque above. Set the text cursor position dbSetCursor(x,y); 379- 380

Mrs. Eaton Page 4 ITP 134 Programming Notes – Chapter 8

Functions that return values to get text data Text operation Text Function Page # Get current text size dbFontSize(); 374 returns integer value Get current font dbTextFont(); 375 returns the current font as a string Get current text width dbTextWidth(string); 378 Get current text height dbTextHeight(string); 378 Get current text style. Check if bold or italic. dbTextStyle(); 375 return 0 if normal returns 1 if italic return 2 if bold return 3 if bold and italic Get current text background transparency setting dbTextBackgroundType(); 378 returns integer 1 if transparent returns integer 0 if opaque

8.5 The Vulture Trouble Game Game Description: A greedy vulture has stolen eggs from a farmer’s hen house. The vulture realizes he’s been caught, and is dropping the eggs one by one. The player’s objective is to use a basket to catch as many of the eggs as possible. If the player does not catch an egg, it hits the ground and breaks. Score 1000 points for each egg caught. (pg 382)

Images for the game (see color insert Figure 8-12)  farm.bmp = farm background for the game screen  vulture.bmp = sprite sheet for flying vulture animation  egg.bmp = egg image used for dropped eggs and status bar across top of screen  basket.bmp = basket to catch eggs  hitBasket.bmp = displays briefly when you catch an egg in the basket  brokenEgg.bmp = displays briefly when an egg hits the ground

Sounds for the game  vultureTrouble.bmp = intro screen music  vultureLevel.bmp = game background music  pop.wave = sound effect when catch egg in basket  clap.wav = sound effect when egg hits ground  type.wav = sound effect for text on Game Over screen  complete.wav = sound effect for Game Over screen  vulurePerfect.wav = sound effect if player catches every egg

Story board for Game (see color insert Figure 8-10 and 8-11) Title Intro Game Game Screen Screen Screen Over

Mrs. Eaton Page 5 ITP 134 Programming Notes – Chapter 8

Vulture Trouble Game Functions  Global constant declarations -> Pages 383-384 Purpose: Declarations for all the images, sprites, sounds, music, time and other constants  DarkGDK function -> Pages 384-385 Purpose: main function that calls all the other functions that control the game. Includes the primary game loop.  loadResources function -> Pages 386 – 387 Purpose: loads all the images and audio files into memory and assigns the image and audio numbers.  intro function -> Pages 387 – 388 Purpose: Displays the title screen, waits for the user to press a key, then displays the intro screen, plays the intro music, then waits for the user to press a key to play the game.  createSprites function -> Pages 388 – 391 Purpose: Creates the sprites used in the game: farm background, basket, egg, hit_basket, broken_egg and animated vulture.  moveBasket function -> Pages 391 - 392 Purpose: Detects user pressing the left and right arrow keys and moves the basket right or left.  moveVulure function -> Pages 392 – 393 Purpose: Moves the vulture back and forth across the top of the screen with the animated sprite.  moveEgg function -> Pages 394 – 395 Purpose: Moves the egg sprite across and down the screen as vulture drops it.  checkCollisions function -> Pages 396 – 397 Purpose: Checks to see if the egg hits the basket or hits the ground.  showHitBasket function -> Pages 397 - 398 Purpose: Plays the pop sound effect and the hit basket sprite when the player catches an egg in the basket.  showBrokenEgg function -> Pages 398 – 399 Purpose: Plays the clap sound effect and the broken egg sprite when an egg hits the ground.  resetEgg function -> Pages 399 – 400 Purpose: Resets the egg in the vulture’s beak after the egg has been caught or breaks.  render function -> Pages 400 – 401 Purpose: Displays all the sprites in their updated positions and refreshes the screen.  summaryScreen function -> Pages 401 – 403 Purpose: Displays the game over screen with the points earned, eggs caught and eggs broken. Displays special message and sound effect if every egg was caught.  deleteSprites function -> Page 403 Purpose: deletes all the sprites from memory and removes from screen.

Mrs. Eaton Page 6 ITP 134 Programming Notes – Chapter 8

Vulture Trouble Game Hierarchy Chart

#1 Global #2 DarkGDK Constants Function

#3 loadResources #4 Intro #5 createSprites Function Function Function

Loo pe E#6 moveBasket #7 moveVulture #8 moveEgg #9 checkCollisions #10 render Function Function Function Function Function

#13 showHitBasket #14 showBrokenEgg #15 resetEgg Function Function Function

#11 summaryScreen Function

#12 deleteSprites Function

Mrs. Eaton Page 7

Recommended publications