SGDC Boot Camp 4

28
SGDC Boot Camp 4 Because I trust you guys

description

SGDC Boot Camp 4. Because I trust you guys. Here’s How it Works. I’m home for Passover. Unavoidable commitment. You’re going to teach yourself this one. Study at your own pace, then do the project Send questions to [email protected]. Submit Your S**t. Do this immediately - PowerPoint PPT Presentation

Transcript of SGDC Boot Camp 4

SGDC Boot Camp 4

SGDC Boot Camp 4Because I trust you guysHeres How it WorksIm home for Passover.Unavoidable commitment.Youre going to teach yourself this one.Study at your own pace, then do the projectSend questions to [email protected]

Submit Your S**tDo this immediatelyNavigate to the folder containing last weeks assignmentNormally, this is [Your Folder]/Documents/Visual Studio 2008/[Project Name]Delete the folders called obj and binZip up the entire project folderSend it to [email protected] Whats Gonna Be CoveredCollectionsHow to keep track of a whole bunch of stuffDYNAMICALLY!IterationHow to deal with a whole bunch of stuffAUTOMATICALLY!CollectionsRemember how a field, like a float, is a slot?Remember how a class (like your BadGuy) is a whole bunch of slots in a larger slot?Well, a collection is a bunch of slots that are bound together.The collections youll run into:ArrayListDictionaryThis Episode Sponsored by the Letter TWhen you see the letter T as a parameter, it means a Type of slot.This doesnt refer to an actual object, just a type of object.These can be T:float boolVector2GoodGuy Assuming youve made the classThese are not T:bool aBool; An instanceVector2.Zero A static memberfoobar A string literalArraysAn array is the simplest form of a collection.One of the C# base classesArrays are linked, usually consecutive, memory slots of the same type. When you declare an array, all of the slots are allotted at once.The [] parameter holders are used to deal with arraysArraysHow to declare an array:float[] fiveEntryFloatArray = new float[5];This is saying, Hey compiler! Crack open five float slots.Note the following:The array must be instantiated, even though the base type (a float) does notThe array isnt instantiated with a constructorfloat[] fiveEntryFloatArray = new float[5](); NO!!The number of slots in the array cant be changed.Let me repeat that: Once an array has been declared, the number of entries is locked.ArraysTo refer to a specific slot by index, do this:fiveEntryFloatArray[1] = 122f;float myNumber = fiveEntryFloatArray[1]; IndexThe slots start at index [0]Youll get an exception if you try to go out of range. This throws an error:float myNumber = fiveEntryFloatArray[5];Think about that for a second.The error returned is IndexOutOfRangeException. Fairly self-explanatory

ListsA more complex way to store many pieces of dataSlots are added as needed and linked togetherUses more memory than an arrayAnd is a teeny bit slower to retrieveHowever, a list is not a fixed size.Lists require the System.Collections.Generic namespaceThis is usually included in the default using statementsListsHow to declare a list:List listOFloats = new List();This is a strongly typed list it will now only accept floats. Non-strongly-typed lists are tough to use.Note the following:The list must be instantiated with a constructorThe constructor must contain the same type in the as the declaration

ListsTo add or remove an entry, use the List.Add() and List.Remove() methodsYou can retrieve entries like in an arrayfloat myNumber = listOFloats[1];Starts at [0]However, do not do this! You can easily forget how many entries are in the list and get an IndexOutOfRangeExceptionA list is not sorted no guarantee youll get the entry youre looking forSo whats the point? Bear with us.DictionariesA really complex way to store many pieces of dataBehaves exactly like a list, except you explicitly set the indices.Keys and values are strongly typed, and can be any type you wantKeys cant be changed once the entry is addedUses a lot more memory than an array, but isnt much slower to retrieveUseful any time a real-life dictionary would be. Good for storing information in an intuitive way.DictionariesHow to declare a dictionary:Dictionary studentsAndGPAs = new Dictionary();Similar constraints as a ListTo add or remove an entry, use the Dictionary.Add() and .Remove() methodsYou can retrieve entries using a key as the indexfloat myGPA = studentsAndGPAs[Zack];DictionariesCaveats:Multiple entries can have the same value, but all keys must be differentYoull throw an exception if you flub the keyIf the value of the key changes, youll have some shenanigansIterationThe fine art of doing things over and over againThree main iteration keywords:fordo/whileforeachforIterates a specific number of times.How to use:for (int i = 0; i < 50; i++) { block o code } In your code, use whitespaceWTF?int i = 0; Declare and set a variablei < 50; Boolean expression. If this is true, block o code will run. If its not, leaves block o code and stops iterating.i++ After each iteration, this calculation is done.The variable you use is available inside the block o code.

do/whileIterates until falseHow to use:do { block o code } while (bool)If bool is true, block o code is done againThis is a great way to freeze your game.Remember, the Draw() method aint called til Update() is done, and if its stuck in an infinite loop, your game is halted.Dont use unless necessary, and make sure theres a kill-switch if it runs too long.Youre probably better off using for. Forget I taught you this one.foreachProbably the most useful keyword in the entire C# languageIterates over each member of a collectionHow to use:foreach (float f in listOfFloats) { block o code}f is available in your block o codeThis lets you do something with each member of a collection, one by one.Such as call their Update() and Draw() methods.Remember, a List can have a flexible number of entries.Starting to put the pieces together?

foreachCaveats:Throws an exception if the number of entries in the collection changes during iteration.Dont add or remove during iteration!To foreach through a Dictionary, useforeach (KeyValuePair kvp in yourDictionary)You cant modify the properties of the KeyValuePair. Sorry.Putting the Pieces TogetherA List lets you hold a dynamic number of memory slotsThe foreach keyword lets you do something with every entry, regardless of how many there areCombine the two, and you no longer need to declare and call methods on entities by hand!Think of the possibilities!Sorry for wasting your time with the last assignment.It was a learning experienceJust a Hot Second!So if we cant add or remove from a collection during foreach, how do we remove dead entries?Simple:Create a deadEntries List During iteration, Add() a reference to the dead entryAfter iteration, foreach entry in deadEntries, Remove() it from the live entries listFinally, Clear() the deadEntries List.Bonus! EnumerationsAn enum is a linked set of named constants.Basically, its a const int field where the user is limited to which ints they can pick, and each int is named.Youve used these before:SpriteEffectsPlayersThink of an enum like a color swatch book

EnumerationsHow to create:public enum FishTypes { Salmon, Mackerel, Cod, Gefilte, Tuna, Flounder }Create this OUTSIDE of a classHow to use:private FishTypes whatsMyFish = FishTypes.Gefilte;Useful to limit programmers to predictable choicesYour HomeworkCreate Breakout!Use your own images for the paddle, ball, and blocksRe-use your collision detection code from last timeHint: The position of two objects that collide can be used to determine where the collision occurredforeach through an array to declare, initialize, update, draw each block in one shotUse an enum to store which color the block isAdd some way to tell the player how many lives he has leftIt must look significantly better than this

Extra CreditAdd a few powerupsFireball: Disable collision detectionLasers: Re-use your Projectile classBig Paddle: Change your Scale (and re-calculate the Bounds!)Make some special blocksInvinci-blocksBlocks that take a few shots to destroyMake sure the user can tell the difference!Make a ridiculous way to destroy that last pesky blockBest project wins a prize

The Attached CodeDemonstrates everything covered todayTo change what happens, edit the actionMode field in the Game1 classYou can see the ActionModes enum right above the Game1 class.27The Attached CodeIn ActionModes.ArrayDemo, a Sprite[16] is used to draw the Andy Warhol thingy in many fewer lines.In ActionModes.ListDemo, a new BadGuy is added to the List whenever the A button is pressed.In ActionModes.DictDemo, a Dictionary is populated with each button on the GamePad and is used to store their values. To see this work, follow the directions in the code to enter Debug mode and view the Dictionarys contents.In ActionModes.ForDemo, a huge number of Sprites are drawn on the screen using for.In ActionModes.DoWhileDemo, an infinite Do/While loop freezes the game.