Advertisement
coolbunny1234

Untitled

Mar 16th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. //Game1.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.GamerServices;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Media;
  13.  
  14. namespace Xbox360Game1
  15. {
  16.     /// <summary>
  17.     /// This is the main type for your game
  18.     /// </summary>
  19.     public class Game1 : Microsoft.Xna.Framework.Game
  20.     {
  21.         GraphicsDeviceManager graphics;
  22.         SpriteBatch spriteBatch;
  23.  
  24.         public Ship ship;
  25.  
  26.         public List<Projectile> LiveProjectiles;
  27.  
  28.         public Texture2D Bullet;
  29.  
  30.         public Game1()
  31.         {
  32.             graphics = new GraphicsDeviceManager(this);
  33.             Content.RootDirectory = "Content";
  34.  
  35.             graphics.PreferredBackBufferWidth = 880;
  36.             graphics.PreferredBackBufferHeight = 520;
  37.             graphics.PreferMultiSampling = true;
  38.             graphics.ApplyChanges();
  39.         }
  40.  
  41.         protected override void Initialize()
  42.         {
  43.             // TODO: Add your initialization logic here
  44.  
  45.             base.Initialize();
  46.         }
  47.  
  48.  
  49.         protected override void LoadContent()
  50.         {
  51.             spriteBatch = new SpriteBatch(GraphicsDevice);
  52.             ship = new Ship();
  53.             ship.ShipTexture = Content.Load<Texture2D>("RocketShip");
  54.             ship.Rotation = 0.0f;
  55.             ship.Position = new Vector2(
  56.                 graphics.GraphicsDevice.Viewport.Width / 2,
  57.                 graphics.GraphicsDevice.Viewport.Height / 2);
  58.  
  59.             LiveProjectiles = new List<Projectile>();
  60.             Bullet = Content.Load<Texture2D>("Bullet");
  61.  
  62.         }
  63.  
  64.         protected override void UnloadContent()
  65.         {
  66.             Content.Unload();
  67.             Content.Dispose();
  68.         }
  69.  
  70.         protected override void Update(GameTime gameTime)
  71.         {
  72.             // Allows the game to exit
  73.             //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  74.                 //this.Exit();
  75.             GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  76.             if (gamePadState.DPad.Left == ButtonState.Pressed)
  77.                ship.Position.X -= 2.5f;
  78.             if (gamePadState.DPad.Right == ButtonState.Pressed)
  79.                ship.Position.X += 2.5f;
  80.             if (gamePadState.DPad.Up == ButtonState.Pressed)
  81.                ship.Position.Y -= 2.5f;
  82.             if (gamePadState.DPad.Down == ButtonState.Pressed)
  83.                ship.Position.Y += 2.5f;
  84.  
  85.             if (gamePadState.Buttons.RightShoulder == ButtonState.Pressed)
  86.                 ship.Rotation += 0.1f;
  87.             if (gamePadState.Buttons.LeftShoulder == ButtonState.Pressed)
  88.                 ship.Rotation -= 0.1f;
  89.  
  90.             if (gamePadState.Triggers.Right >= 0.95f)
  91.             {
  92.                 Projectile bullet = new Projectile();
  93.                 bullet.Position = ship.Position;
  94.                 bullet.Velocity =
  95.                     Vector2.Transform(new Vector2(0, -1 * 15),
  96.                 Matrix.CreateRotationZ(ship.Rotation - MathHelper.ToRadians(46)));
  97.                 LiveProjectiles.Add(bullet);
  98.             }
  99.             base.Update(gameTime);
  100.         }
  101.  
  102.         protected override void Draw(GameTime gameTime)
  103.         {
  104.             GraphicsDevice.Clear(Color.CornflowerBlue);
  105.             spriteBatch.Begin();
  106.  
  107.             for (int i = 0; i < LiveProjectiles.Count; i++)
  108.             {
  109.                 spriteBatch.Draw(Bullet, LiveProjectiles[i].Position, null, Color.White, 0f,
  110.                     new Vector2(Bullet.Width / 2, Bullet.Height / 2), 0.2f, SpriteEffects.None, 0);
  111.             }
  112.  
  113.             spriteBatch.Draw(ship.ShipTexture, ship.Position, null, Color.White, ship.Rotation,
  114.                 new Vector2(
  115.                     ship.ShipTexture.Width / 2,
  116.                     ship.ShipTexture.Height / 2), 0.35f, SpriteEffects.None, 0);
  117.             spriteBatch.End();
  118.  
  119.  
  120.             base.Draw(gameTime);
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126. //Ship.cs
  127.  
  128. using System;
  129. using System.Collections.Generic;
  130. using System.Linq;
  131. using System.Text;
  132. using Microsoft.Xna.Framework.Graphics;
  133. using Microsoft.Xna.Framework;
  134. namespace Xbox360Game1
  135. {
  136.     public class Ship
  137.     {
  138.         public Texture2D ShipTexture;
  139.         public Vector2 Position;
  140.         public Single Rotation;
  141.     }
  142. }
  143.  
  144. //Projectile.cs
  145.  
  146. using System;
  147. using System.Collections.Generic;
  148. using System.Linq;
  149. using System.Text;
  150. using Microsoft.Xna.Framework;
  151.  
  152. namespace Xbox360Game1
  153. {
  154.     class Projectile
  155.     {
  156.         public Vector2 Position;
  157.         public Vector2 Velocity;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement