Skip to content

Instantly share code, notes, and snippets.

@zyxago
Created March 9, 2019 11:58
Show Gist options
  • Save zyxago/855bfd4bd13fec2a2a108aabf1f07d33 to your computer and use it in GitHub Desktop.
Save zyxago/855bfd4bd13fec2a2a108aabf1f07d33 to your computer and use it in GitHub Desktop.
Pixel Explosion of a texture
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PixelExplosion
{
class explode
{
Color[] rawData;
List<Texture2D> pixelList = new List<Texture2D>();
List<Rectangle> rectangles = new List<Rectangle>();
List<Vector2> directionList = new List<Vector2>();
int moveSpeed = 5;
int Size;
Rectangle OriginBox;
public explode(GraphicsDevice graphicsDevice, Texture2D boxText, Rectangle originBox, int scale)
{
OriginBox = originBox;
Size = scale;
rawData = new Color[boxText.Width * boxText.Height];
boxText.GetData<Color>(rawData);
//Ger varje pixel textur en rectangle och position utifrån orginal bilden
for (int i = 0; i < boxText.Width; i++)
{
for (int j = 0; j < boxText.Height; j++)
{
rectangles.Add(new Rectangle(OriginBox.X + (OriginBox.Width / boxText.Width * j), OriginBox.Y + (OriginBox.Height / boxText.Height * i), Size, Size));
}
}
//Lägger till texturer med 1 pixel i från orginal bilden
for (int i = 0; i < rawData.Length; i++)
{
Color[] tempColorArr = new Color[1] { rawData[i] };
pixelList.Insert(i, new Texture2D(graphicsDevice, 1, 1));
pixelList[i].SetData<Color>(tempColorArr);
Vector2 direction = OriginBox.Center.ToVector2() - rectangles[i].Center.ToVector2();
direction.Normalize();
directionList.Insert(i, direction);
}
}
public void Update()
{
for (int i = 0; i < rectangles.Count; i++)
{
rectangles[i] = new Rectangle((int)(rectangles[i].X + -directionList[i].X * moveSpeed), (int)(rectangles[i].Y + -directionList[i].Y * moveSpeed), Size, Size);
}
}
public void Draw(SpriteBatch spriteBatch)
{
int n = 0;
foreach (Texture2D pixel in pixelList)
{
spriteBatch.Draw(pixel, rectangles[n], null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0);
n++;
}
}
}
}
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace PixelExplosion
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState oldState;
Texture2D boxText;
bool remove = false;
int size = 10;
Rectangle box = new Rectangle();
List<explode> explosionList = new List<explode>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
IsMouseVisible = true;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
boxText = Content.Load<Texture2D>("test");
box = new Rectangle(GraphicsDevice.Viewport.Bounds.Center.X - boxText.Width * size / 2, GraphicsDevice.Viewport.Bounds.Center.Y - boxText.Height * size / 2, boxText.Width * size, boxText.Height * size);
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Q))
remove = false;
if (Keyboard.GetState().IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space))
{
explosionList.Add(new explode(GraphicsDevice, boxText, box, size));
remove = true;
}
foreach(explode explosion in explosionList)
{
explosion.Update();
}
oldState = Keyboard.GetState();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
if (!remove)
{
spriteBatch.Draw(boxText, box, null, Color.White, 0f, Vector2.Zero, SpriteEffects.None, 0);
}
foreach (explode explosion in explosionList)
{
explosion.Draw(spriteBatch);
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment