Jump to content
  • Advertisement
Sign in to follow this  
Xneight

Drawing the Texture2D elements in a List

This topic is 960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I am trying to develop a game where 2 different objects fall from the top of  the screen and the person on the bottom of the screen needs to catch one of the and avoid the other object.
What I am hugely struggling with is I couldn't  draw two of the objects that's in a Texture2D list.

What I have is only 1 of the object falling in different positions and I need both. 

 

 

 

namespace 
RectangleCollision
{
 
public class RectangleCollisionGame : Microsoft.Xna.Framework.Game
{
 
GraphicsDeviceManager graphics;
Texture2D personTexture;
SpriteBatch spriteBatch;
Vector2 personPosition;
 
const int PersonMoveSpeed = 5;
 
List<Vector2> blockPositions = new List<Vector2>();
float BlockSpawnProbability = 0.01f;
const int BlockFallSpeed = 2;
 
List<Texture2D> blockTexture = new List<Texture2D>();
 
Random random = new Random();
 
// For when a collision 
is detected
bool personHit = false;
 
// The sub-rectangle of 
the drawable area which should be visible on all TVs
Rectangle safeBounds;
 
// Percentage of the 
screen on every side is the safe area
const float SafeAreaPortion = 0.05f;
?
 
public RectangleCollisionGame()
{
graphics = 
new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
?
 
 
protected override void Initialize()
{
base.Initialize();
// Calculate safe bounds 
based on current resolution


Viewport viewport = graphics.GraphicsDevice.Viewport;
safeBounds = new Rectangle((int)(viewport.Width * SafeAreaPortion),
(int)(viewport.Height * SafeAreaPortion),
(int)(viewport.Width * (1 - 2 * SafeAreaPortion)),
(int)(viewport.Height * (1 - 2 * SafeAreaPortion)));
 
// Start the player in 
the center along the bottom of the screen
personPosition.X = (safeBounds.Width - personTexture.Width) / 2;
personPosition.Y = safeBounds.Height - 
personTexture.Height;
}

protected override void LoadContent()
{
blockTexture.Add(Content.Load<Texture2D>("Block"));
blockTexture.Add(Content.Load<Texture2D>("right"));
 
personTexture = 
Content.Load<Texture2D>("Person");
 

// Create a sprite batch 
to draw those textures
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
}
?
protected override void Update(GameTime gameTime)
{
// Get input
KeyboardState keyboard = Keyboard.GetState();
GamePadState gamePad = GamePad.GetState(PlayerIndex.One);

// Move the player left and right with arrow keys or d-pad
if (keyboard.IsKeyDown(Keys.Left) ||
gamePad.DPad.Left == ButtonState.Pressed)
{
personPosition.X -= PersonMoveSpeed;
}
 
if (keyboard.IsKeyDown(Keys.Right) ||
gamePad.DPad.Right == ButtonState.Pressed)
{
personPosition.X += PersonMoveSpeed;
}


// Spawn new falling blocks
if (random.NextDouble() < BlockSpawnProbability)
{
float x = (float)random.NextDouble() * (Window.ClientBounds.Width - 
blockTexture[0].Width);
blockPositions.Add(new Vector2(x, -blockTexture[0].Height));
}
 
// Update each block
personHit = false;
for (int i = 0; i < blockPositions.Count; i++)
{
// Animate this block falling
blockPositions[i] = new Vector2(blockPositions[i].X, 
blockPositions[i].Y + BlockFallSpeed);
// Get the bounding rectangle of this block
Rectangle sprite = new Rectangle((int)blockPositions[i].X, (int)blockPositions[i].Y,
blockTexture[0].Width, blockTexture[0].Height);

// check collision with person
if (personRectangle.Intersects(sprite))
personHit = true;
 
// Remove this block if it have fallen off the screen
 if ((blockPositions[i].Y > Window.ClientBounds.Height))
{ 
  blockPositions.RemoveAt(i);
  i--;
}
}
base.Update(gameTime);
}

protected override void Draw(GameTime 
gameTime)
{
GraphicsDevice device = graphics.GraphicsDevice;

// Draw 
person
spriteBatch.Draw(personTexture, personPosition, Color.White);
 

// Draw blocks
foreach (Vector2 blockPosition in blockPositions)
{
 for (int i = 0; i < blockPositions.Count; i++ 
)
 {
   spriteBatch.Draw(blockTexture[i], blockPosition, Color.White);
 }
}
 
base.Draw(gameTime);
spriteBatch.End();
}
}
}

 

I am really sorry I am sending major part of the question but I've been trying to make this work for the past 4 hours and I really really need help please.

 

Share this post


Link to post
Share on other sites
Advertisement
// Draw blocks
foreach (Vector2 blockPosition in blockPositions)
{
    for (int i = 0; i < blockPositions.Count; i++) 
    {
        spriteBatch.Draw(blockTexture[i], blockPosition, Color.White);
    }
}

This code makes no sense to me. Why do you have nested for loops? If you have 4 positions in blockPositions you'll be drawing 16 sprites.

 

Moreoever, why are you using a blockPosition index to index the blockTexture list? It looks like you only have 2 textures in your list, so if you have more than 2 positions in the list, your game will throw an argument out of range exception.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!