11 Adding Tomato Targets Session 12.2. Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Lesson Four: More of the Same
Chapter 10 Introduction to Arrays
Road Map Introduction to object oriented programming. Classes
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
Games and Simulations O-O Programming in Java The Walker School
GAME:IT Junior Bouncing Ball Objectives: Create Sprites Create Sounds Create Objects Create Room Program simple game.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
11 Adding Sounds Session 7.1. Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Chapter 8: Arrays.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
11 Working with Images Session Session Overview  Find out more about image manipulation and scaling when drawing using XNA  Start to implement.
Programming Video Games
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Game Maker Terminology
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
11 Getting Player Input Using a Gamepad Session 3.1.
Session 13 Pinball Game Construction Kit (Version 3):
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
11 Debugging Programs Session Session Overview  Create and test a method to calculate percentages  Discover how to use Microsoft Visual Studio.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
XNA ● Proprietary Microsoft framework ● C#. Interface.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Sound and more Animations
MOM! Phineas and Ferb are … Aims:
Scratch for Interactivity
Loops BIS1523 – Lecture 10.
Variables A piece of memory set aside to store data
Programming – Touch Sensors
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
File Handling Programming Guides.
Chapter 10 Algorithms.
PC01 Term 3 Project Snake. PC01 Term 3 Project Snake.
Chapter 10 Algorithms.
Chapter 10 Algorithms.
Presentation transcript:

11 Adding Tomato Targets Session 12.2

Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have to add some targets for the player to steer the cheese towards  We are going to add a line of tomato targets across the top of the screen  When the cheese hits a tomato the tomato will disappear  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have to add some targets for the player to steer the cheese towards  We are going to add a line of tomato targets across the top of the screen  When the cheese hits a tomato the tomato will disappear Chapter 12.2: Adding Tomato Targets2

Adding Tomato Targets  We want to have a large number of targets  We also want it to be easy to change the number of targets we have  The way to manage a large number of items is by creating an array  The above code declares the array reference and sets the number of tomatoes at 20  These variables are part of the game world  We want to have a large number of targets  We also want it to be easy to change the number of targets we have  The way to manage a large number of items is by creating an array  The above code declares the array reference and sets the number of tomatoes at 20  These variables are part of the game world Chapter 12.2: Adding Tomato Targets3 GameSpriteStruct[] tomatoes; int numberOfTomatoes = 20;

The Tomato Texture  The targets we are going to add are tomatoes  They will all be drawn from the same texture  The texture was created and added as content in the same way as the bread and cheese  The targets we are going to add are tomatoes  They will all be drawn from the same texture  The texture was created and added as content in the same way as the bread and cheese Chapter 12.2: Adding Tomato Targets4 tomatoTexture = Content.Load ("Images/Tomato");

Arrays of Structures  Putting the bread and cheese information into GameSpriteStruct structures was a good move  Not only did it make them easy to manage, but it also made it possible for us to store lots of sprites very easily, simply by making an array of GameSpriteSruct values  Once you have created a type of your own, you can use it in arrays like any other C# type  Your new type becomes another building block in your programs  Putting the bread and cheese information into GameSpriteStruct structures was a good move  Not only did it make them easy to manage, but it also made it possible for us to store lots of sprites very easily, simply by making an array of GameSpriteSruct values  Once you have created a type of your own, you can use it in arrays like any other C# type  Your new type becomes another building block in your programs Chapter 12.2: Adding Tomato Targets5

Creating the Tomato Array  This statement creates an array of GameSpriteStruct values that will hold the tomatoes in the game  The size of the array is given by the variable numberOfTomatoes  Each element of the array is created “empty” with numeric fields set to 0 and reference fields set to null  This statement creates an array of GameSpriteStruct values that will hold the tomatoes in the game  The size of the array is given by the variable numberOfTomatoes  Each element of the array is created “empty” with numeric fields set to 0 and reference fields set to null Chapter 12.2: Adding Tomato Targets6 tomatoes = new GameSpriteStruct[numberOfTomatoes];

Spacing Out the Tomatoes  This statement creates a local variable that holds the space between each tomato on the row  This is calculated from the drawable width of the display ( maxDisplayX – minDisplayX ) divided by the number of tomatoes that we are drawing  This means the game will handle different numbers of tomatoes or sized displays automatically  This statement creates a local variable that holds the space between each tomato on the row  This is calculated from the drawable width of the display ( maxDisplayX – minDisplayX ) divided by the number of tomatoes that we are drawing  This means the game will handle different numbers of tomatoes or sized displays automatically Chapter 12.2: Adding Tomato Targets7 float tomatoSpacing = (maxDisplayX - minDisplayX) / numberOfTomatoes; float tomatoSpacing = (maxDisplayX - minDisplayX) / numberOfTomatoes;

Setting Up the Tomato Sprites  This small loop sets up all the tomato sprites  For each sprite the code sets the texture and then sets its position and size by calling setupSprite  This small loop sets up all the tomato sprites  For each sprite the code sets the texture and then sets its position and size by calling setupSprite Chapter 12.2: Adding Tomato Targets8 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Sprite Setup for Loop  This for loop works through each tomato in turn  The local variable i is created to count through the elements in the tomatoes array  This for loop works through each tomato in turn  The local variable i is created to count through the elements in the tomatoes array Chapter 12.2: Adding Tomato Targets9 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Sprite Setup Loop  This makes the texture reference in each tomato refer to the same tomato texture  This means that all tomatoes will look the same  This makes the texture reference in each tomato refer to the same tomato texture  This means that all tomatoes will look the same Chapter 12.2: Adding Tomato Targets10 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Calling setupSprite for Each Sprite  This call sets up each sprite  It uses the same method as is used to set up the bread and cheese sprites  This call sets up each sprite  It uses the same method as is used to set up the bread and cheese sprites Chapter 12.2: Adding Tomato Targets11 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Passing a Reference to the Sprite  setupSprite needs a reference to the sprite it is going to work on  Otherwise the element will be passed by value  setupSprite needs a reference to the sprite it is going to work on  Otherwise the element will be passed by value Chapter 12.2: Adding Tomato Targets12 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Setting the Size of Each Tomato  setupSprite needs a reference to the sprite it is going to work on  Otherwise the element will be passed by value  setupSprite needs a reference to the sprite it is going to work on  Otherwise the element will be passed by value Chapter 12.2: Adding Tomato Targets13 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Setting the Speed of Non-Moving Tomatoes  The tomatoes don’t move in this version of the game  However, we still have to provide a speed value  The tomatoes don’t move in this version of the game  However, we still have to provide a speed value Chapter 12.2: Adding Tomato Targets14 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Setting Tomato X Position  The X position of each tomato is calculated using the spacing value we created earlier  The first one will be at the left-hand edge  The X position of each tomato is calculated using the spacing value we created earlier  The first one will be at the left-hand edge Chapter 12.2: Adding Tomato Targets15 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

Setting Tomato Y Position  All the tomatoes are presently along the very top of the screen  We set their Y position to reflect this  All the tomatoes are presently along the very top of the screen  We set their Y position to reflect this Chapter 12.2: Adding Tomato Targets16 for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position } for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteTexture = tomatoTexture; setupSprite( ref tomatoes[i], 0.05f, // 20 tomatoes across the screen 1000, // 1000 ticks to move across the screen minDisplayX + (i * tomatoSpacing), // x position minDisplayY); // y position }

The Tomato Update Behavior  During Update,we need to copy the X and Y positions of each tomato into the Rectangle that will be used to draw it  We need to do this even though the tomatoes don’t move at the moment  During Update,we need to copy the X and Y positions of each tomato into the Rectangle that will be used to draw it  We need to do this even though the tomatoes don’t move at the moment Chapter 12.2: Adding Tomato Targets17 // Update the tomatoes for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteRectangle.X = (int)tomatoes[i].X; tomatoes[i].SpriteRectangle.Y = (int)tomatoes[i].Y; } // Update the tomatoes for (int i = 0; i < numberOfTomatoes; i++) { tomatoes[i].SpriteRectangle.X = (int)tomatoes[i].X; tomatoes[i].SpriteRectangle.Y = (int)tomatoes[i].Y; }

The Tomato Draw Behavior  The Draw behavior is very simple  The for loop works its way through each tomato  The call of spriteBatch.Draw uses the texture and rectangle of each tomato element to draw it on the screen  The Draw behavior is very simple  The for loop works its way through each tomato  The call of spriteBatch.Draw uses the texture and rectangle of each tomato element to draw it on the screen Chapter 12.2: Adding Tomato Targets18 for (int i = 0; i < numberOfTomatoes; i++) { spriteBatch.Draw(tomatoes[i].SpriteTexture, tomatoes[i].SpriteRectangle, Color.White); } for (int i = 0; i < numberOfTomatoes; i++) { spriteBatch.Draw(tomatoes[i].SpriteTexture, tomatoes[i].SpriteRectangle, Color.White); }

1. Tomato Row Chapter 12.2: Adding Tomato Targets19  This shows our tomatoes along the top row  At the moment the cheese doesn’t interact with them  This shows our tomatoes along the top row  At the moment the cheese doesn’t interact with them

Tomato Collisions  When the cheese hits a tomato we want the tomato to disappear  This is one way the player will score points  We can detect collisions between tomatoes and the cheese by using the Intersects method provided by the Rectangle type  However, we have to find a way of making a tomato vanish  When the cheese hits a tomato we want the tomato to disappear  This is one way the player will score points  We can detect collisions between tomatoes and the cheese by using the Intersects method provided by the Rectangle type  However, we have to find a way of making a tomato vanish Chapter 12.2: Adding Tomato Targets20

Objects and State  Many objects in programs have “state”  An order could be “pending,” “being packed,” or “dispatched”  A bank account could be “open,” “closed,” or “suspended”  We need to add some state information to the sprites in our game so that we can track whether they have been killed or not  We will do this for all the GameSpriteStruct values  Many objects in programs have “state”  An order could be “pending,” “being packed,” or “dispatched”  A bank account could be “open,” “closed,” or “suspended”  We need to add some state information to the sprites in our game so that we can track whether they have been killed or not  We will do this for all the GameSpriteStruct values Chapter 12.2: Adding Tomato Targets21

Adding a Visible State to GameSpriteStruct  We are going to add a “state” to our GameSpriteStruct that determines whether or not a sprite is visible  Sprites which are not visible are not drawn on the display  Sprites which are not visible cannot collide with other sprites  When an tomato is “killed,” its state is changed so that it is no longer visible  We are going to add a “state” to our GameSpriteStruct that determines whether or not a sprite is visible  Sprites which are not visible are not drawn on the display  Sprites which are not visible cannot collide with other sprites  When an tomato is “killed,” its state is changed so that it is no longer visible Chapter 12.2: Adding Tomato Targets22

Adding a Visibility State  We have added a Visible field to the sprite  It is of type bool (either true or false )  We have added a Visible field to the sprite  It is of type bool (either true or false ) Chapter 12.2: Adding Tomato Targets23 struct GameSpriteStruct { public Texture2D SpriteTexture; public Rectangle SpriteRectangle; public float X; public float Y; public float XSpeed; public float YSpeed; public float WidthFactor; public float TicksToCrossScreen; public bool Visible; } struct GameSpriteStruct { public Texture2D SpriteTexture; public Rectangle SpriteRectangle; public float X; public float Y; public float XSpeed; public float YSpeed; public float WidthFactor; public float TicksToCrossScreen; public bool Visible; }

Setting the Visibility State  The setupSprite method now has a parameter that can be used to set up the initial visibility of a sprite Chapter 12.2: Adding Tomato Targets24 void setupSprite( ref GameSpriteStruct sprite, float widthFactor, float ticksToCrossScreen, float initialX, float initialY, bool initialVisibility) { // original sprite setup code here sprite.Visible = initialVisibility; } void setupSprite( ref GameSpriteStruct sprite, float widthFactor, float ticksToCrossScreen, float initialX, float initialY, bool initialVisibility) { // original sprite setup code here sprite.Visible = initialVisibility; }

Setting Up Each Sprite  When setupSprite is called the call must now supply visibility information as a parameter  This affects the setup of the bread, the cheese, and all of the tomatoes  When setupSprite is called the call must now supply visibility information as a parameter  This affects the setup of the bread, the cheese, and all of the tomatoes Chapter 12.2: Adding Tomato Targets25 setupSprite( ref cheese, // reference to the sprite to set up 0.05f, // width factor (a 20th) 200.0f, // ticks to cross the screen minDisplayX, // starting X position minDisplayY, // starting Y position true); // initially visible setupSprite( ref cheese, // reference to the sprite to set up 0.05f, // width factor (a 20th) 200.0f, // ticks to cross the screen minDisplayX, // starting X position minDisplayY, // starting Y position true); // initially visible

Using Visibility to Control Drawing  We need to change the tomato drawing code  Now only tomatoes that are visible will be drawn  Note that you can use a logical value directly as a condition  We need to change the tomato drawing code  Now only tomatoes that are visible will be drawn  Note that you can use a logical value directly as a condition Chapter 12.2: Adding Tomato Targets26 for (int i = 0; i < numberOfTomatoes; i++) { if (tomatoes[i].Visible) { spriteBatch.Draw(tomatoes[i].SpriteTexture, tomatoes[i].SpriteRectangle, Color.White); } for (int i = 0; i < numberOfTomatoes; i++) { if (tomatoes[i].Visible) { spriteBatch.Draw(tomatoes[i].SpriteTexture, tomatoes[i].SpriteRectangle, Color.White); }

Detecting Tomato Collisions  This code lets the cheese bounce off tomatoes and destroy them Chapter 12.2: Adding Tomato Targets27 for (int i = 0; i < numberOfTomatoes; i++) { if (tomatoes[i].Visible) { if (cheese.SpriteRectangle.Intersects( tomatoes[i].SpriteRectangle)) { tomatoes[i].Visible = false; cheese.YSpeed = cheese.YSpeed * -1; break; } for (int i = 0; i < numberOfTomatoes; i++) { if (tomatoes[i].Visible) { if (cheese.SpriteRectangle.Intersects( tomatoes[i].SpriteRectangle)) { tomatoes[i].Visible = false; cheese.YSpeed = cheese.YSpeed * -1; break; }

Breaking Out of a Loop  This statement breaks out of a for loop because we only destroy one tomato per call of Update Chapter 12.2: Adding Tomato Targets28 for (int i = 0; i < numberOfTomatoes; i++) { if (tomatoes[i].Visible) { if (cheese.SpriteRectangle.Intersects( tomatoes[i].SpriteRectangle)) { tomatoes[i].Visible = false; cheese.YSpeed = cheese.YSpeed * -1; break; } for (int i = 0; i < numberOfTomatoes; i++) { if (tomatoes[i].Visible) { if (cheese.SpriteRectangle.Intersects( tomatoes[i].SpriteRectangle)) { tomatoes[i].Visible = false; cheese.YSpeed = cheese.YSpeed * -1; break; }

2. Tomato Killer Chapter 12.2: Adding Tomato Targets29  This version of the game allows tomatoes to be destroyed  No actual tomatoes were harmed in the production of this code  This version of the game allows tomatoes to be destroyed  No actual tomatoes were harmed in the production of this code

Summary  You can create arrays of types that you have created  An array variable is a reference that refers to an array instance  Several objects can “share” a single object if they each have a reference to that object  It is often useful to make an object stateful by adding a field that holds its state  The break keyword allows a program to complete a loop early  You can create arrays of types that you have created  An array variable is a reference that refers to an array instance  Several objects can “share” a single object if they each have a reference to that object  It is often useful to make an object stateful by adding a field that holds its state  The break keyword allows a program to complete a loop early Chapter 12.2: Adding Tomato Targets30

True/False Revision Quiz  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object.  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object. Chapter 12.2: Adding Tomato Targets31

True/False Revision Quiz  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object.  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object. Chapter 12.2: Adding Tomato Targets32

True/False Revision Quiz  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object.  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object. Chapter 12.2: Adding Tomato Targets33

True/False Revision Quiz  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object.  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object. Chapter 12.2: Adding Tomato Targets34

True/False Revision Quiz  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object.  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object. Chapter 12.2: Adding Tomato Targets35

True/False Revision Quiz  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object.  An array variable is actually a reference.  You can create arrays of structures that you have created.  The break statement causes your program to stop.  An object in memory can only have one reference referring to it.  The state of an object can be represented by the value of a field inside the object. Chapter 12.2: Adding Tomato Targets36