Chapter 9 Text, Collisions, and the Vulture Trouble Game

ITP 134 C++ Study Guide

Chapter 9 Text, Collisions, and the Vulture Trouble Game

Instructions: Use this Study Guide to help you understand the important points of this chapter. See the book for lots of great programming examples. Important concepts, keywords, statements and functions are shown in emphasized font.
Code syntax and examples are shown in code font.

Games & Graphics in C++
2nd edition by Tony Gaddis

Note: All the AGK statements have a prefix of agk:: so this is assumed for all of the following AGK functions when you use them in a program.

ITP 134 – Mrs. Eaton Chapter 9 Study Guide – 2nd Edition Page 1

Chapter 9 Text, Collisions, and the Vulture Trouble Game

9.1 Text Objects

CONCEPT: You can create text objects to display messages and information. Text objects have properties such as position, size and color. (page 367)

Text Functions

All the text functions use a text index. I am using index to denote this. This is index = text index

Function / Page / Purpose / Parameters or Arguments / Example Program
CreateText(index, “string”); / 367 / Create a text object. / string = string to display / 9-1 Text Sizes (pages 370-372)
DeleteText(index); / 368 / Remove a text object from memory. / index
GetTextExists
(index); / 368 / Determine if a text object currently exists. / Returns integer 1 if text object exists or 0 otherwise.
SetTextSize(index, size); / 368 / Set the size of text in a text object. / Default is 4 units for 640x480 resolution.
size = size in units (this is not pixels) / 9-1 Text Sizes (pages 370-372)
GetTextSize(index); / 369 / Get the size of text for a text object. / Returns float value of the current text size for the text object.
SetTextPosition
(index, x, y); / 369 / Change the position of a text object. / Default position is 0,0.
float x, y are coordinate positions.
Default position: Top, left corner .
Default alignment: Left / 9-1 Text Sizes (pages 370-372)
9-2 Text Alignment (pages 374-375)
SetTextX(index, x);
SetTextY(index, y); / 369
370 / Change the x or y coordinate position of a text object / Float x is the X coordinate. Float y is the Y coordinate.
GetTextX(index, x);
GetTextY(index, y); / 369
370 / Get the x or y coordinate position of a text object. / Returns float x or y coordinate.
SetTextAlignment
(index, mode); / 373 / Change the alignment of a text object. / Int mode:
0 = left alignment
1 = center
2 = right alignment / 9-2 Text Alignment (pages 374-375)
9-3 Text Spacing (pages 377 – 378)
SetTextSpacing
(index, spacing); / 377 / Change the spacing between characters in a text object. / float spacing:
positive value increases spacing;
negative value decreases spacing;
/ 9-3 Text Spacing (pages 377 – 378)
SetTextString(index, “string”) / 379 / Change the string value of an existing text object / string = string to display / 9-4 Text Object Collision (pages 381-382)
GetTextTotalWidth
(index) / 380 / Get the total width of a text object / Returns float value for width of text object.
GetTextTotalHeight
(index) / 380 / Get the total height of a text object / Returns float value for height of text object.
SetTextVisible
(index, visible) / 380 / Hide or show a text object / Visible = 0 to hide,
1 to display text
SetTextColor
(index, red, green, blue, alpha) / 380 / Change the color and transparency of a text object / Red, green, blue & alpha are integers range from 0 to 255.
GetTextHitTest
(index, x, y) / 381 / Detect if a single point is within the bounds of a text object’s bounding box. / Float x,y coordinate positions. Returns integer 0 if coordinate not inside text bounding box and 1 if it is. / 9-4 Text Object Collision (pages 381-382)

9.2 Sprite Collision Detection

CONCEPT: A collision between sprites occurs when on sprite’s bounding box comes in contact with another sprite’s bounding box. Collisions between sprites can be detected. (page 384)

Function / Page / Purpose / Parameters or Arguments / Example Program
GetSpriteCollision
(index1, index2) / 385 / Determines whether 2 sprites have collided / Index1 index of first sprite. Index2 index of second sprite.
Returns 1 if sprite rectangles overlap, 0 otherwise. / 9-5 Sprite Collision (pages 385-387)

The PizzaBot Game

·  The main character is a pizza-eating robot. The robot starts in the lower right corner of the screen. A slice of pizza will occur in a random location. The object of the game is to use the arrow keys to move the robot to the pizza. When the robot collides with the pizza, the Yum! Image displays in place of the pizza.

·  Game Functions in addition to Begin, Main and End

1.  updateRobot() moves the robot when the user presses an arrow key

2.  detectcollision() determines if the robot has collided with the pizza

3.  showYum() displays the Yum! Sprite in place of the pizza sprite after a collision

4.  generatePizza() generates a new slice of pizza at a random location

9.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 394)

·  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 9-10 Speed of a falling brick on page 395 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 9-7 Free Fall for a simulation of a falling ball. (pg 397-400)

·  See Program 9-8 Dual Motion for a simulation of a ball that is thrown from the top right corner and moves down horizontally and down vertically as well. (pg 400 – 402) Figure 9.13 shows 4 frames of the program screens. (pg 402)

9.4 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 vulture has 40 eggs. 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. The game keeps track of the number of eggs caught as well as the broken eggs. When the last egg has been dropped, a summary screen appears. (pg 403)

Images for the game (see color insert Figure C-9)

·  farm.bmp = farm background for the game screen

·  vulture.bmp = sprite sheet (texture atlas) for flying vulture animation

·  egg.png = egg image used for dropped eggs and status bar across top of screen

·  basket.png = basket to catch eggs

·  hitBasket.png = displays briefly when you catch an egg in the basket

·  brokenEgg.png = displays briefly when an egg hits the ground

Vulture Trouble Game (continued)

Sounds for the game

·  vultureTrouble.mp3 = intro screen music

·  vultureLevel.mp3 = 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 C-9 and C-10)

Vulture Trouble Game Functions

·  Global constant declarations -> Pages 406-408
Purpose: Declarations for all the images, sprites, sounds, music, time and other constants

·  app::Begin function -> Pages 409-414
Purpose: Set up the game. Set up virtual resolution and window title. Load the images, create the sprites, load the sounds and music. Set up the vulture animation. Set up starting positions of the vulture, egg, and basket sprites. Use a for loop to create the mini-egg sprites. Create the text objects for the Title screen, Intro Screen, Game Screen and Game Results screen. Then hide the text objects until they need to be displayed.

·  App:: Loop function -> Pages 414-416
Purpose: Primary game loop function that is executed every frame of the game, and calls all the other functions that control the game. Changes the game states from game_started to intro_started to game_in_play, to game_over to summary_started.

·  displayTitleScreen function -> Page 416
Purpose: displays the title screen and plays the intro music

·  hideTitleScreen function -> Page 417
Purpose: hides the title screen and changes the game state to intro_started

·  displayIntroScreen function -> Page 416
Purpose: displays the intro screen and plays the game play music

·  hideIntroScreen function -> Page 418
Purpose: hides the intro screen, shows the game sprites and changes the game state to game_in_play

·  playGame function -> Page 418-419
Purpose: the heart of the game that calls the other functions to move the basket, move the Vulture, move the egg and check for collisions

·  hideGame function -> Page 419
Purpose: Called after no more eggs are left. Hides the game sprites, stops game play music, changes state to game_over

·  moveBasket function -> Pages 419 - 420
Purpose: Detects user pressing the left and right arrow keys and moves the basket right or left.

Vulture Trouble Game Functions (continued)

·  moveVulure function -> Pages 421-422
Purpose: Moves the vulture back and forth across the top of the screen with the animated sprite. When hits the edge of the screen rotates the vulture to fly in the other direction.

·  moveEgg function -> Pages 422-423
Purpose: Moves the egg sprite across and down the screen as vulture drops it.

·  checkCollisions function -> Pages 423-424
Purpose: Checks to see if the egg hits the basket or hits the ground.

·  showHitBasket function -> Pages 424-425
Purpose: Displays the hit basket sprite and plays the pop sound effect when the player catches an egg in the basket.

·  showBrokenEgg function -> Pages 425-426
Purpose: Displays the broken egg sprite and plays the clap sound effect when an egg hits the ground.

·  resetEgg function -> Page 426
Purpose: Resets the egg in the vulture’s beak after the egg has been caught or breaks.

·  displayGameOverScreen function -> Page 427
Purpose: Displays the game over screen and plays the game complete sound.

·  hideGameOverScreen function -> Page 427
Purpose: Hides the game over screen and stops the game complete sound. Changes the game state to summary_started

·  displaySummaryScreen function -> Pages 428-430
Purpose: Displays the summary screen with the points earned, eggs caught and eggs broken.
Displays special message and sound effect if every egg was caught.

·  hideSummaryScreen function -> Pages 430-431
Purpose: Hides the text objects for the summary screen and the sprites. Resets the global variables so the game can be played again. Resets the game state to game_started.

ITP 134 – Mrs. Eaton Chapter 9 Study Guide – 2nd Edition Page 1