XNA Creators Club Online
Page 1 of 1 (10 items)
Sort Posts: Previous Next

How do I draw a texture more than once?

Last post 3/18/2009 12:04 PM by u551. 9 replies.
  • 3/16/2009 7:30 PM

    How do I draw a texture more than once?

    Hi,

    I have started programming in C# a month ago, then I decided that I wanted to make a game, so I got the XNA studio and started tinkering.  Some days ago I started making a Breakout (or Brick), and I have finished coding the Ball movement, the Pad movement, the collisions between the Pad and the Ball, the Ball and the walls and I have also finished coding that when the Ball is lost it reapears on the Pad.

    I still have to code the bricks, but when I got there I found a problem, which was that I don't know how to draw multiple bricks (using the same texture).  Of course this could be achieved by creating 30 spritebatches and Drawing each one individually, but this would be rather impractical.  I would like to know if there is any way of drawing multiple bricks, using the same texture, in different locations each defined by a Vector2.

    Till now I have (for the Brick):

    Texture2D brick;
    Vector2 brick_location = new Vector2 (X, Y)


    brick = Content.Load<

    Texture2D>("brick");


     

    spriteBatch.Begin(

    SpriteBlendMode.AlphaBlend);

     

    spriteBatch.Draw(brick, brick_location,

    Color.White);

     

    spriteBatch.End();

     

    And then the collision code.

    Thanks for any answer

  • 3/16/2009 8:44 PM In reply to

    Re: How do I draw a texture more than once?

    So, lets assume you got a collection (array,list,etc) for your bricks - called brickslist.

    spriteBatch.Begin();

    foreach (Brick b in brickslist)
       spriteBatch.draw(b.texture, b.position, Color.white);  // where position = Vector2 holding the position and texture = bricks's sprite

    spriteBatch.End();

  • 3/16/2009 10:33 PM In reply to

    Re: How do I draw a texture more than once?

    Ok, but how do I create an Arraylist of Vector2's?  I only know how to store normal numbers in it.
  • 3/16/2009 11:21 PM In reply to

    Re: How do I draw a texture more than once?

    Vector2[] vectorArray = Vector2[size]; // for array
    OR:
    List<Vector2> vectorList = new List<Vector2>(); // for list

    You might want to look into object oriented programming, and then create a class for bricks. But its not necessary for this project though.
  • 3/16/2009 11:51 PM In reply to

    Re: How do I draw a texture more than once?

    Well, object-oriented programming is kind of new to me, but I am trying to implement it at least to create the bricks.  So far my class looks like this:

    class

     

    Brick

     

    {

     

    public Vector2 Position;         --> The vector that will serve as a location for the brick to be drawn.

     

     

    public int Brick_Number;         --> Just in case I need to identify a brick.

     

     

    public bool Visible;                --> Whether the game will draw the brick or not (after the brick has been hit).

     

     

    public string Name;               --> The brick's name (brick1, brick2, etc).

     

    }

    I have created a loop to create 15 bricks (3x5):

    But I dont know how to create bricks that have a number added at the end of their name..

     

    for

     

    (n = 1; n < 15; n++)

     

    {

    Brick ?

    }

  • 3/17/2009 12:57 AM In reply to

    Re: How do I draw a texture more than once?

    Just as these guys have said. Create a loop with a constructor and add each item you make to an arrayList. To keep them from all having the same position, create a new vector2 or rectangle and use the loop counter as the modifier so it will create a different value each time.

    for(int i = 0; i < blockArray.Count; i++)
    {
        brick a = new brick(.....new Vector2(i * brick.Width, 20));
        blockArray.add(a);
    }
  • 3/17/2009 1:18 AM In reply to

    Re: How do I draw a texture more than once?

    CarlosNYM:
    Just as these guys have said. Create a loop with a constructor and add each item you make to an arrayList. To keep them from all having the same position, create a new vector2 or rectangle and use the loop counter as the modifier so it will create a different value each time.

    for(int i = 0; i < blockArray.Count; i++)
    {
        brick a = new brick(.....new Vector2(i * brick.Width, 20));
        blockArray.add(a);
    }


    Well, this is a good idea, but what I was looking for was something similar:

    1). A for loop, that cycles 15 times (as there are 3 rows and 5 colmuns of bricks, so 15 locations have to be defined)

    2). In each loop, a new Brick is created.  What I consider important here is that if I create a loop for exaple with Brick A = new Brick, wouldn't I be creating a Brick A

    15 times? or does C# know that I am creating various Bricks?

    3).  The vector2 for each Brick is defined by another code that asigns them a X and Y so that 3 rows and 5 columns are created.

    4). Draw everything.

    5). What does "new brick(.....)" do? The class brick doesnt have any parameters, so I get and error when I try to use it.
  • 3/17/2009 8:24 AM In reply to

    Re: How do I draw a texture more than once?

    Fede22:


    1). A for loop, that cycles 15 times (as there are 3 rows and 5 colmuns of bricks, so 15 locations have to be defined)

    2). In each loop, a new Brick is created.  What I consider important here is that if I create a loop for exaple with Brick A = new Brick, wouldn't I be creating a Brick A

    15 times? or does C# know that I am creating various Bricks?

    3).  The vector2 for each Brick is defined by another code that asigns them a X and Y so that 3 rows and 5 columns are created.

    4). Draw everything.

    5). What does "new brick(.....)" do? The class brick doesnt have any parameters, so I get and error when I try to use it.



    First, your question number five. For this you need to create a constructor for your brick-class. In the class, make a method like:

    public Brick(Vector2 pos) // add here all the arguments you want to give to the constructor, separated with comma
    {
         this.position = pos; // "this" refers to this instance of brick, can only be used inside the class
         // all the other initialization here
    }

    Then, to create a new brick:
    Brick something = new Brick(position,other parameters you added);

    Your question number 2: it all depends what variables you assign these bricks to, if you have Brick A = new Brick(), and then again the same, the A will no longer refer to the old brick, and the old brick will be removed from the memory by carbage collector as nothing points at it anymore. So you must have array or a list to store pointers to your bricks. Dont use Brick A = new Brick() in your loop (only the last one could be used, others are discarded), but brickArray[i] = new Brick() OR brickList.add(new Brick()); instead.

  • 3/18/2009 1:11 AM In reply to

    Re: How do I draw a texture more than once?

    Yes! I did it.  I created the Brick class, declared a Brick in the loop, saved the Brick created in a list, with its properties (position, brick number and if its visible or not)

    Then, I created a foreach loop, and all 15 bricks were drawn.  The spritebatch code is protected by an if, which checks if the brick's visible property is true, and if it false then it skips the code so it doesnt draw the brick.

    All this I was able to do with your help, so I appreciate it very much!

    BUT

    I still have to code the collision between the ball and the brick... so far the ball collides with the screen edges and the pad.  I will try to create a collision method that when the ball collides with a brick, its visible property is change to false, si it isn't drawn anymore.  The good thing about the objects is that now I don't have to create a method that makes the Brick dissapear, but just change its visible property to false.  Any ideas?

    The collision between the Pad and the ball:

    It is in spanish so:

    Pelota = Ball
    Lugar = Place
    Paleta = Pad
    Forma = Shape = the texture itself

    float

     

    x1 = pelotalugar.X;

     

     

    float y1 = pelotalugar.Y;

     

     

    float h1 = pelotaforma.Height;

     

     

    float w1 = pelotaforma.Width;

     

     

    float x2 = paletalugar.X;

     

     

    float y2 = paletalugar.Y;

     

     

    float h2 = paletaforma.Height;

     

     

    float w2 = paletaforma.Width;

     

     

    if (((x2 > x1 && x2 < x1 + w1) || (x1 > x2 && x1 < x2 + w2)) && ((y2 > y1 && y2 < y1 + h1) || (y1 > y2 && y1 < y2 + h2)))  //omg iz complicated

     

    {

    pelotavelocidad.Y *= -1;  // changes the ball's course

    }



  • 3/18/2009 12:04 PM In reply to

    Re: How do I draw a texture more than once?

    Why do you want to have invisible tiles? :D Just remove them when the ball hits, so you dont have to care about their visibility, ever. Just their existance.

    As for the collision detection, basic rectangle collision should do it (if (ball.position.x > brick.position.x && ball.position.x < brick.position.x + brick.width && ball.position.y > brick.position.y && ball.position.y < brick.position.y + brick.height)). You can also use rectangle-class that contains built-in method checking this (Rectangle.Contains()) or something.

    These only check if ball's center is inside a brick, so if the ball is of large size, you want to make the collidable rectangle a bit bigger than the actual brick.
Page 1 of 1 (10 items) Previous Next