C# XNA firing bullets using array - c#

Apparantly I have to show research efforts (and be clearer)?!?...
I have been trying to make a top-down space shooter from scratch using XNA. I have done this quite a few times in the past but havent been coding for a while and this is catching me out again. I am having a problem making the bullets fire how I want them too (I want a maximum specified number of bullets eg.5 on screen at any one time).
I have read many many articles on using arrays but for some reason I cannot see why they only appear as one bullet. I have debugged to the best of my ability and see that it seems they are all being created and 'fired' when space if pushed, however it seems they are all drawn in the same position and hence look like one bullet. Interestingly, the higher i set my 'maxBullets' variable the faster the bullets travel, as if the position -= speed is being applied to all of them each time I create a new Bullet1.
Can any one please help me find the solution. If you need any info if I have left something out, please just let me know. All help appreciated. Thanks :)
Code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 AlienAttacks
{
public class Player
{
Texture2D texture, bulletTexture1;
KeyboardState kbState;
public Vector2 position;
public int turnSpeed = 15, backSpeed = 2, forwardSpeed = 3;
Rectangle gameScreenBounds;
public int frameWidth, frameHeight, currentFrameX, currentFrameY;
Rectangle drawnRect;
Bullet[] bullets;
Bullet bullet1;
public int maxBullets = 3; // this will actually allow one extra bullet due to array starting at zero
public int bulletSpeed = 4;
public float FireTimer = 0.0f, FireRate = 0.8f;
public Player(Texture2D Texture, int FrameWidth, int FrameHeight, Rectangle GameScreenBounds, Texture2D BulletTexture1)
{
texture = Texture;
frameWidth = FrameWidth;
frameHeight = FrameHeight;
gameScreenBounds = GameScreenBounds;
bulletTexture1 = BulletTexture1;
bullet1 = new Bullet(bulletTexture1, bulletSpeed);
bullets = new Bullet[maxBullets];
for (int i = 0; i < maxBullets; i++)
{
bullets[i] = bullet1;
}
}
public void Update(GameTime gameTime)
{
drawnRect = new Rectangle(currentFrameX * frameWidth, currentFrameY * frameHeight, frameWidth, frameHeight);
kbState = Keyboard.GetState();
// Keyboard Controls
if (kbState.IsKeyDown(Keys.A) && position.X > gameScreenBounds.Left)
{
position.X -= turnSpeed;
}
if (kbState.IsKeyDown(Keys.D) && position.X + frameWidth < gameScreenBounds.Right)
{
position.X += turnSpeed;
}
if (kbState.IsKeyDown(Keys.W) && position.Y > gameScreenBounds.Top)
{
currentFrameX = 1;
position.Y -= forwardSpeed;
}
else
currentFrameX = 0;
if (kbState.IsKeyDown(Keys.S) && position.Y + frameHeight < gameScreenBounds.Bottom)
{
position.Y += backSpeed;
}
for (int i = 0; i < maxBullets; i++)
{
bullets[i] = bullet1;
bullets[i].Update(gameTime);
}
FireTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (kbState.IsKeyDown(Keys.Space))
{
for (int i = 0; i < maxBullets; i++)
{
if (FireTimer >= FireRate)
{
if (!bullets[i].IsAlive)
{
bullets[i].IsAlive = true;
bullets[i].position = position;
FireTimer = 0.0f;
}
}
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, drawnRect, Color.White);
for (int i = 0; i < maxBullets; i++)
{
bullets[i].Draw(spriteBatch);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 AlienAttacks
{
public class Bullet
{
public Texture2D texture;
public Vector2 position;
public int speed;
public bool IsAlive = false;
public Bullet(Texture2D Texture, int Speed)
{
texture = Texture;
speed = Speed;
}
public void Update(GameTime gameTime)
{
if (IsAlive)
{
position.Y -= speed;
// if bullet goes off top of screen...
if (position.Y - texture.Height < 0)
{
BulletDead();
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
if (IsAlive)
spriteBatch.Draw(texture, position, Color.White);
}
public void BulletDead()
{
IsAlive = false;
}
}
}
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 AlienAttacks
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Rectangle gameScreenBounds;
Texture2D hudBGTexture;
Rectangle hudRect;
int hudPositionY;
Texture2D p1Texture, p1bulletTexture1;
Player player1;
Vector2 player1StartPosition;
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
graphics.PreferredBackBufferWidth = 1600;
graphics.PreferredBackBufferHeight = 900;
//graphics.IsFullScreen = true;
graphics.ApplyChanges();
hudRect = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, 100);
hudPositionY = graphics.PreferredBackBufferHeight - hudRect.Height;
gameScreenBounds = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight - hudRect.Height);
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);
hudBGTexture = Content.Load<Texture2D>("hud");
p1bulletTexture1 = Content.Load<Texture2D>("shot");
p1Texture = Content.Load<Texture2D>("red");
player1 = new Player(p1Texture, 144, 104, gameScreenBounds, p1bulletTexture1);
player1StartPosition = new Vector2(graphics.PreferredBackBufferWidth / 2 - player1.frameWidth / 2, graphics.PreferredBackBufferHeight - hudRect.Height - player1.frameHeight);
player1.position = player1StartPosition;
}
/// <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
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
player1.Update(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>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
SpriteBatch targetBatch = new SpriteBatch(GraphicsDevice);
RenderTarget2D target = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
GraphicsDevice.SetRenderTarget(target);
spriteBatch.Begin();
player1.Draw(spriteBatch);
spriteBatch.Draw(hudBGTexture, new Vector2(0, hudPositionY), Color.WhiteSmoke);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
targetBatch.Begin();
targetBatch.Draw(target, new Rectangle(0, 0, GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height), Color.White);
targetBatch.End();
base.Draw(gameTime);
}
}
}

It looks like each item in your array points to the one instance.
bullet1 = new Bullet(bulletTexture1, bulletSpeed);
bullets = new Bullet[maxBullets];
for (int i = 0; i < maxBullets; i++)
{
bullets[i] = bullet1;
}
Should be like this:
bullets = new Bullet[maxBullets];
for (int i = 0; i < maxBullets; i++)
{
bullets[i] = new Bullet(bulletTexture1, bulletSpeed);
}

Related

XNA spawning multiple enemies

Ok, I'm sure that i am just missing something really easy because i just can't find the answer ANYWHERE. I am making an infinite runner game(2d), but i can get one enemy spawning right at the start, and that's it. What the heck am i missing? Here's the code:
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 WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
static Random enemyspawner = new Random();
int enemyloc = enemyspawner.Next(500) + 500;
/*GraphicsDeviceManager, SpriteBatch, Texture2D and Vector2 may be found only in XNA. They are just used for drawing objects and defining locations.*/
GraphicsDeviceManager graphic;
SpriteBatch SpriteBatch;
Texture2D charr;
int enemyspeed = 1;
Vector2 charPos;
bool jumping; //Is the character jumping?
float startY, jumpspeed = 0; //startY to tell us //where it lands, jumpspeed to see how fast it jumps
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D grass;
Texture2D enemy;
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
charr = Content.Load<Texture2D>(#"dog_jump"); //Load image
charPos = new Vector2(230, 415);//Char loc, X/Y
grass = Content.Load<Texture2D>(#"grass1");
enemy = Content.Load<Texture2D>(#"fire hydrant");
startY = charPos.Y;//Starting position
jumping = false;//Init jumping to false
jumpspeed = 0;//Default no speed
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);
// 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>
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)
{
for (int x = 0; x == 1000; x++ )
{
enemyspeed++;
}
for (int x = 0; x == 75; x++)
{
x = 0;
spriteBatch.Begin();
spriteBatch.Draw(enemy, new Vector2(enemyloc, 450), Color.White);
spriteBatch.End();
enemyloc = enemyspawner.Next(500);
}
enemyloc -= enemyspeed;
//Init keyboard
KeyboardState keyState = Keyboard.GetState();
if (jumping)
{
charPos.Y += jumpspeed;//Making it go up
jumpspeed += 1;//Some math (explained later)
if (charPos.Y >= startY)
//If it's farther than ground
{
charPos.Y = startY;//Then set it on
jumping = false;
}
}
else
{
if (keyState.IsKeyDown(Keys.Space))
{
jumping = true;
jumpspeed = -12;//Give it upward thrust
}
}
base.Update(gameTime);
}
/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(grass, new Vector2(0, 450), Color.White);
spriteBatch.Draw(charr, charPos, Color.White);
spriteBatch.End();
Spawner();
base.Draw(gameTime);
}
public void Spawner()
{
spriteBatch.Begin();
spriteBatch.Draw(enemy, new Vector2(enemyloc, 450 - 50), Color.White);
spriteBatch.End();
}
}
}
Also, yes i did get te gravity code off the internet, nothing that i tried was working
In the second for loop in the update method you are setting x to zero every time. That will prevent it from getting outside of that loop and that would explain the behaviour you have seen
first of all, this doesn't make any sense:
for (int x = 0; x == 1000; x++ )
{
enemyspeed++;
}
This only increases the speed if x equals 1000.
You will get the same result if you remove the for loop, but you probably want this: for (int x = 0; x <1000; x++ )//increases speed 1000 times
You also got a problem here:
for (int x = 0; x == 75; x++)//same here as mentioned above
{
x = 0;
spriteBatch.Begin();
spriteBatch.Draw(enemy, new Vector2(enemyloc, 450), Color.White);
spriteBatch.End();
enemyloc = enemyspawner.Next(500);
}
Also, you keep setting x to 0, so this loop will run forever.
EDIT:
for (int x = 0; x == 1000; x++ )
I looked at it again and I coudn't find any reason why you would increase the speed 1000 times, by using a for loop. So I believe/assume you want to update the speed after 1000 updates? In that case, this is what you want:
//Add a atrubit count to your class:
int count = 0;
//Update method:
protected override void Update(GameTime gameTime)
{
count++;//update count
if(count % 1000 == 0){
enemyspeed++;//update speed after 1000 updates
}
if(count % 75 == 0){//draw after 75 updates
spriteBatch.Begin();
spriteBatch.Draw(enemy, new Vector2(enemyloc, 450), Color.White);
spriteBatch.End();
enemyloc = enemyspawner.Next(500);
}
//do your other stuff
}

C# XNA Collision detection with random falling sprite

i am new in XNA .i am trying to build a small 2D game where enemy(ball) are falling from the top of the screen randomly.the player can move inside the screen. what i want to do is when the player(spaceShip) collide with the ball the ball will remove from the screen.And i don't know how to do that. can anyone help me with this?
here's my code-
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 RandomSprite
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
TimeSpan timeSpan = TimeSpan.FromMilliseconds(2000);
Sprite spaceShip;
Texture2D ballTexture;
Texture2D backgroundtexture;
Vector2 ballPos = new Vector2(100f, 100f);
List<Sprite> ballList = new List<Sprite>();
float timer = 0f;
float dropInterval = .50f;
float speed = 4f;
Random random;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
random = new Random();
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
backgroundtexture = Content.Load<Texture2D>("dungeon600x400");
spriteBatch = new SpriteBatch(GraphicsDevice);
ballTexture = Content.Load<Texture2D>("ball");
spaceShip = new Sprite();
spaceShip.Texture = Content.Load<Texture2D>("gugu");
// retrieve the height of the screen
int screenHeight = GraphicsDevice.Viewport.Height;
// find the center point of the screen along the x-axis
int screenCenterX = GraphicsDevice.Viewport.Width / 2;
spaceShip.Position = new Vector2(
screenCenterX - (spaceShip.Texture.Width / 2),
screenHeight - spaceShip.Texture.Height - 20);
}
private void HandleInput()
{
// Retrieve the current state of the keyboard
KeyboardState keyboardState = Keyboard.GetState();
Vector2 playerVelocity = Vector2.Zero;
// Check if the Left arrow key is pressed and change the velocity of the character accordingly
if (keyboardState.IsKeyDown(Keys.Left))
{
playerVelocity += new Vector2(-speed, 0);
}
// Check if the Right arrow key is pressed and change the velocity of the character accordingly
if (keyboardState.IsKeyDown(Keys.Right))
{
playerVelocity += new Vector2(speed, 0);
}
if (keyboardState.IsKeyDown(Keys.Up))
{
playerVelocity += new Vector2(0, -speed);
}
if (keyboardState.IsKeyDown(Keys.Down))
{
playerVelocity += new Vector2(0, speed);
}
// Apply the velocity to the character's position
spaceShip.Position += playerVelocity;
}
public void HandleFallingCake()
{
List<Sprite> toRemove = new List<Sprite>();
foreach (Sprite ball in ballList)
{
if (ball.Position.Y > (GraphicsDevice.Viewport.Height - 100))
toRemove.Add(ball);
else
ball.Position += new Vector2(0, speed);
}
if (toRemove.Count==1 )
{
foreach (Sprite cake in toRemove)
{
ballList.Remove(cake);
}
}
}
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
timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (timer >= dropInterval)
{
int xPos = random.Next(GraphicsDevice.Viewport.Width-50);
ballList.Add(new Sprite(ballTexture, new Vector2(xPos, -100)));
timer = 0f;
}
HandleFallingCake();
HandleInput();
timeSpan -= gameTime.ElapsedGameTime;
if (timeSpan <= TimeSpan.Zero)
{
timeSpan = TimeSpan.FromMilliseconds(2000);
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
//spriteBatch.Draw(backgroundtexture, new Rectangle(0,0,GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
spaceShip.Draw(spriteBatch);
foreach (Sprite ball in ballList)
{
ball.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
and i have a subclass sprite.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RandomSprite
{
class Sprite
{
public Texture2D Texture;
public Vector2 Position ;
public Sprite() { }
public Sprite(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
}
public void Draw(SpriteBatch spriteBatch)
{
if (Texture != null)
spriteBatch.Draw(Texture, Position, Color.White);
}
}
}
You describe the goal, but not ask questions.
If you want to know how to check the intersection of two objects, then you need to define for them bounds (class Rectangle) and check their intersection method Rectangle.Intersect. Anyway better adjust your question.

Boundaries working fine in bottom, right side but not on the left, top

I am following: http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/animating_the_player and it was when during implementing the animation something went wrong. But I am not sure why:
Game1.cs (the only method I changed here was LoadContent()-method and UpdatePlayer()-method.
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.Input.Touch;
namespace Shooter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Represents the player
Player player;
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;
GamePadState currentGamePadState;
GamePadState previousGamePadState;
float playerMoveSpeed;
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
player = new Player();
playerMoveSpeed = 8.0f;
TouchPanel.EnabledGestures = GestureType.FreeDrag;
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);
// TODO: use this.Content to load your game content here
//Load the player resources
// Load the player resources
Animation playerAnimation = new Animation();
Texture2D playerTexture = Content.Load<Texture2D>("shipAnimation");
playerAnimation.Initialize(playerTexture, Vector2.Zero, 115, 69, 8, 30, Color.White, 1f, true);
Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y
+ GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
player.Initialize(playerAnimation, playerPosition);
}
/// <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
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
previousGamePadState = currentGamePadState;
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
currentGamePadState = GamePad.GetState(PlayerIndex.One);
UpdatePlayer(gameTime);
base.Update(gameTime);
}
private void UpdatePlayer(GameTime gameTime)
{
player.Update(gameTime);
player.Position.X += currentGamePadState.ThumbSticks.Left.X *playerMoveSpeed;
player.Position.Y -= currentGamePadState.ThumbSticks.Left.Y *playerMoveSpeed;
if (currentKeyboardState.IsKeyDown(Keys.Left) || currentGamePadState.DPad.Left == ButtonState.Pressed)
{
player.Position.X -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Right) || currentGamePadState.DPad.Right == ButtonState.Pressed)
{
player.Position.X += playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Up) || currentGamePadState.DPad.Up == ButtonState.Pressed)
{
player.Position.Y -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Down) || currentGamePadState.DPad.Down == ButtonState.Pressed)
{
player.Position.Y += playerMoveSpeed;
}
//Make sure the player does not go out of bounds
player.Position.X = MathHelper.Clamp(player.Position.X, 0, GraphicsDevice.Viewport.Width - player.Width);
player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, GraphicsDevice.Viewport.Height - player.Height);
}
/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
player.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Player.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Shooter
{
class Player
{
//Animation representing the player
public Animation PlayerAnimation;
//Position of the Player relative to the upper left side of the screen
public Vector2 Position;
//State of the player
public bool Active;
//Amount of hit points that player has
public int Health;
//Get the width of the player ship
public int Width
{
get { return PlayerAnimation.FrameWidth; }
}
//Get the height of the player ship
public int Height
{
get { return PlayerAnimation.FrameHeight; }
}
public void Initialize(Animation animation, Vector2 position)
{
PlayerAnimation = animation;
//Set the starting position of the player around the middle of the screen and to the back
Position = position;
//Set the player to be active
Active = true;
//Set the player health
Health = 100;
}
public void Update(GameTime gameTime)
{
PlayerAnimation.Position = Position;
PlayerAnimation.Update(gameTime);
}
public void Draw(SpriteBatch spriteBatch)
{
PlayerAnimation.Draw(spriteBatch);
}
}
}
Animation.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace Shooter
{
class Animation
{
// The image representing the collection of images used for animation
Texture2D spriteStrip;
// The scale used to display the sprite strip
float scale;
// The time since we last updated the frame
int elapsedTime;
// The time we display a frame until the next one
int frameTime;
// The number of frames that the animation contains
int frameCount;
// The index of the current frame we are displaying
int currentFrame;
// The color of the frame we will be displaying
Color color;
// The area of the image strip we want to display
Rectangle sourceRect = new Rectangle();
// The area where we want to display the image strip in the game
Rectangle destinationRect = new Rectangle();
// Width of a given frame
public int FrameWidth;
// Height of a given frame
public int FrameHeight;
// The state of the Animation
public bool Active;
// Determines if the animation will keep playing or deactivate after one run
public bool Looping;
// Width of a given frame
public Vector2 Position;
public void Initialize(Texture2D texture, Vector2 position,
int frameWidth, int frameHeight, int frameCount,
int frametime, Color color, float scale, bool looping)
{
// Keep a local copy of the values passed in
this.color = color;
this.FrameWidth = frameWidth;
this.FrameHeight = frameHeight;
this.frameCount = frameCount;
this.frameTime = frametime;
this.scale = scale;
Looping = looping;
Position = position;
spriteStrip = texture;
// Set the time to zero
elapsedTime = 0;
currentFrame = 0;
// Set the Animation to active by default
Active = true;
}
public void Update(GameTime gameTime)
{
// Do not update the game if we are not active
if (Active == false)
return;
// Update the elapsed time
elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
// If the elapsed time is larger than the frame time
// we need to switch frames
if (elapsedTime > frameTime)
{
// Move to the next frame
currentFrame++;
// If the currentFrame is equal to frameCount reset currentFrame to zero
if (currentFrame == frameCount)
{
currentFrame = 0;
// If we are not looping deactivate the animation
if (Looping == false)
Active = false;
}
// Reset the elapsed time to zero
elapsedTime = 0;
}
// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);
// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,
(int)Position.Y - (int)(FrameHeight * scale) / 2,
(int)(FrameWidth * scale),
(int)(FrameHeight * scale));
}
public void Draw(SpriteBatch spriteBatch)
{
if (Active)
{
spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);
}
}
}
}
In Game1.cs this is what checking the boundaries. However, this was not changed in this part of the tutorial, so I am not sure why it started to happen, have I missed something? Anything you guys can point out that I did wrong?
//Make sure the player does not go out of bounds
player.Position.X = MathHelper.Clamp(player.Position.X, 0, GraphicsDevice.Viewport.Width - player.Width);
player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, GraphicsDevice.Viewport.Height - player.Height);
After looking at your code, My best guess is that the problem is with the draw function of your animation and how destinationRect is defined.
It looks like the game thinks that the center of your Animation is the top-left corner of your animation..
I think maybe you want to set destinationRect in the Update method of your animation like so
// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
destinationRect = new Rectangle(
(int)Position.X,
(int)Position.Y,
(int)(FrameWidth * scale),
(int)(FrameHeight * scale));
with the Rectangle's X and Y coordinates being the top left corner of your animation instead of the center.

why my ball is blurry when I started the game in xna 4.0?

I am using this code, but I still dont figure out why the ball is blurry when I started the game, can someone help me??, I just tryingto to me the ball when the user click but I dont know why It is blurry, I still cheking the code, please help
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 Critters
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ball;
Vector2 playerPosition;
Vector2 direction;
Vector2 destination;
float speed;
float playerPosX;
float playerPosY;
MouseState oldState;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.IsMouseVisible = true;
}
/// <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
graphics.PreferredBackBufferWidth = 700;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Darrells Game";
playerPosY = graphics.GraphicsDevice.Viewport.Height / 10 * 8;
playerPosX = graphics.GraphicsDevice.Viewport.Width / 2;
playerPosition = new Vector2(playerPosX, playerPosY);
destination = playerPosition;
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);
ball = Content.Load<Texture2D>("orb");
// TODO: use this.Content to load your game content her
}
/// <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
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Vector2.DistanceSquared(destination, playerPosition) >= speed * speed)
{
MovePlayer();
}
else
{
playerPosition = destination;
}
MouseState leftState = Mouse.GetState();
MouseState rightState = Mouse.GetState();
if ((leftState.LeftButton == ButtonState.Pressed && rightState.RightButton == ButtonState.Pressed))
{
int mouseX = leftState.X;
int mouseY = leftState.Y;
destination = new Vector2(mouseX, mouseY);
speed = 8.0f;
}
else if (leftState.LeftButton == ButtonState.Pressed)
{
int mouseX = leftState.X;
int mouseY = leftState.Y;
destination = new Vector2(mouseX, mouseY);
speed = 2.0f;
}
base.Update(gameTime);
}
/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
DrawPlayer();
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
private void DrawPlayer()
{
Vector2 textureCentre = new Vector2(ball.Width / 2, ball.Height / 2);
spriteBatch.Draw(ball, playerPosition, null, Color.White, 0f, textureCentre, 0.8f, SpriteEffects.None, 1f);
}
private void MovePlayer()
{
{
direction = destination - playerPosition;
direction.Normalize();
playerPosition += direction * speed;
}
}
}
}
In your draw call:
spriteBatch.Draw
(
ball, // texture
playerPosition, // location
null, // source rect
Color.White, // color
0f, // rotation
textureCentre, // origin
0.8f, // scale
SpriteEffects.None, // effects
1f // layerDepth
);
You have set the scale to 0.8f. This will draw your texture smaller than it really is, which will involve some 'blurring'. Changing the scale value to 1f should get rid of any blurring caused by scaling.
EDIT: If the blurring you mean is the flickering that happens before you have clicked, that is caused by having a destination that is exactly equal to the current location. Your MovePlayer method then causes the current position to become (NaN, NaN):
// before the user has clicked, destination == playerPosition
private void MovePlayer()
{
direction = destination - playerPosition; // direction = (0, 0)
direction.Normalize(); // direction = (NaN, NaN)
playerPosition += direction * speed; // playerPosition = (NaN, NaN)
}
A quick fix for this would be in your Initialize method, change the line:
protected override void Initialize()
{
...
// change this
mDestination = mPlayerPosition;
// to this
mDestination = mPlayerPosition + new Vector2(1, 1);

Pixel Perfect Collision Is Causing Inaccurate Collision With Maze And PacMan

I am creating a PacMan clone just for fun and I am using the pixel perfect collision algorithm to detect when PacMan collides with the maze and the dots (which has not been implemented in the game yet). I know I am using the algorithm correctly but the PacMan is collided with the maze when both the maze and PacMan and are not near each other. I picture for proof showing the debugging screen and the game. Here is the image and code below:
Main Code:
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 PacMan_Bytes
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
enum Direction
{
Left,
Right,
Up,
Down
};
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D maze;
Texture2D pacman;
Color[] mazeclr = new Color[484 * 483];
Color[] pacmanclr = new Color[20 * 27];
Rectangle dest;
Rectangle src;
int frame = 2;
float rotation = 0.0f;
Direction dir;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 484;
graphics.PreferredBackBufferHeight = 483;
dest = new Rectangle(50, 455, 20, 27);
src = new Rectangle(frame, 0, 20, 27);
}
/// <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);
maze = Content.Load<Texture2D>("maze");
pacman = Content.Load<Texture2D>("pacman");
maze.GetData<Color>(mazeclr);
// Get the colors that comprises the image for pixel perfect collision.
pacman.GetData<Color>(0, src, pacmanclr, 0, 20 * 27);
}
/// <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
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 13.0f);
// Each frame check if the PacMan has collided with the maze.
bool collision = PacmanAnimator.CollidedWithMaze(dest, pacmanclr, maze.Bounds, mazeclr);
// If PacMan doesn't collide with the maze allow the PacMan to move.
if (collision == false)
{
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
PacmanAnimator.AnimateAndMoveLeft(ref rotation, ref frame, ref src, ref dest);
dir = Direction.Left;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
PacmanAnimator.AnimateAndMoveRight(ref rotation, ref frame, ref src, ref dest);
dir = Direction.Right;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
PacmanAnimator.AnimateAndMoveUp(ref rotation, ref frame, ref src, ref dest);
dir = Direction.Up;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
PacmanAnimator.AnimateAndMoveDown(ref rotation, ref frame, ref src, ref dest);
dir = Direction.Down;
}
}
// If collision is true move the PacMan away from the maze's border.
else if (collision == true)
{
if (dir == Direction.Down)
{
dest.Y -= 1;
}
else if (dir == Direction.Up)
{
dest.Y += 1;
}
else if (dir == Direction.Left)
{
dest.X += 1;
}
else if (dir == Direction.Right)
{
dest.X -= 1;
}
}
if (dest.X < 0)
{
dest.X = graphics.PreferredBackBufferWidth - 20;
}
base.Update(gameTime);
}
/// <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)
{
GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin();
spriteBatch.Draw(maze, Vector2.Zero, Color.White);
spriteBatch.Draw(pacman, dest, src, Color.White, rotation, new Vector2(20 / 2, 24 / 2), SpriteEffects.None, 1);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
PacmanAnimator 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;
namespace PacMan_Bytes
{
static class PacmanAnimator
{
// Animates and moves the PacMan to the left.
public static void AnimateAndMoveLeft(ref float rotation, ref int frame, ref Rectangle src, ref Rectangle dest)
{
switch (frame)
{
case 2:
{
frame = 40;
break;
}
case 40:
{
frame = 2;
break;
}
}
src.X = frame;
dest.X -= 7;
rotation = MathHelper.ToRadians(180.0f);
}
// Animates and moves the sprite to the right.
public static void AnimateAndMoveRight(ref float rotation, ref int frame, ref Rectangle src, ref Rectangle dest)
{
switch (frame)
{
case 2:
{
frame = 40;
break;
}
case 40:
{
frame = 2;
break;
}
}
src.X = frame;
dest.X += 7;
rotation = 0.0f;
}
// Moves and animates the sprite upward.
public static void AnimateAndMoveUp(ref float rotation, ref int frame, ref Rectangle src, ref Rectangle dest)
{
switch (frame)
{
case 2:
{
frame = 40;
break;
}
case 40:
{
frame = 2;
break;
}
}
src.X = frame;
dest.Y -= 7;
rotation = MathHelper.ToRadians(270.0f);
}
// Moves the sprite downward and animates it.
public static void AnimateAndMoveDown(ref float rotation, ref int frame, ref Rectangle src, ref Rectangle dest)
{
switch (frame)
{
case 2:
{
frame = 40;
break;
}
case 40:
{
frame = 2;
break;
}
}
src.X = frame;
dest.Y += 7;
rotation = MathHelper.ToRadians(90.0f);
}
// Subroutine that deals with collision.
public static bool CollidedWithMaze(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB)
{
// Find the bounds of the rectangle intersection
int top = Math.Max(rectangleA.Top, rectangleB.Top);
int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
int left = Math.Max(rectangleA.Left, rectangleB.Left);
int right = Math.Min(rectangleA.Right, rectangleB.Right);
// Check every point within the intersection bounds
for (int y = top; y < bottom; y++)
{
for (int x = left; x < right; x++)
{
// Get the color of both pixels at this point
Color colorA = dataA[(x - rectangleA.Left) +
(y - rectangleA.Top) * rectangleA.Width];
Color colorB = dataB[(x - rectangleB.Left) +
(y - rectangleB.Top) * rectangleB.Width];
// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
{
// then an intersection has been found
return true;
}
}
}
// No intersection found
return false;
}
}
}
PS. The PacMan sprite has a transparent background and the black areas you see on the maze are transparent. So I know that is not the problem. By the way sorry for such a long post.
Edit: Added comments to make code more understandable.
Your problem is that you're trying to test "not completely transparent" with this line:
// If both pixels are not completely transparent,
if (colorA.A != 0 && colorB.A != 0)
But that's the opposite: that is testing if they're NOT completely OPAQUE.
Completely transparent would be alpha 255, or 1.0f, depending on int/float.
After doing some research I found out it is better to do tile base collision rather than pixel perfect collision.

Categories