Basic XNA Game theory, with sprite loading and its movement and Collision detection

// In this programme I just want to introduce some basic concepts i.e the steps the flow of

// the windows game programme using C# in XNA development environment.

// for taking the complete project you have to email me at jawadnawaz@live.com

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;

namespace

JawadGame

{

///<summary>

/// This is the main type for your game

///</summary>

publicclassGame1 : Microsoft.Xna.Framework.Game

{

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

Rectangle mainFrame, playSize, pauseSize, jerrySize, tomSize, sonicSize, soniccSize;

public Game1()

{

graphics =

newGraphicsDeviceManager(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>

protectedoverridevoid 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>

// This is a texture we can render.

Texture2D bg, play, pause, tom, jerry, sonic, sonicc;

protectedoverridevoid LoadContent()

{

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

spriteBatch =

newSpriteBatch(GraphicsDevice);

bg = Content.Load<Texture2D>(“Textures/bg”);

mainFrame = newRectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

play = Content.Load<Texture2D>(“Textures/play”);

playSize = newRectangle(0, 0, (int)(play.Width * 0.5), (int)(play.Height * 0.5));

pause = Content.Load<Texture2D>(“Textures/pause”);

pauseSize = newRectangle(750, 0, (int)(pause.Width * 0.5), (int)(pause.Height * 0.5));

sonic = Content.Load<Texture2D>(“Textures/sonic”);

sonicSize = newRectangle(242, 140, (int)(sonic.Width * 0.9), (int)(sonic.Height * 0.9));

tom = Content.Load<Texture2D>(“Textures/tom”);

tomSize = newRectangle(470, 175, (int)(tom.Width * 0.8), (int)(tom.Height * 0.8));

jerry = Content.Load<Texture2D>(“Textures/jerry”);

jerrySize = newRectangle(20, 300, (int)(jerry.Width * 0.3), (int)(jerry.Height * 0.3));

sonicc = Content.Load<Texture2D>(“Textures/sonicc”);

soniccSize = newRectangle(800, 0, (int)(sonicc.Width * 0.8), (int)(sonicc.Height * 0.9));

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

}

///<summary>

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

/// all content.

///</summary>

protectedoverridevoid 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>

protectedoverridevoid Update(GameTime gameTime)

{

soniccSize.X -= 1;

KeyboardState key = Keyboard.GetState();

if (key.IsKeyDown(Keys.Right))

{

jerrySize.X += 1;

}

elseif (key.IsKeyDown(Keys.Left))

{

jerrySize.X -= 1;

}

elseif (key.IsKeyDown(Keys.Up))

{

jerrySize.Y -= 1;

}

elseif (key.IsKeyDown(Keys.Down))

{

jerrySize.Y += 1;

}

//Reset all objects on initial locations.

elseif (key.IsKeyDown(Keys.Space))

{

jerrySize.X = 20;

jerrySize.Y = 300;

tomSize.X = 500;

soniccSize.X = 800;

soniccSize.Y = 0;

}

//collision detection between Tom and Jerry.

if (jerrySize.Intersects(tomSize))

{

tomSize.X += tomSize.X / 13;

}

// 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);

}

publicvoid movement(GameTime gameTime)

{

base.Update(gameTime);

}

///<summary>

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

///</summary>

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

protectedoverridevoid Draw(GameTime gameTime)

{

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// Draw the sprite.

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

spriteBatch.Draw(bg, mainFrame, Color.White);

spriteBatch.Draw(sonicc, soniccSize, Color.White);

spriteBatch.Draw(sonic, sonicSize, Color.White);

spriteBatch.Draw(tom, tomSize, Color.White);

spriteBatch.Draw(jerry, jerrySize, Color.White);

spriteBatch.Draw(play, playSize, Color.White);

spriteBatch.Draw(pause, pauseSize, Color.White);

spriteBatch.End();

base.Draw(gameTime);

}

}

}

Leave a comment