SHARE
TWEET

TinyGame(91)

a guest May 24th, 2012 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. namespace TinyGame
  6. {
  7.     public class Game1 : Microsoft.Xna.Framework.Game
  8.     {
  9.         GraphicsDeviceManager graphics;
  10.         private class Sprite
  11.         {
  12.             public Vector2 Pos, Direction;
  13.             public double frame, frametime, attacktime, index, speed = 128, changeDir, kill, frameSpeed = .33, attackSpeed = .75, shoot = 0;
  14.  
  15.             public void Draw(SpriteBatch batch, Texture2D sheet)
  16.             {
  17.                 batch.Draw(sheet, Pos, new Rectangle((int)(index) * 32, (int)(frame) * 32, 32, 32), Color.White);
  18.             }
  19.             public void Update(GameTime gt)
  20.             {
  21.                 frametime += gt.ElapsedGameTime.TotalSeconds;
  22.                 if (frametime > frameSpeed) { frame++; frametime = 0; if (frame > 1) frame = 0; }
  23.                 attacktime -= gt.ElapsedGameTime.TotalSeconds;
  24.                 Pos += Direction * (float)speed * (float)gt.ElapsedGameTime.TotalSeconds;
  25.                 bool needChange = false;
  26.                 if(Pos.X < 0) {Pos.X = 0; needChange = true;}
  27.                 if(Pos.X + 32 > 480) {Pos.X = 480 - 32; needChange = true;}
  28.                 if (changeDir != 0 && needChange) Direction.X *= -1;
  29.                 if (changeDir != 0 && attacktime <= 0) shoot = 1;
  30.                 if (Pos.Y + 32 < 0) kill = 1;
  31.             }
  32.         }
  33.         KeyboardState ks, oldKs;
  34.         SpriteBatch spriteBatch;
  35.         SpriteFont font;
  36.         Texture2D sprites;
  37.         List<Sprite> Sprites = new List<Sprite>();
  38.         List<Sprite> Bullets = new List<Sprite>();
  39.         int score = 0;
  40.         System.Random r = new System.Random();
  41.         public Game1()
  42.         {
  43.             graphics = new GraphicsDeviceManager(this);
  44.             graphics.PreferredBackBufferHeight = 600;
  45.             graphics.PreferredBackBufferWidth = 480;
  46.         }
  47.         protected override void LoadContent()
  48.         {
  49.             spriteBatch = new SpriteBatch(GraphicsDevice);
  50.             sprites = Content.Load<Texture2D>("Content/sprites");
  51.             Sprites.Add(new Sprite() { index = 0, Pos = new Vector2(480/2 - 16, 600-32) });
  52.             for (int i = 1; i < r.Next(8, 25); i++) Sprites.Add(new Sprite() { attackSpeed = System.Math.Max(r.Next(3, 20) * r.NextDouble(), 2), attacktime = System.Math.Max(r.Next(1, 10) * r.NextDouble(), 1), speed = r.Next(40, 128), index = r.Next(1, 3), changeDir = 1, Pos = new Vector2(r.Next(0, 480 - 32), r.Next(0, 16) * 32), Direction = new Vector2(r.Next(0, 2) == 1 ? 1 : -1, 0) });
  53.             font = Content.Load<SpriteFont>("Content/Font");
  54.         }
  55.         protected override void Update(GameTime gameTime)
  56.         {
  57.             if (Sprites[0].kill > 0) { score = 0; Sprites.Clear(); Bullets.Clear(); LoadContent(); return; }
  58.             if (Sprites.Count == 1) for (int i = 1; i < r.Next(8, 25); i++) Sprites.Add(new Sprite() { attackSpeed = System.Math.Max(r.Next(3, 20) * r.NextDouble(), 2), attacktime = System.Math.Max(r.Next(1, 10) * r.NextDouble(), 1), speed = r.Next(40, 128), index = r.Next(1, 3), changeDir = 1, Pos = new Vector2(r.Next(0, 480 - 32), r.Next(0, 16) * 32), Direction = new Vector2(r.Next(0, 2) == 1 ? 1 : -1, 0) });
  59.             oldKs = ks;
  60.             ks = Keyboard.GetState();
  61.             bool left = ks.IsKeyDown(Keys.Left), right = ks.IsKeyDown(Keys.Right), shoot = ks.IsKeyUp(Keys.Space) && oldKs.IsKeyDown(Keys.Space) && Sprites[0].attacktime <= 0;
  62.             Sprites[0].Direction.X = left && right ? 0 : left ? -1 : right ? 1 : 0;
  63.             for (int i = Sprites.Count - 1; i >= 0; i--) {
  64.                 Sprites[i].Update(gameTime);
  65.                 if (Sprites[i].shoot > 0) { Bullets.Add(new Sprite() { index = 4, Direction = new Vector2(0, 1), Pos = new Vector2(Sprites[i].Pos.X, Sprites[i].Pos.Y - 32), speed = 164, frameSpeed = .20 }); Sprites[i].attacktime = Sprites[i].attackSpeed; Sprites[i].shoot = 0; }
  66.                 if (Sprites[i].kill > 0) Sprites.RemoveAt(i); }
  67.             if (shoot) { Sprites[0].attacktime = Sprites[0].attackSpeed; Bullets.Add(new Sprite() { index = 3, Direction = new Vector2(0, -1), Pos = new Vector2(Sprites[0].Pos.X, Sprites[0].Pos.Y - 32), speed = 164, frameSpeed = .20 }); }
  68.             for (int i = Bullets.Count - 1; i >= 0; i--) {
  69.                 for (int j = Sprites.Count - 1; j >= 0; j--) {
  70.                     if (Bullets[i].Direction.Y > 0 && j != 0) continue;
  71.                     else if (Bullets[i].Direction.Y < 0 && j == 0) continue;
  72.                     else if (new Rectangle((int)Bullets[i].Pos.X + 4, (int)Bullets[i].Pos.Y + 4, 24, 24).Intersects(new Rectangle((int)Sprites[j].Pos.X, (int)Sprites[j].Pos.Y, 32, 32)))
  73.                     { Bullets[i].kill = 1; Sprites[j].kill = 1; score += Bullets[i].Direction.Y > 0 ? 0 : 5; continue; }
  74.                 }
  75.                 Bullets[i].Update(gameTime);
  76.                 if (Bullets[i].kill > 0)  Bullets.RemoveAt(i);
  77.             }
  78.             base.Update(gameTime);
  79.         }
  80.         protected override void Draw(GameTime gameTime)
  81.         {
  82.             GraphicsDevice.Clear(Color.DarkGreen);
  83.             spriteBatch.Begin();
  84.             for (int i = 0; i < Sprites.Count; i++) { Sprites[i].Draw(spriteBatch, sprites); }
  85.             for (int i = 0; i < Bullets.Count; i++) { Bullets[i].Draw(spriteBatch, sprites); }
  86.             spriteBatch.DrawString(font, string.Format("Score: {0}", score), new Vector2(10, 580), Color.AliceBlue);
  87.             spriteBatch.End();
  88.             base.Draw(gameTime);
  89.         }
  90.     }
  91. }
RAW Paste Data
Top