Multiple Instances (i.e. bullets)

Got some cool game ideas? Let us know about them and we'll do our best to help you create your game with the SwinGame SDK.

Multiple Instances (i.e. bullets)

Postby FlyingPanda on Mon Jun 28, 2010 7:18 pm

Ok. So I'm planning on making a danmaku-style game (original, aren't I...), but first I need to learn the basics of SwinGame on Visual Basic 2008.

My question is, as there will be many, many bullets flying around at the same time, how do I make each one a separate instance, check collisions for each, move each, and draw each individually (arrays, I would presume, but could someone lend me some exact code)?

The "Empty Your Mind" tutorial seems to have the answer..... but in Pascal.... :|
So, can anyone help me here? I just need a few lines of VB code for initializing each bullet, and then drawing,checking, or moving each, and I'll pick up the rest from there.


Thanks :)
FlyingPanda
Apprentice
 
Posts: 1
Joined: Mon Jun 28, 2010 7:06 pm

Re: Multiple Instances (i.e. bullets)

Postby Draps on Wed Jun 30, 2010 5:34 am

hey dude
yeh im having the same problem i have what seems to be what would work but it doesn't

i have had aacain look over it and he believes it should work. instead i get no errors and nothing happens

sigh if you find anything let me know via the forums or hkdraps@hotmail.com

and as to how i did it i did it via lists in the System.Collections.Generic
Draps.
Draps
Journeyman
 
Posts: 29
Joined: Tue May 04, 2010 6:32 pm

Re: Multiple Instances (i.e. bullets)

Postby mitch on Wed Jun 30, 2010 12:54 pm

My question is, as there will be many, many bullets flying around at the same time, how do I make each one a separate instance, check collisions for each, move each, and draw each individually (arrays, I would presume, but could someone lend me some exact code)?


The way that I would solve your problem is as follows:

Firstly, you need create a class that contains bullet information, position, size, etc. And then have one copy of the image shared between them (IE: Use a static image that's allocated while it's loading). Using a static variable will save memory in the long run.
For example (I use C#, but the concept should be easy to see):
Code: Select all
class Bullet
{
   public System.Drawing.PointF Position {get;set;} // Center of the bullet.
   public System.Drawing.PointF DrawStart {get;set;} // Drawing start point.
   public System.Drawing.RectangleF Bounds {get;set;}
   public void Move(float x, float y)
   {
      // Update Position and Bounds...
   }
   public static Image BulletImage; // Needs to be set to work.
};

List<Bullet> bullets;


To check whether or not a object is colliding with another object, you can either do a "Rectangle Intersects Check" or do it another say.

Let's say that I have a variable called unit, which has a property called Bounds, and I'm looking at a bullet called bullet.

Code: Select all
if (bullet.Bounds.Intersects(unit.Bounds))
{
   // Intersection.
}


Iterating over the list of bullets will allow you to do individual checks, but it can be time consuming when you have lots of units on the screen (It can be optimized as well).
Drawing is relatively simple, just iterate over the list and draw (You can get the bullets position from DrawStart).

I'll probably update this later on, but it should give you a rough idea of where to start.
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Multiple Instances (i.e. bullets)

Postby Draps on Wed Jun 30, 2010 4:23 pm

Ahh well the way i was doing mine was with sprites but a list of sprite i think i have it right but im now sure

my bullets:
Code: Select all
public class Bullet
    {

        public List<Bullet> Bullets = new List<Bullet>();

        private Sprite _Sprite;

       
        public Bullet(String name, float SpawnX, float SpawnY)
        {
           
            for (int i = 0; i < Bullets.Count; i++)
            {
                Bullets.Add(new Bullet(name, SpawnX, SpawnY));
                Bullets[i]._Sprite = Graphics.CreateSprite(Resources.GameImage(name));
                Bullets[i]._Sprite.X = SpawnX;
                Bullets[i]._Sprite.Y = SpawnY;
           
            }
        }

        public void DrawBullet()
        {
                for (int i = 0; i < Bullets.Count; i++)
                {
                    Graphics.DrawSprite(Bullets[i]._Sprite);

                }
        }



and my calling of them:
Code: Select all
public static Bullet _bullet;



if (Input.IsKeyPressed(SwinGame.Keys.VK_SPACE))
                {
                    _bullet = new Bullet("bullet1", pship.X + 16, pship.Y - 2);

                }


mmm i was going to use sprites.
Draps.
Draps
Journeyman
 
Posts: 29
Joined: Tue May 04, 2010 6:32 pm

Re: Multiple Instances (i.e. bullets)

Postby mitch on Wed Jun 30, 2010 5:39 pm

I can see a few problems with what you have posted there.

Firstly:
Code: Select all
public class Bullet
    {

        public List<Bullet> Bullets = new List<Bullet>();


Is a bit odd, may I recommend that you do the following:
Code: Select all
public List<Bullet> Bullets = new List<Bullet>();
public class Bullet
    {


You then don't need to constantly recreate the Bullet class (Saving a bit of time and memory in the process).

This means that you need to change this line:
Code: Select all
if (Input.IsKeyPressed(SwinGame.Keys.VK_SPACE))
{
    _bullet = new Bullet("bullet1", pship.X + 16, pship.Y - 2);
}


To something like
Code: Select all
if (Input.IsKeyPressed(SwinGame.Keys.VK_SPACE))
{
    Bullets.Add(new Bullet("bullet1", pship.X + 16, pship.Y - 2));
}


And you'd then need to update your drawing code to point to Bullets.

Does this make sense?
Projects
Spawn (Concept) (SGSDK or SDL.NET) - War ("Alpha") (SGSDK) - PD (SDL.NET)
User avatar
mitch
Master
 
Posts: 90
Joined: Thu Mar 13, 2008 12:26 pm
Location: Hamilton, Victoria

Re: Multiple Instances (i.e. bullets)

Postby Draps on Wed Jun 30, 2010 10:55 pm

ahh yeh i think i get what you mean

ill give it a try thanks
Draps.
Draps
Journeyman
 
Posts: 29
Joined: Tue May 04, 2010 6:32 pm

Re: Multiple Instances (i.e. bullets)

Postby Draps on Wed Jun 30, 2010 11:26 pm

damn still having issues. sigh ill have abit more of a play tomorrow
Draps.
Draps
Journeyman
 
Posts: 29
Joined: Tue May 04, 2010 6:32 pm


Return to Game Ideas

Who is online

Users browsing this forum: No registered users and 1 guest

cron