Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mode: The game should have three player modes: player vs. player or player vs. c

ID: 3865749 • Letter: M

Question

Mode: The game should have three player modes: player vs. player or player vs. computer or computer vs. computer. Goal: Strategically place your ships throughout your part of the ocean to keep them away from the enemy. Try and sink the enemy by hitting their ships before they hit you! Skill Level: When the game is in player vs. computer mode, there should be 3 skills levels. The skill levels are as follows: Beginner: The computer randomly guesses selects coordinates. No intelligence involved. Intermediate: The computer will make an educated guess passed on previous hit. For instance, if computer achieved hit, it should check next available coordinate along x coordinate. Expert: The computer will make an educated guess passed on previous hit. For instance, if computer achieved hit, it should check next available coordinate along x axis and/or y axis. Ocean Grids: The Pacific Ocean is broken up into two ocean grids: one grid for each fleet. An ocean grid consists of a 10 x 10 grid. Each position on the grid will be represented by a two positive integers that constitutes a XY coordinate. Ships: There are a total of 5 ships for both the PBF and the IRF respectively. Each fleet has the same complement of ships. The description of the ships is listed below including the number of hits needed to sink a ship: Aircraft Carrier 5 hits Battleship-4 hits Cruiser-3 Hits Destroyer 3 hits Submarine-2 Hits

Explanation / Answer

before proceding further lets just think of a game, how it should be, how it should execute in an endless manner. Let's look at below pseudocode.

Hide Copy Code

Game1() – General initialization (Game1.cs)

Initialize() – Game initialization (Game1.cs)

LoadContent() – Load Graphics resources (Game1.cs)

Run() - Start game loop (Program.cs). In every step:

Update() - Read user input, do calculations, and test for game ending (Game1.cs)

Draw() – Renderization code (Game1.cs)

UnloadContent() – Free graphics resources (Game1.cs)

As the aboe pseudocode explains us everything. From program.cs we will create game1 object and invoke it.

The Draw method of game1 will call initialize only one time, then it will load content and start drawing objects on screen. In one second, on Xbox or Windows the update method will be called 60 times, so this is the place where we will read inputs, update game objects etc. Finally based on user input we will stop game and then its unload content method will be called.

Now look at below snippets, which is exactly same.

Hide Shrink Copy Code

/// <summary>

/// This is the main type for your game

public class Game1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

public Game1()

{

graphics = new GraphicsDeviceManager(this);

Content.RootDirectory = "Content";

// Frame rate is 30 fps by default for Windows Phone.

TargetElapsedTime = TimeSpan.FromTicks(333333);

// Extend battery life under lock.

InactiveSleepTime = TimeSpan.FromSeconds(1);

}

/// 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.

protected override void Initialize()

{

// TODO: Add your initialization logic here

base.Initialize();

}

/// LoadContent will be called once per game and is the place to load

/// all of your content.

protected override void LoadContent()

{

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here

}

/// UnloadContent will be called once per game and is the place to unload

/// all content.

protected override void UnloadContent()

{

// TODO: Unload any non ContentManager content here

}

/// Allows the game to run logic such as updating the world,

/// checking for collisions, gathering input, and playing audio.

/// <param name="gameTime" />Provides a snapshot of timing values.

protected override void Update(GameTime gameTime)

{

// Allows the game to exit

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);

}

/// This is called when the game should draw itself.

/// <param name="gameTime" />Provides a snapshot of timing values.

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime);

}

}

as its structure automatically explaining every thing. Till here you should have basic understanding of game, how it works etc. So we will start building game now and simultaneously I will keep explaining objects creating, moving and other animation concepts.

Start Game Development

Before stating developing Fighter Space Ship game, we need to plan for game, what all features will be there, how game will proceed, game state management.

What this game will do, you will be having a space ship which will be controlled by user input. There be meteors in galaxy will be moving and trying to block you. We need to escape from them and reach to our destination.

From above statement, we can thing of certain items as compnents of game.

Your space ship will be an object which will be controlled by user.

Meteor will be an object.

Your game1.cs, main object will be controlling everything. Galaxy need not be an object, as we can draw an galaxy image in game1.cs only.

Below code for space ship will be used as an component

Hide Shrink Copy Code

class SpaceShip : DrawableGameComponent

{

Texture2D shipTexture;

Vector2 shipPosition;

const int ShipHeight = 84;

const int ShipWidth = 84;

public SpaceShip(Game game)

: base(game)

{

}

public override void Initialize()

{

base.Initialize();

}

protected override void LoadContent()

{

shipTexture = Game.Content.Load<texture2d>("FighterSpaceShip");

shipPosition = new Vector2(Artifacts.GamePreferedBufferWidth / 2 - ShipWidth/2, Artifacts.GamePreferedBufferHeight - ShipHeight);

base.LoadContent();

}

protected override void UnloadContent()

{

base.UnloadContent();

}

public override void Update(GameTime gameTime)

{

//keyboard demo

KeyboardState keyboardState = Keyboard.GetState();

if (keyboardState.IsKeyDown(Keys.Up))

shipPosition += new Vector2(0, -3);

if (keyboardState.IsKeyDown(Keys.Down))

shipPosition += new Vector2(0, 3);

if (keyboardState.IsKeyDown(Keys.Left))

shipPosition += new Vector2(-3, 0);

if (keyboardState.IsKeyDown(Keys.Right))

shipPosition += new Vector2(3, 0);

//keep space ship in bounds

if (!GraphicsDevice.Viewport.Bounds.Contains(GetBounds()))

{

Rectangle screenBounds = GraphicsDevice.Viewport.Bounds;

if (shipPosition.X < screenBounds.Left)

{

shipPosition.X = screenBounds.Left;

}

if (shipPosition.X > screenBounds.Width - ShipWidth)

{

shipPosition.X = screenBounds.Width - ShipWidth;

}

if (shipPosition.Y < screenBounds.Top)

{

shipPosition.Y = screenBounds.Top;

}

if (shipPosition.Y > screenBounds.Height - ShipHeight)

{

shipPosition.Y = screenBounds.Height - ShipHeight;

}

}

base.Update(gameTime);

}

public override void Draw(GameTime gameTime)

{

SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

spriteBatch.Begin();

spriteBatch.Draw(shipTexture, shipPosition, Color.White);

spriteBatch.End();

base.Draw(gameTime);

}

public Rectangle GetBounds()

{

return new Rectangle((int)shipPosition.X, (int)shipPosition.Y,

ShipWidth, ShipHeight);

}

public void RestSpaceShip()

{

shipPosition = new Vector2(Artifacts.GamePreferedBufferWidth / 2 - ShipWidth / 2, Artifacts.GamePreferedBufferHeight - ShipHeight);

}

}

as you can see the update and draw method of class, update is accepting user input from keyboard which will help spcae ship to move. Also notice that update method is not letting the spce ship go out of screen. Draw method is drawing space ship on screen.

Same way we can create meteor component. Please find below code for that.

Hide Shrink Copy Code

public class Meteor : Microsoft.Xna.Framework.DrawableGameComponent

{

Vector2 meteorPosition = Vector2.Zero;

Vector2 meteorVelocity = Vector2.Zero;

Texture2D meterorTexture;

Random random;

const int MeterorHeight = 56;

const int MeterorWidth = 64;

public Meteor(Game game)

: base(game)

{

// TODO: Construct any child components here

}

public override void Initialize()

{

// TODO: Add your initialization code here

base.Initialize();

}

protected override void LoadContent()

{

meterorTexture = Game.Content.Load<texture2d>("meteorImg64");

random = new Random(this.GetHashCode());

base.LoadContent();

}

protected override void UnloadContent()

{

base.UnloadContent();

}

protected void UpdateMeteorPosition()

{

meteorPosition.X = random.Next(Game.Window.ClientBounds.Width - MeterorWidth);

meteorPosition.Y = 0;

meteorVelocity.Y = 1 + random.Next(7);

meteorVelocity.X = random.Next(7) - 1;

}

public override void Update(GameTime gameTime)

{

if (!GraphicsDevice.Viewport.Bounds.Contains(new Rectangle((int)meteorPosition.X, (int)meteorPosition.Y, meterorTexture.Width, meterorTexture.Height)))

{

UpdateMeteorPosition();

}

meteorPosition += meteorVelocity;

base.Update(gameTime);

}

public override void Draw(GameTime gameTime)

{

SpriteBatch spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;

if (meteorPosition == Vector2.Zero && meteorVelocity == Vector2.Zero)

UpdateMeteorPosition();

spriteBatch.Begin();

spriteBatch.Draw(meterorTexture, meteorPosition, Color.White);

spriteBatch.End();

base.Draw(gameTime);

}

public bool CheckCollision(Rectangle rect)

{

Rectangle spriterect = new Rectangle((int)meteorPosition.X, (int)meteorPosition.Y, MeterorWidth, MeterorHeight);

return spriterect.Intersects(rect);

}

}

as you can notice we are randomly creating a meteor and keep it moving on screen with its velocity which is randomly decided.

Finally we can create our game1.cs class which will join these componants and keep updating the game.

Hide Shrink Copy Code

public class SpaceGame : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

Texture2D galaxyBackImage;

SpaceShip ship=null;

const int MeteorCount = 5;

const int MeteorUpdateTime = 10000;

private int lastTickCount;

int rockCount = 0;

public SpaceGame()

{

graphics = new GraphicsDeviceManager(this);

Content.RootDirectory = "Content";

//graphics.IsFullScreen = true;

graphics.PreferredBackBufferWidth = Artifacts.GamePreferedBufferWidth;

graphics.PreferredBackBufferHeight = Artifacts.GamePreferedBufferHeight;

//graphics.ApplyChanges();

}

protected override void Initialize()

{

// TODO: Add your initialization logic here   

//if(ship==null)

// Components.Add(new SpaceShip(this));

//Components.Add(new Meteor(this));

//GameLevel = 1;

base.Initialize();

}

protected override void LoadContent()

{

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = new SpriteBatch(GraphicsDevice);

  

Services.AddService(typeof(SpriteBatch), spriteBatch);

galaxyBackImage = Content.Load<texture2d>("BGI_galaxy");

  

// TODO: use this.Content to load your game content here

}

protected override void UnloadContent()

{

// TODO: Unload any non ContentManager content here

}

protected override void Update(GameTime gameTime)

{

// Allows the game to exit

KeyboardState keyboard = Keyboard.GetState();

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || (keyboard.IsKeyDown(Keys.Escape)))

this.Exit();

if (ship == null)

{

StartGame();

}

UpdateGame();

base.Update(gameTime);

}

protected void UpdateGame()

{

// Check collisions

bool hasColision = false;

Rectangle shipRectangle =ship.GetBounds();

foreach (GameComponent gc in Components)

{

if (gc is Meteor)

{

hasColision = ((Meteor)gc).CheckCollision(shipRectangle);

if (hasColision)

{

// BOOM!

//explosion.Play();

// Remove all previous meteors

RemoveAllMeteors();

// Let's start again

StartGame();

break;

}

}

}

CheckforNewMeteor();

}

private void CheckforNewMeteor()

{

if ((System.Environment.TickCount - lastTickCount) > MeteorUpdateTime)

{

lastTickCount = System.Environment.TickCount;

Components.Add(new Meteor(this));

//newMeteor.Play();

rockCount++;

}

}

private void RemoveAllMeteors()

{

for (int i = 0; i < Components.Count; i++)

{

if (Components[i] is Meteor)

{

Components.RemoveAt(i);

i--;

}

}

}

protected void StartGame()

{

for (int i = 0; i < MeteorCount; i++)

Components.Add(new Meteor(this));

if (ship == null)

{

ship = new SpaceShip(this);

Components.Add(ship);

}

ship.RestSpaceShip();

  

lastTickCount = System.Environment.TickCount;

rockCount = MeteorCount;

}

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

spriteBatch.Draw(galaxyBackImage, new Rectangle(0, 0, graphics.GraphicsDevice.DisplayMode.Width, graphics.GraphicsDevice.DisplayMode.Height), Color.White);

spriteBatch.End();

base.Draw(gameTime);

}

}

public class Artifacts

{

public const int GamePreferedBufferHeight=600;

public const int GamePreferedBufferWidth=800;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote