Created
August 1, 2012 09:15
-
-
Save Adamhood22/3225326 to your computer and use it in GitHub Desktop.
scrolling background in xna
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Audio; | |
using Microsoft.Xna.Framework.Content; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
using Microsoft.Xna.Framework.Storage; | |
namespace Tetris_Shooter | |
{ | |
public class ScrollingBackground | |
{ | |
// class ScrollingBackground | |
private Vector2 screenpos, origin, texturesize; | |
private Texture2D mytexture; | |
private int screenheight; | |
public void Load(GraphicsDevice Device, Texture2D BackgroundTexture) | |
{ | |
mytexture = BackgroundTexture; | |
screenheight = Device.Viewport.Height; | |
int screenwidth = Device.Viewport.Width; | |
// Set the origin so that we're drawing from the | |
// center of the top edge. | |
origin = new Vector2(mytexture.Width / 2, 0); | |
// Set the screen position to the center of the screen. | |
screenpos = new Vector2(screenwidth / 2, screenheight / 2); | |
// Offset to draw the second texture, when necessary. | |
texturesize = new Vector2(0, mytexture.Height); | |
} | |
// ScrollingBackground.Update | |
public void Update(float deltaY) | |
{ | |
screenpos.Y += deltaY; | |
screenpos.Y = screenpos.Y % mytexture.Height; | |
} | |
// ScrollingBackground.Draw | |
public void Draw(SpriteBatch Batch) | |
{ | |
// Draw the texture, if it is still onscreen. | |
if (screenpos.Y < screenheight) | |
{ | |
Batch.Draw(mytexture, screenpos, null, | |
Color.White, 0, origin, 1, SpriteEffects.None, 0f); | |
} | |
// Draw the texture a second time, behind the first, | |
// to create the scrolling illusion. | |
Batch.Draw(mytexture, screenpos - texturesize, null, | |
Color.White, 0, origin, 1, SpriteEffects.None, 0f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment