Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10...

30
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics in C++ Tony Gaddis

Transcript of Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10...

Page 1: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Addison Wesley is an imprint of

© 2010 Pearson Addison-Wesley. All rights reserved.

Chapter 10Arrays and Tile Mapping

Starting Out with

Games & Graphics in C++

Tony Gaddis

Page 2: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

1-2

Concept:

In the programs you have designed so far, you have used variables to store data in memory. The simplest way to store a value in memory is to store it in a variable. Variables work well in many situations, but they have limitations.

Page 3: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics• Variables can only hold one value at a time

– Not well suited for storing and processing sets of data

– Must be declared and individually processed

• Arrays are specifically designed for storing and processing sets of data

– Named storage location in memory, like variables

– Can hold a group of values, unlike variables

– All values must be of the same data type

– Cannot store a mixture of data types

• Number inside braces is called the size declarator

– Specifies the number of values that the array can hold

• An array’s size cannot be changed while the program is running

– Named constants make array sizes easier to maintain

1-3

Page 4: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• The storage locations in arrays are known as elements

– Located in consecutive memory locations

• Each element in an array is assigned a unique number called a subscript

– Used to identify specific elements in an array

– First element is assigned the subscript 0

– Second element is assigned the subscript 1

– And so forth

1-4

Array Elements and Subscripts

Figure 10-1 Array subscripts

Page 5: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• You access the individual elements in an array by using their subscripts

1-5

Assigning Values to Array Elements

Figure 10-2 Values assigned to each element

Page 6: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• The C++ language does not perform array bounds checking

– Array subscript values are not checked by the compiler

1-6

No Array Bounds Checking in C++

Page 7: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• Step through an entire array, performing the same operation on each element

1-7

Using a Loop to Step through an Array

Page 8: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• You can optionally initialize an array with values when you declare it

• The series of values separated with commas and enclosed in curly braces is called an initialization list

– Values are stored in the array elements in the order they appear in the list

1-8

Array Initialization

Implicit Array Sizing

Page 9: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• Passing an array as an argument typically requires two arguments

– The array itself

– An integer specifying the number of elements in the array

1-9

Passing an Array as an Argument to a Function

Page 10: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• Compare two arrays with a loop that steps through both arrays, comparing their corresponding elements

1-10

Comparing Two Arrays

Page 11: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• To shuffle an array means to randomly rearrange its contents– For each element in

the array– Randomly select

another element– Swap the contents

of this element with the randomly selected element

1-11

Shuffling an Array

Page 12: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• To successfully swap the contents of two variables, we need a third variable to serve as a temporary storage location

1-12

Swapping Array Elements

Page 13: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.1 Array Basics

• When you process a partially filled array:

– Process only the elements that contain valid items

– Use an integer variable that holds the number of items in the array

– Increment the integer variable each time we add an item to the array

1-13

Partially Filled Arrays

Page 14: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.2 Sorting Arrays

1-14

Concept:

A sorting algorithm rearranges the contents of an array so they appear in a specific order. The selection sort is a specific example of a sorting algorithm.

Page 15: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.2 Sorting Arrays• Many programming tasks require that data in an array be sorted in

some order

• A sorting algorithm is a technique for stepping through an array and rearranging its contents in some order

– Ascending order means from lowest to highest

– Descending order means from highest to lowest

• We will examine the selection sort algorithm

– Smallest value is moved to element 0

– Next smallest value is moved to element 1

– Process continues until all of the elements are in proper order

1-15

Figure 10-13 Values in an array

Page 16: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.2 Sorting Arrays

1-16

Figure 10-14 Values in the array after the first swap Figure 10-15 Values in the array after the second swap

Figure 10-16 Values in the array after the third swap Figure 10-17 Values in the array after the fourth swap

Figure 10-18 Values in the array after the fifth swap

Page 17: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.2 Sorting Arrays

1-17

Page 18: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.3 Two-Dimensional Arrays

1-18

Concept:

A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data.

Page 19: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.3 Two-Dimensional Arrays

• Two-dimensional arrays are useful for working with multiple sets of data

• Think of a two-dimensional array as having rows and columns of elements

1-19

Figure 10-21 A two-dimensional array

Page 20: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.3 Two-Dimensional Arrays

• To declare a two-dimensional array, two size declarators are required:– The first one is for the number for the rows– The second one is for the number of columns

1-20

Declaring a Two-Dimensional Array

Figure 10-23 Subscripts for each element of the values array

• When processing data, each element has two subscripts:

– One for its row

– Another for its column

Page 21: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.3 Two-Dimensional Arrays

• To access the elements in a two-dimensional array, you must use both subscripts

1-21

Accessing the Elements in a Two-Dimensional Array

Figure 10-24 Output of Program 10-12

Figure 10-25

Number stored in

the values array in

example output of

Program 10-12

Page 22: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.3 Two-Dimensional Arrays

• When initializing a two-dimensional array, it helps visually to enclose each row’s values in a set of braces

1-22

Initializing a Two-Dimensional Array

Figure 10-26 Initialization of the numbers

array

Page 23: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.3 Two-Dimensional Arrays

• When a two-dimensional array is passed to a function, the parameter must contain a size declarator for the number of columns

1-23

Passing a Two-Dimensional Array to a Function

Page 24: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.4 Tile Maps

1-24

Concept:

Tiles are small rectangular images that are commonly used to construct the background imagery in a game. A tile map is a two-dimensional array that specifies tiles and their locations on the screen.

Page 25: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.4 Tile Maps• Tiles are small rectangular images that can be put together to form a larger image• Used in early video games, still used by many game programmers today

– Memory efficient– Increase performance

1-25

Figure 10-26 A tile-based image

Page 26: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.4 Tile Maps• Images are constructed using only a few tiles

– Most are duplicates

1-26

Figure 10-30 Tiles

Page 27: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.4 Tile Maps• A tile map is an array that maps the location of each tile on the screen

– Each element holds the image number of the tile– Rows and columns of the tile map correspond to rows and columns on the screen.

1-27

Page 28: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.4 Tile Maps• A function is needed to display the tiles on the screen

1-28

Page 29: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Copyright © 2010 Pearson Addison-Wesley

10.4 Tile Maps

• You can display layered sets of tiles by using two tile maps

– One for background

– Another for obstacles

1-29

Displaying Layered Sets of Tiles

Page 30: Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.

Addison Wesley is an imprint of

© 2010 Pearson Addison-Wesley. All rights reserved.

Chapter 10Arrays and Tile Mapping

QUESTIONS

?