Chapter 10 Using Files and Arrays with the AGK
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 10 Using Files and Arrays with the AGK ITP 134 C++ Study Guide
Chapter 10 Using Files and Arrays with the AGK 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.
10.1 File Input and Output CONCEPT: When a program needs to save data for later use, it writes the data in a file. The data can be read from the file later. Write File Functions All the file functions use a file number index. I am using index to denote this. That is index = file number. Function Page Purpose Parameters or Example Program Arguments OpenToWrite(index, 440 Open an output file for writing. Filename.ext is the 10-2 WriteRandom “filename.ext”); name of the file Colors (pg 452-3) 10-2 WriteRandom Colors (pg 452-3) OpenToWrite(index, 440 Open an existing output file for AppendMode is integer “filename.ext”, writing, either overwrite the file 0 = overwrite AppendMode); or append 1= append to end WriteInteger(index, 441 Write an integer value to an Value is an integer 10-1 BugZapperV2 value); output file. value 10-2 WriteRandom WriteFloat(index, 441 Write a float value to an output Value is a float value value); file. WriteString(index, 441 Write a string literal or a string String is a string object string); object to an output file. or a string literal Terminated by a null value. WriteLine(index, 442 Write a string literal or a string String is a string object string); object to an output file. or a string literal Terminated with the newline character. CloseFile(index); 442 Close a file to disconnect it and You should close the 10-1 BugZapperV2 free the file number index. file when you finish 10-2 WriteRandom with it. Colors
ITP 134 – Mrs. Eaton Chapter 10 Study Guide – 2nd Edition Page 1 Chapter 10 Using Files and Arrays with the AGK Read File Functions All the file functions use a file number index. I am using index to denote this. That is index = file number. Function Page Purpose Parameters or Example Program Arguments OpenToRead(index, 442 Open an input file for reading. Filename.ext is the 10-1 BugZapperV2 “filename.ext”); name of the file (pages 445-451) Saving the High Score 10-3 ReadRandom Colors (pg 452-455) ReadInteger(index); 443 Read an integer value from an Returns an integer 10-1 BugZapperV2 input file. value 10-3 ReadRandom Colors ReadFloat(index); 443 Read a float value from an input Returns a float value file. ReadString(index); 443 Read a null terminated string Returns a string object from an input file. ReadLine(index, 444 Read a newline terminated Returns a string object string); string from an input file. CloseFile(index); 444 Close a file to disconnect it and You should close the 10-1 BugZapperV2 (same function as free the file number index. file when you finish 10-3 ReadRandom on previous page) with it. Colors FileIsOpen(index); 444 Determine if a file is already Returns 1 if file is open. Snippet page 444. open before you perform read Returns 0 if file is not or write operations. open.
10.2 Using Arrays in an AGK Program CONCEPT: Arrays can be used for a variety of tasks in a game, such as simulating a card deck, keeping a list of sprites, or creating a tile-based screen. (page 459) Sorting Algorithm is a technique for stepping through an array and rearranging its contents in order. (page 471) Selection Sort Algorithm works like this: (page 471) 1. Find the index of the smallest unsorted item in the array. 2. Swap that item with the first item. Now the first item is sorted. 3. Start back at the 2nd item and go through the array and find the smallest item in the array. 4. Swap that item with the 2nd item. 5. Continue the process until all items are sorted.
Note: Remember from CSC 200 that we learned about 4 different sorting algorithms: Bubble sort, selection sort and Quicksort. Now you are getting to actually program a selection sort algorithm.
10.3 Tile Maps CONCEPT: Tiles are small rectangular images that are commonly used to construct the background imagery in a game. A tile map is a 2D array that specifies tiles and their locations on the screen. (page 478)
ITP 134 – Mrs. Eaton Chapter 10 Study Guide – 2nd Edition Page 2 Chapter 10 Using Files and Arrays with the AGK
Chapter 10 Program Examples In the Spotlight: Saving a Game’s High Score Program 10-1 BugZapperVersion2 (pg 446-451)
In the Spotlight: Detecting the End of a File Program 10-2 WriteRandomColors (pg 452-453)
Program 10-3 ReadRandomColors (pg 453-455)
In the Spotlight: Writing a Log File Program 10-4 DirectionKeysLog (pg 456-458)
In the Spotlight: Using an Array As a Deck of Cards Program 10-5 DealCards (pg 460-465)
In the Spotlight: Shuffling an Array Program 10-6 ShuffleCards (pg 466-479)
Program 10-7 SortAndShuffle (pg 473-478)
Program 10-8 TileDemo (pg 481-484)
Program 10-9 WalkingAlec (pg 484-492)
Program 10-10 Obstacles (pg 493-500)
ITP 134 – Mrs. Eaton Chapter 10 Study Guide – 2nd Edition Page 3