Download presentation
Presentation is loading. Please wait.
1
Animated Sprites
2
Class-Level Variables
GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D panobkg; Rectangle bkg; Vector2 bkgpos; Vector2 bkgorigin; Vector2 homerpos; KeyboardState keystate; Rectangle homer; Texture2D homersprite; Random rnd = new Random(); float homerflip; Vector2 homercenter = new Vector2(28, 43); int homercount; int jumpcount; int mshomer = 60; int msdel; double y; int ybase; double vy; double ay = -0.1; SoundEffect woohoo; SoundEffect scream; SoundEffect doh; homer animation rate (60 milliseconds per frame)
3
SpriteSheet for Animated Sprites
742 x 174 174/2 = 87 742/13 = 57
4
size of blit in spritesheet
protected override void Initialize() { base.Initialize(); bkgpos.X = 0; bkgpos.Y = 0; graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 348; bkg = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); bkgorigin.X = 0; bkgorigin.Y = 0; graphics.ApplyChanges(); homer.X = 0; homer.Y = 0; homer.Width = 57; homer.Height = 87; homercount = 6; jumpcount = 0; homerflip = 0; msdel = 0; homerpos.X = graphics.PreferredBackBufferWidth / 2; homerpos.Y = graphics.PreferredBackBufferHeight - 63; ybase = (int)homerpos.Y; vy = 0.0; } size of blit in spritesheet
5
Homer Running msdel += gameTime.ElapsedGameTime.Milliseconds;
keystate = Keyboard.GetState(); if (keystate.IsKeyDown(Keys.Right)) { homer.Y = 0; if (homerpos.X < bkg.Width / ) homerpos.X += 4; else bkg.X += 4; if (bkg.X >= panobkg.Width - bkg.Width) if (msdel > mshomer) homercount -= 1; msdel = 0; if (homercount < 0) homercount = 5; } if (keystate.IsKeyDown(Keys.Left)) { homer.Y = 87; if (homerpos.X > bkg.Width / ) homerpos.X -= 4; else bkg.X -= 4; if (bkg.X <= 0) if (msdel > mshomer) homercount -= 1; msdel = 0; if (homercount < 0) homercount = 5; }
6
Scrolling Background location of homer in world coordinates
if (keystate.IsKeyUp(Keys.Right) && keystate.IsKeyUp(Keys.Left) && jumpcount == 0) homercount = 6; if (homerflip > 0) homerflip += (float)0.6; if (homerflip > 25) homerflip = 0; homer.X = (homercount + jumpcount) * 57; if (homerpos.X > bkg.Width - 35) homerpos.X = bkg.Width - 35; if (homerpos.X < 35) homerpos.X = 35; if (bkg.X < 0) bkg.X = 0; if (bkg.X > panobkg.Width - bkg.Width) bkg.X = panobkg.Width - bkg.Width; location of homer in world coordinates scrolling in background
8
Homer Jumping if (keystate.IsKeyDown(Keys.Up) && jumpcount == 0) {
vy = 1.3; jumpcount = 7; homercount = 0; woohoo.Play(); } if (jumpcount > 0) vy += ay; y += vy + ay / 2.0; homerpos.Y -= (int)y; if (homerpos.Y > ybase) homerpos.Y = ybase; jumpcount = 0; vy = 0.0; y = 0.0;
9
location sprite is being drawn size of sprite blit
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); spriteBatch.Draw(panobkg, bkgorigin, bkg, Color.White, 0, bkgorigin, 1, 0, 1); spriteBatch.Draw(homersprite, homerpos, homer, Color.White, homerflip, homercenter, 2, 0, 0); spriteBatch.End(); base.Draw(gameTime); } source of sprite image location sprite is being drawn size of sprite blit sprite rotation angle center of rotation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.