| using System; |
| using System.Collections.Generic; |
| using Microsoft.Xna.Framework; |
| using Microsoft.Xna.Framework.Audio; |
| using Microsoft.Xna.Framework.Content; |
| using Microsoft.Xna.Framework.GamerServices; |
| using Microsoft.Xna.Framework.Graphics; |
| using Microsoft.Xna.Framework.Input; |
| using Microsoft.Xna.Framework.Net; |
| using Microsoft.Xna.Framework.Storage; |
| |
| namespace FighterPilot |
| { |
| /// <summary> |
| /// This is the main type for your game |
| /// </summary> |
| public class Game1 : Microsoft.Xna.Framework.Game |
| { |
| GraphicsDeviceManager graphics; |
| |
| SpriteBatch spriteBatch; |
| |
| Texture2D backgroundtexture; |
| Rectangle viewportRect; |
| GameObject Player; |
| const int maxbullet = 10; |
| GameObject[ Bullets; |
| KeyboardState previousKeyboardState = Keyboard.GetState(); |
| const float playervelocity = 5.0f; |
| const float bulletvelocity = 15.0f; |
| const float bulletrotation = 4.7f; |
| |
| 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 |
| |
| 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. |
| spriteBatch = new SpriteBatch(GraphicsDevice); |
| |
| Player = new GameObject(Content.Load<Texture2D>("Sprites\\planef150005hf8r")); |
| Player.position = new Vector2(200, graphics.GraphicsDevice.Viewport.Height -300); |
| Bullets = new GameObject[maxbullet]; |
| for (int i = 0; i < maxbullet; i++) |
| { |
| Bullets[i] = new GameObject(Content.Load<Texture2D>("Sprites\\cannonball")); |
| } |
| backgroundtexture = Content.Load<Texture2D>("Sprites\\background1"); |
| viewportRect = new Rectangle(0, 0, |
| graphics.GraphicsDevice.Viewport.Width, |
| graphics.GraphicsDevice.Viewport.Height); |
| |
| |
| base.LoadContent(); |
| } |
| |
| /// <summary> |
| /// UnloadContent will be called once per game and is the place to unload |
| /// all 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) |
| { |
| // Allows the game to exit |
| KeyboardState keyboardState = Keyboard.GetState(); |
| if(keyboardState.IsKeyDown(Keys.Escape)) |
| { |
| this.Exit(); |
| } |
| if (keyboardState.IsKeyDown(Keys.Up)) |
| { |
| Player.position.Y -= playervelocity; |
| } |
| if (keyboardState.IsKeyDown(Keys.Down)) |
| { |
| Player.position.Y += playervelocity; |
| } |
| if (keyboardState.IsKeyDown(Keys.Left)) |
| { |
| Player.position.X -= playervelocity; |
| } |
| if (keyboardState.IsKeyDown(Keys.Right)) |
| { |
| Player.position.X += playervelocity; |
| } |
| if (keyboardState.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space)) |
| { |
| firebullet(); |
| } |
| |
| updatebullets(); |
| |
| previousKeyboardState = keyboardState; |
| |
| base.Update(gameTime); |
| } |
| |
| public void firebullet() |
| { |
| foreach (GameObject bullet in Bullets) |
| { |
| if (bullet.alive) |
| { |
| bullet.alive = true; |
| bullet.position = Player.position; |
| bullet.rotation = 4.7f; |
| bullet.velocity = new Vector2(bulletvelocity); |
| return; |
| } |
| } |
| } |
| |
| public void updatebullets() |
| { |
| foreach (GameObject bullet in Bullets) |
| { |
| if (bullet.alive) |
| { |
| bullet.position += bullet.velocity; |
| if (!viewportRect.Contains(new Point( |
| (int)bullet.position.X, |
| (int)bullet.position.Y))) |
| { |
| bullet.alive = false; |
| continue; |
| } |
| } |
| } |
| } |
| |
| /// <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) |
| { |
| graphics.GraphicsDevice.Clear(Color.CornflowerBlue); |
| |
| spriteBatch.Begin(SpriteBlendMode.AlphaBlend); |
| spriteBatch.Draw(backgroundtexture, viewportRect, Color.White); |
| |
| foreach (GameObject bullet in Bullets) |
| { |
| if (bullet.alive = true) |
| { |
| spriteBatch.Draw(bullet.sprite, |
| bullet.position, |
| Color.White); |
| } |
| } |
| |
| spriteBatch.Draw(Player.sprite, |
| Player.position, |
| null, |
| Color.White, |
| Player.rotation, |
| Player.center, 1.0f, |
| SpriteEffects.None, 0); |
| |
| |
| spriteBatch.End(); |
| |
| base.Draw(gameTime); |
| } |
| } |
| } |
| |