Asteroids Clone(Having trouble making the bullet code)

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.

Asteroids Clone(Having trouble making the bullet code)

Postby sahil885 on Wed Apr 20, 2011 1:11 pm

Hi I'm making an game which is bit of an asteroids clone

I'm having trouble making the bullet code to make it fire from the player ship

Here's my attempt of the bullet code

public class Bullet : Ship
{
private Sprite _BulletSprite;

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


// Bullet Constructor
public Bullet(String name, float SpawnX, float SpawnY)
{
//_BulletSprite = Graphics.CreateSprite(Resources.GameImage("bullet"), 2, 2, 30, 30);

for (int i = 0; i < Bullets.Count; i++)
{
Bullets.Add(new Bullet(name, SpawnX, SpawnY));
Bullets[i]._BulletSprite = Graphics.CreateSprite(Resources.GameImage("bullet"), 2, 2, 50, 50);

Bullets[i]._BulletSprite.X = SpawnX;
Bullets[i]._BulletSprite.Y = SpawnY;
}
}


public void updateBullet()
{
if (Input.IsKeyPressed(SwinGame.Keys.VK_SPACE))
{
Bullets.Add(new Bullet("bullet", _playership.X + 16, _playership.Y - 2));
}
}

public void drawBullet()
{
for (int i = 0; i < Bullets.Count; i++)
{
Graphics.DrawSprite(Bullets[i]._BulletSprite);
//Graphics.DrawSprite(_BulletSprite);
}
}
}
sahil885
Apprentice
 
Posts: 3
Joined: Wed Apr 20, 2011 12:23 pm

Re: Asteroids Clone(Having trouble making the bullet code)

Postby acain on Thu Apr 21, 2011 4:57 pm

By the looks of it your code you have a List of bullets within each Bullet... is that what you want?

I'd suggest you want to have a List of Bullets in your Game, and when the user presses the space bar you create a Bullet and add it to the list. I assume you have a "Game" class of some sorts.

When you create the bullet tell it where it starts, and which way it is travelling (you could use a Vector for this).

Hope that gives you some pointers to start with.
Cheers,

Andrew Cain
----------
Swinburne University of Technology
Lecturer of Software Development in PSD
User avatar
acain
Site Admin
 
Posts: 252
Joined: Wed Dec 05, 2007 2:35 pm
Location: Swinburne University - Hawthorn - Melbourne

Re: Asteroids Clone(Having trouble making the bullet code)

Postby sahil885 on Thu Apr 21, 2011 7:50 pm

Well I just want the player which is the ship to fire a bullet vertical to the enemies
but I haven't really used vectors in the swingame API
The bullet code was from a previous post on this forum
viewtopic.php?f=3&t=1488

I've been trying to solve this problem for a couple days now in the most simple way
Thanks for the tips though
sahil885
Apprentice
 
Posts: 3
Joined: Wed Apr 20, 2011 12:23 pm

Re: Asteroids Clone(Having trouble making the bullet code)

Postby acain on Thu Apr 21, 2011 8:22 pm

Have a look at Mitch's comments on the original code as well. That may also help.
Cheers,

Andrew Cain
----------
Swinburne University of Technology
Lecturer of Software Development in PSD
User avatar
acain
Site Admin
 
Posts: 252
Joined: Wed Dec 05, 2007 2:35 pm
Location: Swinburne University - Hawthorn - Melbourne

Re: Asteroids Clone(Having trouble making the bullet code)

Postby Montycarlo on Mon Apr 25, 2011 3:16 pm

Hi Sahil,

In response to your email, acain is providing you with exactly how I handled the bullets in my 2009 submission.

In short, a list in a 'Manager' class is created and populated with every bullet produced.
On each frame, the array is iterated through and each bullets' onEnterFrame() method is called, which performs the necessary code for each bullet's private properties (The addition of the velocity to the x & y point).
Following that, each instances' draw() method is called such that the new position of the bullet is displayed on the screen.

Hit detection was achieved using pythagoras to approximate the length between the objects and hence, if they were overlapping.

Example Code
Code: Select all
// In Manager class
for each(Bullet i in bulletList){
    i.onEnterFrame()
    i.draw()
    for( int q=0; q<shipList.length; q++){
        if(bulletList[q] == i) continue;
        if( collisionDetector.hitTest(i, shipList[q])){
            shipList[q].explode()
            i.remove()
        }
    }
}
Code: Select all
// In Bullet Class
void public onEnterFrame(){
x += velocity.x
y += velocity.y
}
void public draw(){
.....
}
Code: Select all
// In Collision Detection Class
Boolean public hitTest(gameObj A, gameObj B){
    return Math.pow(A.getX - B.getX, 2) + Math.pow(A.getY - B.getY, 2) < Math.pow(A.getRad, 2) + Math.pow(B.getRad, 2);
}
gameObj is an interface that both the Bullet class & the Ship class implements. It guarantees properties getRad, getX getY in it's instances.
User avatar
Montycarlo
Journeyman
 
Posts: 25
Joined: Wed Jul 01, 2009 9:08 am

Re: Asteroids Clone(Having trouble making the bullet code)

Postby sahil885 on Tue Apr 26, 2011 9:08 pm

Thats a big help man
Thanks Heaps :D :D :D
sahil885
Apprentice
 
Posts: 3
Joined: Wed Apr 20, 2011 12:23 pm


Return to Game Ideas

Who is online

Users browsing this forum: No registered users and 1 guest

cron