8
Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Embed Size (px)

Citation preview

Page 1: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Finishing 2D

COSC 315Fall 2014

Bridget M. Blodgett

Page 2: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Text Overlay

• Right now our game functions but is lacking in a challenge to keep the player playing

• One of the simplest to implement is a score component

• Add the following code to Spritepublic int scoreValue {get; protected set;}

• All of the constructors for Sprite and its children should be modified to accept the new int

Page 3: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Score

• Now that there is data to draw from add a variable to Game1 for the current games score– int currentScore = 0;

• Drawing text is similar to drawing sprites• Make a folder to hold your fonts and add a

new item to it– Select the Sprite Font from the template and

name it Score

Page 4: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Fonts

• By default XNA uses a font called Kootenay• If you want to change fonts it is safest to select

one from the Redistributable Font Pack• Once made the spritefont is simply an XML file– Altering the XML will change that font

• You will need to load the font just like the sprite images– SpriteFont scoreFont; – scoreFont = Content.Load<SpriteFont>(@”fonts\score”);

Page 5: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Adding the Font

• Once it is loaded you need to call the font in the Draw methodspriteBatch.Begin();spriteBatch.DrawString(scoreFont, “Score: “ + currentScore, new Vector2(10,10), Color.DarkBlue, 0, Vector2.Zero, 1, SpriteEffects.None, 1);spriteBatch.End();

Page 6: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Random Sprite Types

• Another way to make the game more interesting is to vary the types of enemies available

• You can weight the different types so that some types spawn more frequently than others

• In sprite manager:int likelihoodAutomated = 75;int likelihoodChasing = 20;int likelihoodEvading = 5;

Page 7: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Applying Weights• Replace the code to spawn the skullball with code that picks a random

number and based upon the weights picks which item to spawnint random = ((Game1)Game).rnd.Next(100);if(random < likelihoodAutomated) {

spriteList.Add(new AutomatedSprite(Game.Content.Load<Texture2D>(@”images\skullball”), position, new Point(75,75), 10 , new Point (0,0), new Point (6,8), speed, “skullcollision”, 0));

}else if (random < likelihoodAutomated + likelihoodChasing) {

spriteList.Add(new ChasingSprite(Game.Content.Load<Texture2D>(@”images\skullball”), position, new Point(75,75), 10 , new Point (0,0), new Point (6,8), speed, “skullcollision”, 0));

} else {spriteList.Add(new EvadingSprite(Game.Content.Load<Texture2D>(@”images\skullball”), position, new Point(75,75), 10 , new Point (0,0), new Point (6,8), speed, “skullcollision”, 0));

}

Page 8: Finishing 2D COSC 315 Fall 2014 Bridget M. Blodgett

Activity

• Take the code we just wrote and expand it to offer up to 5 types of non-player sprites

Three Blades AutomatedSprite 50%

Four Blades AutomatedSprite 50%

Skull Ball ChasingSprite 50%

Plus ChasingSprite 50%

Bolt EvadingSprite 100%