Here is the Game1 class:
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace New_Rock_Rain
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch = null;
Texture2D backgroundTexture;
private Texture2D meteorTexture;
private Ship player;
private void Start()
{
if (player == null)
{
player = new Ship(this, ref meteorTexture);
Components.Add(player);
}
player.PutInStartPosition();
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
backgroundTexture = Content.Load<Texture2D>("starfield");
meteorTexture = Content.Load<Texture2D>("SquareGuy");
Services.AddService(typeof(SpriteBatch), spriteBatch);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (player == null)
Start();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(
backgroundTexture, new Rectangle(0,0,
graphics.GraphicsDevice.DisplayMode.Width,
graphics.GraphicsDevice.DisplayMode.Height),
Color.White
);
spriteBatch.End();
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
// Draw the game components (sprites included)
base.Draw(gameTime);
// End rendering sprites
spriteBatch.End();
}
}
}
And here is the Ship.cs Gamne Component:
#region Using Statments
using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace New_Rock_Rain
{
public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
{
protected Texture2D texture;
protected Rectangle spriteRectangle;
protected Vector2 position;
protected const int SHIPWIDTH = 30;
protected const int SHIPHEIGHT = 30;
protected Rectangle screenBounds;
public Ship(Game game, ref Texture2D theTexture)
: base(game)
{
texture = theTexture;
position = new Vector2();
spriteRectangle = new Rectangle(31, 83, SHIPWIDTH, SHIPHEIGHT);
}
public void PutInStartPosition()
{
position.X = screenBounds.Width / 2;
position.Y = screenBounds.Height - SHIPHEIGHT;
}
public override void Initialize()
{
base.Initialize();
}
public override void Update(GameTime gameTime)
{
//This is so the user can move
KeyboardState keyboard = new KeyboardState();
if (keyboard.IsKeyDown(Keys.Up))
position.Y -= 3;
if (keyboard.IsKeyDown(Keys.Down))
position.Y += 3;
if (keyboard.IsKeyDown(Keys.Left))
position.X -= 3;
if (keyboard.IsKeyDown(Keys.Right))
position.X += 3;
//This keeps the ship inside the screen we have made
if (position.X < screenBounds.Left)
position.X = screenBounds.Left;
if (position.X > screenBounds.Right - SHIPWIDTH)
position.X = screenBounds.Right - SHIPWIDTH;
if (position.Y < screenBounds.Top)
position.Y = screenBounds.Top;
if (position.Y > screenBounds.Bottom - SHIPHEIGHT)
position.Y = screenBounds.Bottom - SHIPHEIGHT;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
SpriteBatch sBatch =
(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
sBatch.Draw(texture, position, spriteRectangle, Color.White);
base.Draw(gameTime);
}
public Rectangle GetBounds()
{
return new Rectangle((int)position.X, (int)position.Y, SHIPWIDTH, SHIPHEIGHT);
}
}
}

New Topic/Question
Reply



MultiQuote









|