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

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Advertisements

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Unemployment: Search and Efficiency Wages.
Chapter 16 Unemployment: Search and Efficiency Wages.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 The Basics of Javascript Programming the World Wide Web Fourth.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Introduction to C Programming
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6: Modular Programming Problem Solving & Program Design in.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Tutorial 3 – Creating a Multiple-Page Report
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
1.
Chapter 7: Arrays In this chapter, you will learn about
Copyright © Cengage Learning. All rights reserved.
Chapter 10: Applications of Arrays and the class vector
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Using C++
ABC Technology Project
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
VOORBLAD.
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Essential Cell Biology
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 15 2 k Factorial Experiments and Fractions.
Intracellular Compartments and Transport
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Essential Cell Biology
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 13 One-Factor Experiments: General.
How Cells Obtain Energy from Food
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 14 Factorial Experiments (Two or More Factors)
CpSc 3220 Designing a Database
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Data Structures Using C++ 2E
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Starting Out with Programming Logic & Design
Presentation transcript:

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

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.

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 arrays size cannot be changed while the program is running –Named constants make array sizes easier to maintain 1-3

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

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

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++

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

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

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

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

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

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

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

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.

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 Values in an array

Copyright © 2010 Pearson Addison-Wesley 10.2 Sorting Arrays 1-16 Figure Values in the array after the first swapFigure Values in the array after the second swap Figure Values in the array after the third swapFigure Values in the array after the fourth swap Figure Values in the array after the fifth swap

Copyright © 2010 Pearson Addison-Wesley 10.2 Sorting Arrays 1-17

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.

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 A two-dimensional array

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 Subscripts for each element of the values array When processing data, each element has two subscripts: –One for its row –Another for its column

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 Output of Program Figure Number stored in the values array in example output of Program 10-12

Copyright © 2010 Pearson Addison-Wesley 10.3 Two-Dimensional Arrays When initializing a two-dimensional array, it helps visually to enclose each rows values in a set of braces 1-22 Initializing a Two-Dimensional Array Figure Initialization of the numbers array

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

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.

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 A tile-based image

Copyright © 2010 Pearson Addison-Wesley 10.4 Tile Maps Images are constructed using only a few tiles –Most are duplicates 1-26 Figure Tiles

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

Copyright © 2010 Pearson Addison-Wesley 10.4 Tile Maps A function is needed to display the tiles on the screen 1-28

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

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping QUESTIONS ?