Using and Creating Sprites Lecture 3 Using and Creating Sprites Catch a Falling Star
Background Image
Simple Sprites Sprites are 2D textures that can be created using any pixel level drawing tool. Power Point can be used to create the sprite as a vector object and then be converted to a bitmap or png file by performing a copy and paste into Paint. The standard transparency color is magenta RGB = (255,0,255). Do not use compressed graphical formats such as .jpg with transparency to avoid the purple halo. This is an artifact of the blending of colors at boundaries. The smoothing reduces jaggies but those colors near magenta will not be interpreted as the transparency color.
Using SpriteFonts in MonoGame and XNA SpriteFonts are existing <?xml version="1.0" encoding="utf-8"?> <!-- This file contains an xml description of a font, and will be read by the XNA Framework Content Pipeline. Follow the comments to customize the appearance of the font in your game, and to change the characters which are available to draw with. --> <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription"> <!-- Modify this string to change the font that will be imported. --> <FontName>SegoeUImono</FontName> <!-- Size is a float value, measured in points. Modify this value to change the size of the font. --> <Size>12</Size> <!-- Spacing is a float value, measured in pixels. Modify this value to change the amount of spacing in between characters. --> <Spacing>0</Spacing> <!-- UseKerning controls the layout of the font. If this value is true, kerning information will be used when placing characters. --> <UseKerning>true</UseKerning> <!-- Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic", and "Bold, Italic", and are case sensitive. --> <Style>Regular</Style> <!-- If you uncomment this line, the default character will be substituted if you draw or measure text that contains characters which were not included in the font. --> <!-- <DefaultCharacter>*</DefaultCharacter> --> <!-- CharacterRegions control what letters are available in the font. Every character from Start to End will be built and made available for drawing. The default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin character set. The characters are ordered according to the Unicode standard. See the documentation for more information. --> <CharacterRegions> <CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion> </CharacterRegions> </Asset> </XnaContent> in MonoGame and XNA SpriteFonts are existing objects that you may edit In Windows OS MonoGame can convert any TrueType font into an .xnb file. (see Lecture 4 for more info)
Class-Level Variables GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D bkg; Rectangle bkgrect; SpriteFont fontgameover; SpriteFont fontgame; Vector2 timepos = new Vector2(10, 10); Vector2 scorepos = new Vector2(650, 10); Vector2 gameoverpos = new Vector2(200, 150); Song cafs; SoundEffect bloop; Texture2D star_sprite; Texture2D face_sprite; Star astar; Face aface; Color col = Color.White; KeyboardState kb; bool gotit; bool pressed; double speed = 10.0; double time2go; int score;
Initialize( ) Method protected override void Initialize() { time2go = 60.0; score = 0; gotit = false; pressed = true; astar = new Star(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, 300.0, 200.0, 3.0, -4.0); aface = new Face(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, 400.0, 240.0, 0.0, 0.0); bkgrect.X = 0; bkgrect.Y = 0; bkgrect.Width = graphics.PreferredBackBufferWidth; bkgrect.Height = graphics.PreferredBackBufferHeight; base.Initialize(); }
Managing Content protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); fontgame = Content.Load<SpriteFont>("game_font"); bkg = Content.Load<Texture2D>("starry_night"); fontgameover = Content.Load<SpriteFont>("a_font"); star_sprite = Content.Load<Texture2D>("stella"); face_sprite = Content.Load<Texture2D>("face"); bloop = Content.Load<SoundEffect>("bloop"); Vector2 gameoverpos = new Vector2(200, 150); cafs = Content.Load<Song>("como_cafs"); MediaPlayer.Volume = 0.05f; MediaPlayer.Play(cafs); }
Update( ) Method protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); kb = Keyboard.GetState(); aface.Vx = 0.0; aface.Vy = 0.0; if (kb.IsKeyDown(Keys.Left)) aface.Vx = -speed; if (kb.IsKeyDown(Keys.Right)) aface.Vx = speed; if (kb.IsKeyDown(Keys.Up)) aface.Vy = -speed; if (kb.IsKeyDown(Keys.Down)) aface.Vy = speed; if (!pressed && kb.IsKeyDown(Keys.Space)) pressed = true; if ((Math.Abs(aface.X + face_sprite.Width / 2.0 - (astar.pos.X + star_sprite.Width / 2.0)) + (Math.Abs(aface.Y + face_sprite.Height / 2.0 - (astar.pos.Y + star_sprite.Height / 2.0)))) < 50.0) gotit = true; } if (pressed && !(kb.IsKeyDown(Keys.Space))) pressed = false; if (gotit) score += 1; bloop.Play(0.02f, 1.0f, 0.0f); gotit = false; if (!gotit) astar.move(); col = Color.White; else col = Color.Orange; aface.move(); time2go -= (double)gameTime.ElapsedGameTime.Milliseconds / (double)1000.0; if (time2go < 0) MediaPlayer.Stop(); base.Update(gameTime);
Draw( ) Method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(bkg, bkgrect, Color.White); if (time2go > 0) spriteBatch.DrawString(fontgame, "Time = " + Convert.ToString((int)time2go), timepos, Color.Yellow); spriteBatch.Draw(star_sprite, astar.pos, col); spriteBatch.Draw(face_sprite, aface.pos, col); } else spriteBatch.DrawString(fontgameover, "Game Over", gameoverpos, Color.Yellow); spriteBatch.DrawString(fontgame, "Score = " + Convert.ToString(score), scorepos, Color.Blue); spriteBatch.End(); base.Draw(gameTime);