Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: None | Size: 5.61 KB | Hits: 37 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using Microsoft.Xna.Framework.Net;
  12. using Microsoft.Xna.Framework.Storage;
  13. using ShipGen;
  14. using System.Diagnostics;
  15.  
  16. namespace SpaceSquadron
  17. {
  18.     /// <summary>
  19.     /// This is the main type for your game
  20.     /// </summary>
  21.     public class SpaceSquadronGame : Microsoft.Xna.Framework.Game
  22.     {
  23.         GraphicsDeviceManager graphics;
  24.         SpriteBatch spriteBatch;
  25.         Texture2D backgroundTexture;
  26.  
  27.         Random random = new Random();
  28.  
  29.         SpriteFont menuFont;
  30.  
  31.         PlayerShip player1;
  32.         List<Bullet> playerBullets = new List<Bullet>();
  33.  
  34.         EnemyGenerator enemyGenerator = new EnemyGenerator();
  35.  
  36.         public SpaceSquadronGame()
  37.         {
  38.             graphics = new GraphicsDeviceManager(this);
  39.             Content.RootDirectory = "Content";
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Allows the game to perform any initialization it needs to before starting to run.
  44.         /// This is where it can query for any required services and load any non-graphic
  45.         /// related content.  Calling base.Initialize will enumerate through any components
  46.         /// and initialize them as well.
  47.         /// </summary>
  48.         protected override void Initialize()
  49.         {
  50.             graphics.PreferredBackBufferWidth = 1280;
  51.             graphics.PreferredBackBufferHeight = 720;
  52.             graphics.ApplyChanges();
  53.  
  54.             graphics.SynchronizeWithVerticalRetrace = false;
  55.             this.IsFixedTimeStep = false;
  56.  
  57.             Components.Add(new FrameRateCounter(this));
  58.             //Components.Add(new GamerServicesComponent(this));
  59.  
  60.             base.Initialize();
  61.         }
  62.  
  63.         /// <summary>
  64.         /// LoadContent will be called once per game and is the place to load
  65.         /// all of your content.
  66.         /// </summary>
  67.         protected override void LoadContent()
  68.         {
  69.             // Create a new SpriteBatch, which can be used to draw textures.
  70.             spriteBatch = new SpriteBatch(GraphicsDevice);
  71.  
  72.             menuFont = Content.Load<SpriteFont>("Fonts\\MenuFont");
  73.  
  74.             backgroundTexture = new StaryBackground(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 2000);
  75.  
  76.             Bullet.Texture = Content.Load<Texture2D>("GFX\\Bullet");
  77.             Bullet.Origin = new Vector2(Bullet.Texture.Width / 2, Bullet.Texture.Height / 2);
  78.  
  79.             player1 = new PlayerShip(
  80.                 PixelMaskGenerator.GetRandomImage(GraphicsDevice, 20, Color.Orange),
  81.                 PlayerIndex.One);
  82.             player1.Position = new Vector2(100, 100);
  83.  
  84.             enemyGenerator.Initialize(GraphicsDevice);
  85.         }
  86.  
  87.         /// <summary>
  88.         /// UnloadContent will be called once per game and is the place to unload
  89.         /// all content.
  90.         /// </summary>
  91.         protected override void UnloadContent()
  92.         {
  93.             // TODO: Unload any non ContentManager content here
  94.         }
  95.  
  96.         /// <summary>
  97.         /// Allows the game to run logic such as updating the world,
  98.         /// checking for collisions, gathering input, and playing audio.
  99.         /// </summary>
  100.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  101.         protected override void Update(GameTime gameTime)
  102.         {
  103.             // Allows the game to exit
  104.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  105.                 this.Exit();
  106.  
  107.             if (player1.IsAlive && player1.HasHealth)
  108.                 player1.Update(gameTime, playerBullets, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  109.  
  110.             enemyGenerator.Update(gameTime, null, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  111.  
  112.             Rectangle screenRect = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  113.  
  114.             List<Bullet> bulletsToRemove = new List<Bullet>();
  115.  
  116.             foreach (Bullet b in playerBullets)
  117.             {
  118.                 b.Update();
  119.  
  120.                 if (!screenRect.Intersects(b.Bounds))
  121.                     bulletsToRemove.Add(b);
  122.  
  123.                 enemyGenerator.CheckCollisionWithBullets(b, bulletsToRemove);
  124.             }
  125.  
  126.             foreach (Bullet b in bulletsToRemove)
  127.                 playerBullets.Remove(b);
  128.  
  129.             base.Update(gameTime);
  130.         }
  131.  
  132.         /// <summary>
  133.         /// This is called when the game should draw itself.
  134.         /// </summary>
  135.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  136.         protected override void Draw(GameTime gameTime)
  137.         {
  138.             GraphicsDevice.Clear(Color.Black);
  139.  
  140.             spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
  141.  
  142.             GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Point;
  143.             GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Point;
  144.             GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Point;
  145.  
  146.             spriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
  147.  
  148.             player1.Draw(spriteBatch);
  149.  
  150.             foreach (Bullet b in playerBullets)
  151.                 b.Draw(spriteBatch);
  152.  
  153.             enemyGenerator.Draw(spriteBatch);
  154.  
  155.             spriteBatch.End();
  156.  
  157.             base.Draw(gameTime);
  158.         }
  159.     }
  160. }