Hello!

I need to get some collision detection going in my code but I have no idea where to start!

can anybody find out how to put this in?

the detection will stop drwaing monstors if the bullet hits them

// The "SpaceShooter" class.
//import java.applet.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;
import java.applet.AudioClip;

public class SpaceShooter extends Applet
    implements ActionListener, KeyListener, Runnable
{

    //All the global varibles
    int x1 = 50, y1 = 50, x = 10, dx, y = 10, dy, a = 0, by, bx = 95, mx = 510, my = 20;
    private Image dbImage;
    private Graphics dbg;
    Thread thread, thread2;
    Image img, tile, background, rocketBullet, bullet, credit, sam, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;
    int collide = 0, instruction = 1, counter = 0;

    // Place instance variables here

    public void init ()
    {
        //Imports all the pictures
        img = getImage (getDocumentBase (), "Shoot.gif");
        bullet = getImage (getDocumentBase (), "bullet.gif");
        background = getImage (getDocumentBase (), "space.jpg");
        credit = getImage (getDocumentBase (), "Credits.jpg");
        sam = getImage (getDocumentBase (), "Samus.jpg");
        mon1 = getImage (getDocumentBase (), "mon1.gif");
        mon2 = getImage (getDocumentBase (), "mon2.gif");
        mon3 = getImage (getDocumentBase (), "mon3.gif");
        mon4 = getImage (getDocumentBase (), "mon4.gif");
        mon5 = getImage (getDocumentBase (), "mon5.gif");
        mon6 = getImage (getDocumentBase (), "mon6.gif");
        mon7 = getImage (getDocumentBase (), "mon8.gif");
        addKeyListener (this);

        // Place the body of the initialization method here
    } // init method


    public void actionPerformed (ActionEvent e)  //When user clicks action (below)is preformed
    {

    }


    //Starting here, the following lines of code make my bullet finally loop!
    public void start ()
    {
        thread = new Thread (this);
        thread.start ();
    }


    public void stop ()
    {
        thread.stop ();

    }


    public void run ()
    {
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
        while (true)
        {

            // Delay for 20 milliseconds
            try
            {
                Thread.sleep (2);
            }
            catch (InterruptedException e)
            {
            }

            bx = dx;
            by = dy;

            //If you hit an edge, reverse the direction
            /*if ((x1 + dx < 0) || (x1 + dx > getSize ().width))
            {
                dx = 0;
            }
            if ((y1 + dy < 0) || (y1 + dy > getSize ().height))
            {
                dy = 0;
            }*/

            //Find the new bullet coordinates
            x1 += bx;
            y1 += by;

            //Allows the user to fire another missle
            if (counter == 1)
            {
                x1 = 50;
                y1 = y + 8;
                counter = 0;
            }

            //Collision
            if (x1 == 685 && y1 == 20)
            {
                collide = collide + 1;
            }

            repaint ();

            // reset thread priority
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
        } // while (true)
    }


    public void paint (Graphics g)
    {
        //Draws all the pictures
        if (instruction == 1)
        {
            g.drawImage (credit, 0, 0, 750, 500, this);
        }

        if (instruction != 1)
        {

            if (collide >= 0)
            {

                g.drawImage (background, 0, 0, 750, 500, this);
                g.drawImage (img, x, y, 70, 43, this);

                g.drawImage (sam, mx + 350, my + 150, 100, 100, this);

                g.drawImage (mon1, mx, my, 50, 50, this);
                g.drawImage (mon1, mx + 175, my + 20, 50, 50, this);

                g.drawImage (mon2, mx + 50, my + 210, 50, 50, this);
                g.drawImage (mon2, mx + 175, my + 260, 50, 50, this);

                g.drawImage (mon3, mx + 100, my + 85, 50, 50, this);
                g.drawImage (mon3, mx + 200, my + 185, 50, 50, this);

                g.drawImage (mon4, mx + 25, my + 110, 50, 50, this);
                g.drawImage (mon4, mx + 300, my + 85, 50, 50, this);

                g.drawImage (mon5, mx - 25, my + 210, 50, 50, this);
                g.drawImage (mon5, mx - 50, my - 10, 50, 50, this);

                g.drawImage (mon6, mx - 10, my + 310, 50, 50, this);
                g.drawImage (mon6, mx - 70, my + 60, 50, 50, this);

                g.drawImage (mon7, mx + 10, my + 410, 73, 43, this);
                g.drawImage (mon7, mx + 150, my + 360, 73, 43, this);

                if (collide == 1)
                {

                    g.drawImage (background, 0, 0, 750, 500, this);
                    g.drawImage (img, x, y, 70, 43, this);

                    g.drawImage (sam, mx + 350, my + 150, 100, 100, this);


                    g.drawImage (mon1, mx + 175, my + 20, 50, 50, this);

                    g.drawImage (mon2, mx + 50, my + 210, 50, 50, this);
                    g.drawImage (mon2, mx + 175, my + 260, 50, 50, this);

                    g.drawImage (mon3, mx + 100, my + 85, 50, 50, this);
                    g.drawImage (mon3, mx + 200, my + 185, 50, 50, this);

                    g.drawImage (mon4, mx + 25, my + 110, 50, 50, this);
                    g.drawImage (mon4, mx + 300, my + 85, 50, 50, this);

                    g.drawImage (mon5, mx - 25, my + 210, 50, 50, this);
                    g.drawImage (mon5, mx - 50, my - 10, 50, 50, this);

                    g.drawImage (mon6, mx - 10, my + 310, 50, 50, this);
                    g.drawImage (mon6, mx - 70, my + 60, 50, 50, this);

                    g.drawImage (mon7, mx + 10, my + 410, 73, 43, this);
                    g.drawImage (mon7, mx + 150, my + 360, 73, 43, this);
                }

                //Draws the bullet
                if (triMove == true)
                {
                    g.drawImage (bullet, x1, y1, 40, 10, this);
                }
            }
        }


        //Place the body of the drawing method here
    } //paintmethod


    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();

        if (key == KeyEvent.VK_UP)
        {
            by = -15;
            y = y + by;
        }


        else if (key == KeyEvent.VK_DOWN)
        {
            by = 15;
            y = y + by;
        }


        else if (key == KeyEvent.VK_SPACE)
        {
            counter = counter + 1;
            dx = 2;
            x1 = x1 + dx;
            triMove = true;
        }


        else if (key == KeyEvent.VK_ENTER)
        {
            instruction = instruction + 1;
        }
    }


    public void keyReleased (KeyEvent e)
    {
    }


    public void keyTyped (KeyEvent e)
    {
    }


    public void update (Graphics g)
    {
        // initialize doublebuffers

        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShooter class

Recommended Answers

All 31 Replies

You need to create a small class for your monsters that keeps track of their coordinates and bounding areas. A start would be something similar to this

class Monster {
  int x;
  int y;
  int width;
  int height;
  // of course, a Rectangle could be stored instead and it would save you the math
  // in contains()
  Image image;

  public void draw(Graphics g){
    g.drawImage(image,x,y,width,height,null);
  }

  public boolean contains(int x, int y){
    // does bounding area contain this point?
  }

  public void move(int dx, int dy){
    // move the monster by dx and dy units
  }
}

Keep the monster instances in a List and use them to move, render, and do hit testing.

I'm sorry... I don't understand.... a list? don't forget I have no idea how to even code any of this "collision" I have to do.

Are your monsters etc rectangles? If not, this is quite hard.
Are they Java Shapes (instances of the Shape class) - if so there's a method in Shape that does that.
For rectangles you can easily see if they touch/overlap by comparing their top/bottom coordinates to see if they overlap, then their left/right coordinates ditto. If they overlap both vertically and horizontally then the rectangles overlap.
Draw a few cases out on a sheet of paper and write in the top/bottom/left/right coords for both rectangles - you should be able to see how to compare them to get the result you need.

Unfortunatly, no.... The monters are bothe .jpg and .gif pictures...

Yes, but the pictures are rectangles aren't they?
If the monsters occupy just part of the picture, and you want to test for collisions between the actual monsters, rather than the whole pictures, then that's a pretty difficult, and slow, thing to do.

if by rectangle you mean the background too then no..

I removed the backgrounds to reduce the white rectangles on by space like background

Hi!

Ezzaral, I don't understand what you mean by "bounding"? also will I have to make multiple "monster" classes for each monster? and for the "move" part? what do you mean by that (I suppose you mean the monster moving right?) if so, how will the collision detection stop drawing the "dead" monster?

If anyone else can explain the class example that Ezzaral posted, please do! (explain it like I don't know java so I am guarenteed to understand it)

Sorry for asking for such a begginer explaination!

Thanks!

hey javanoob101,

you have one "monster" class. since you want more than one monster showing on your screen, you instantiate multiple instances of monster class. so, there is one single class, but multiple instances of it.

now, when you do the instantiation, you store all instances on an array. and, inside your class, you could have some boolean variable that is initially true, but turns false once a bullet hits an instance of it. as a result, your machine knows when to print the actual instance of monster and when not.

as I myself am more of a beginner that an experienced programmer, I think I'd share my understanding of solutions offered as beginners are more likely to understand one another than experts :P

Oh! It suddenly makes more sense now! ok I see... so I make one "monster class" but multiples of it right (with different class names eg. class monster 1, class monster2 etc.?) then an array of "int's" for the x and y coords of each monster? then a boolean that starts out as a true stament then if my bullet x and y coords match up with a monster's x and y then the statment becomes false preventing the monster from being drawn?

am I getting it right?

I don't think you fully understand objects. An object is an instance of a class. So, say you have a class named Monster, as stated above. You only ever have that 1 class, but you can create infinite number of objects(instances) of that class.

Monster mon1 = new Monster();
//mon1 is an object(instance) of the Monster class
//you can create any number of objects
Monster mon2 = new Monster();
Monster mon3 = new Monster();
//but don't forget, each object is  a separate entity! which means you can modify each monster independently of another, each object is an [I]instance[/I] of the Monster class

Therefore, if these 3 monsters were drawn on screen, each would have their own unique co-ords etc.

So this would work? and also, I read before that I'll need an array-- what kind of array? 1D,2D? (My class only learned about 1D arrays). and still, what is the "bounding" supposed to mean? (is this the edges of the picture the "boundries"?)

*I modified the following code from before to what I am understanding right now*
Thanks! (in advance)

class mon1 //Instead of "Monster" beside "class" here, I would put "mon1" (I don't think this is right)
{

  int x;  //Monster's [B]own[/B] x coord?
  int y;  //Monster's [B]own[/B] y coord
  int width;
  int height;
  // of course, a Rectangle could be stored instead and it would save you the math
  // in contains()
  Image image;   //What is THIS??

  public void draw(Graphics g){
    g.drawImage(image,x,y,width,height,null);//What is the "null" for?
  }

  public boolean contains(int x, int y){
    // does bounding area contain this point?
  }

  public void move(int dx, int dy){
    // move the monster by dx and dy units
  } //For animation right?
}

1. call the class Monster. Java class names normally start with a capital and describe the kind of thing the class represents. Each instance of this class is a Monster. Please don't call it mon1 - that will confuse everybody.
2. yes, x,y,width,height are the position and size of each Monster. In your case, right now, the widths/heights may all be the same, but they don't need to be.
3. Image is the Image you use to draw this particular instance of a Monster. For the first instance this will probably be mon1.gif (etc)
4. To understand the "null" look at the API documentation for that method.
5. Yes, the move method is for the animation. Call every Monster's move() method from a Timer so every Monster will move (eg) once every 50 mSec. I would make the dx,dy values instance variables of the Monster class, just like its x and y. That way each Monster has its own velocity. In that case there's no need to pass the right dx,dy valkues into each move() method. It's much easier that way.

To do (5) you need a list of all the Monsters. That's where the array comes in. All you need is a simple 1D array of Monsters, so you can loop thru the array calling the move() for each element (Monster) in the array. If you've covered ArrayList or Vector in your course these would be a better solution, if not just make an array of the right size for now - you can improve that when the deadline is less urgent.
The draw(Graphics g) method works the same way - in your main paintComponent method you just loop thru all the Monsters calling their draw methods.

the best way to know if you are getting it right is to implement it, and along the way you'll see what you are doing wrong. what I tried to say is not exactly what you said last, but that does not mean your idea won't work. try it, see if it works, and if does not, come back and tell us what you did, and someone will help.

I agree with bibiki, at least as far as learning the basics of the language and debugging are concerned. The danger is that a beginner will find some program architecture that he can get to work for one particular exercise, but which actually represents a bad practice/habit that will be hard to get out of.
For example

so I make one "monster class" but multiples of it right (with different class names eg. class monster 1, class monster2 etc.?) then an array of "int's" for the x and y coords of each monster?

Now that will work, but it's a really bad direction to head in.
We have some responsibility to make suggestions like Ezzaral's about using classes properly - that way the OP will get the maximum value.

so where would I put this in my program? before the graphics class?

Well...

My teacher has told me not to do a class simply because I don't have enough time to make it work in my code before I have to hand my ISU in.....

But! any other suggestions will be appreciated!

Thank-you.

And does your teacher have better suggestions for collision detection?

No she does not have a soulution...
but it doesn't matter... I patrially got the code going!
my classmate gave me a snippit of his collision detection so I played around with the values and Tada! it kinda works.

The problem is if you shoot the bullet across the screen above any of the monsters (eg. If you shoot at the very top of the screen) the monsters all dissapear! Even if the bullet doesn't hit anything! I've tried to fix this but I've had no luck...

Perhaps sombody else will?

*Note: If you scroll down, you will see a comment saying "Collision detection"
This is my collision detection

"ranx" is a random "x" value for the monsters (there are 7 of them eg. ranx1, ranx2)
"rany" is a random "y" value for the monsters (again ther are 7 of them eg. rany1, rany2)

"x1" is the bullet's "x" coord
"y1" is the bullet's "y" coord

Here is the new code

// The "SpaceShooter" class.
//import java.applet.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;
import java.applet.AudioClip;

public class SpaceShooter extends Applet
    implements ActionListener, KeyListener, Runnable
{

    //All the global varibles
    int x1 = 50, y1 = 50, x = 10, dx, y = 10, dy, by, bx = 95, mx = 510, instruction = 1, counter = 0;
    int rany, rany2, rany3, rany4, rany5, rany6, rany7;
    int ranx, ranx2, ranx3, ranx4, ranx5, ranx6, ranx7;
    double randy, randy2, randy3, randy4, randy5, randy6, randy7;
    double randx, randx2, randx3, randx4, randx5, randx6, randx7;
    private Image dbImage;
    private Graphics dbg;
    Thread thread, thread2;
    Image img, tile, background, rocketBullet, bullet, credit, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
    Graphics backg;
    boolean triMove = false;

    // Place instance variables here

    public void init ()
    {
        randy = Math.random () * 650 + 1;
        rany = (int) Math.rint (randy);

        randy2 = Math.random () * 650 + 1;
        rany2 = (int) Math.rint (randy2);

        randy3 = Math.random () * 650 + 1;
        rany3 = (int) Math.rint (randy3);

        randy4 = Math.random () * 650 + 1;
        rany4 = (int) Math.rint (randy4);

        randy5 = Math.random () * 650 + 1;
        rany5 = (int) Math.rint (randy5);

        randy6 = Math.random () * 650 + 1;
        rany6 = (int) Math.rint (randy6);

        randy7 = Math.random () * 650 + 1;
        rany7 = (int) Math.rint (randy7);


        //Random "X" value for monsters
        randx = Math.random () * 500 + 1;
        ranx = (int) Math.rint (randx);

        randx2 = Math.random () * 500 + 1;
        ranx2 = (int) Math.rint (randx2);

        randx3 = Math.random () * 500 + 1;
        ranx3 = (int) Math.rint (randx3);

        randx4 = Math.random () * 500 + 1;
        ranx4 = (int) Math.rint (randx4);

        randx5 = Math.random () * 500 + 1;
        ranx5 = (int) Math.rint (randx5);

        randx6 = Math.random () * 500 + 1;
        ranx6 = (int) Math.rint (randx6);

        randx7 = Math.random () * 500 + 1;
        ranx7 = (int) Math.rint (randx7);

        //Imports all the pictures
        img = getImage (getDocumentBase (), "Shoot.gif");
        bullet = getImage (getDocumentBase (), "bullet.gif");
        background = getImage (getDocumentBase (), "space.jpg");
        credit = getImage (getDocumentBase (), "Credits.jpg");
        mon1 = getImage (getDocumentBase (), "mon1.gif");
        mon2 = getImage (getDocumentBase (), "mon2.gif");
        mon3 = getImage (getDocumentBase (), "mon3.gif");
        mon4 = getImage (getDocumentBase (), "mon4.gif");
        mon5 = getImage (getDocumentBase (), "mon5.gif");
        mon6 = getImage (getDocumentBase (), "mon6.gif");
        mon7 = getImage (getDocumentBase (), "mon8.gif");
        addKeyListener (this);

        // Place the body of the initialization method here
    } // init method



    public void actionPerformed (ActionEvent e)  //When user clicks action (below)is preformed
    {

    }


    //Starting here, the following lines of code make my bullet finally loop!
    public void start ()
    {
        thread = new Thread (this);
        thread.start ();
    }


    public void stop ()
    {
        thread.stop ();

    }


    public void run ()
    {
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
        while (true)
        {

            // Delay for 20 milliseconds
            try
            {
                Thread.sleep (2);
            }
            catch (InterruptedException e)
            {
            }

            bx = dx;
            by = dy;

            //Find the new bullet coordinates
            x1 += bx;
            y1 += by;


            //Allows the user to fire another missle
            if (counter == 1)
            {
                x1 = 50;
                y1 = y + 8;
                counter = 0;
            }

            //Collision Detection
            if (x1 >= ranx && rany + 50 > y1)
            {
                ranx += 5000;
            }

            if (x1 >= ranx2 && rany2 + 50 > y1)
            {
                ranx2 += 5000;
            }

            if (x1 >= ranx3 && rany3 + 50 > y1)
            {
                ranx3 += 5000;
            }

            if (x1 >= ranx4 && rany4 + 50 > y1)
            {
                ranx4 += 5000;
            }

            if (x1 >= ranx5 && rany5 + 50 > y1)
            {
                ranx5 += 5000;
            }

            if (x1 >= ranx6 && rany6 + 50 > y1)
            {
                ranx6 += 5000;
            }

            if (x1 >= ranx7 && rany7 + 50 > y1)
            {
                ranx7 += 5000;
            }

            repaint ();

            // reset thread priority
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
        } // while (true)
    }


    public void paint (Graphics g)
    {

        //Draws all the pictures
        if (instruction == 1)
        {
            g.drawImage (credit, 0, 0, 800, 700, this);
        }

        if (instruction != 1)
        {


            g.drawImage (background, 0, 0, 800, 700, this);

            g.drawImage (img, x, y, 70, 43, this);

            g.setColor (Color.red);
            g.drawString ("" + x1, 10, 10);

            g.drawImage (mon1, ranx, rany, 50, 50, this);

            g.drawImage (mon2, ranx2, rany2, 50, 50, this);

            g.drawImage (mon3, ranx3, rany3, 50, 50, this);

            g.drawImage (mon4, ranx4, rany4, 50, 50, this);

            g.drawImage (mon5, ranx5, rany5, 50, 50, this);

            g.drawImage (mon6, ranx6, rany6, 50, 50, this);

            g.drawImage (mon7, ranx7, rany7, 73, 43, this);

            //Draws the bullet
            if (triMove == true)
            {
                g.drawImage (bullet, x1, y1, 40, 10, this);
            }
        }


        //Place the body of the drawing method here
    } //paintmethod


    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();

        if (key == KeyEvent.VK_UP)
        {
            by = -15;
            y = y + by;
        }


        else if (key == KeyEvent.VK_DOWN)
        {
            by = 15;
            y = y + by;
        }


        else if (key == KeyEvent.VK_SPACE)
        {
            counter = counter + 1;
            dx = 2;
            bx += 10;
            x1 = x1 + dx;
            triMove = true;
            repaint ();
        }


        else if (key == KeyEvent.VK_ENTER)
        {
            instruction = instruction + 1;
        }
    }


    public void keyReleased (KeyEvent e)
    {
    }


    public void keyTyped (KeyEvent e)
    {
    }


    public void update (Graphics g)
    {
        // initialize doublebuffers

        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // SpaceShooter class

Also...

why is my bullet animated so slowly in my web browser?

Is there any way of speeding it up?

yes there is. there must be somewhere in your code a part that updates bullet's position by increasing/decreasing the values of bullet's x and y positions. if you increase the number by which x and y positions increase, your bullet appears to move faster in your screen.

ok... That solves my speed problem but what about my coliision detection problem when you shoot above the monsters. (They "die" like they should if they are hit!)

there must be some if statement that checks whether bullet border's (x and y positions) equal those of a monster, right? now, try adding/subtracting a few pixel's in that statement like:

if (bullet_border + 3 pixels == monster border){monster disappear}

if that does not work, try subtracting 3 pixels :)

hmmm... Could there be issues with my monster's random X and Y preventing the collison detection form ever being true? (as in the bullet's "Y" coord has to equal that of a monsters exact "Y" coord?)

WAIT!!! IT SEEMS TO HALF WORK!!! (but the bullet's y coord has to be 3 pixles away for it to work.)

how would I make it work if it's about 3 pixles away (you know... the bullet's y coord is give or take 3 pixles away)

How would I do This?

(Thanks bibiki for the help so far!!!)

I DID IT!!!!!!!!!!!!!!!!!!!!!!


I GOT THE COLLISION WORKING!!!!

THANK-YOU TO EVEYONE WHO POSTED!!

BUT! I'm not done In this thread!

I still need to make a collision detection for the player if the monster (When I get it animated) touches the player you lose a life!

I'll keep watch over this thread until I get this going!

Thank-you to everyone!

P.S Instead of making yet another thread for animating the monsters (possibly useing a timer) I may post the aniamtion questions in this thread..

If anyone thinks that I should start a new thread for the animation please tell me!

(I don't want to get possibly banned for that!)

OK! new problem!

if my y coord of my bullet is equal to the monster' y value then it id dead right?

but if I tell the if statment to see if the bullet's x value is the exact same as my monster's x value, the monster still dies. even if the x value's are not the same!

here is the code:

I am sorry for the long if statment!
if you know of a easier (and shorter way) please tell me!

"x1" is my bullet's "x" coord

"ranx" is my monster's "x" value

"rany" is my monster's "y" value

if (x1 == ranx && y1 == rany - 5 || y1 == rany - 4 || y1 == rany - 3 || y1 == rany - 2 || y1 == rany - 1 || y1 == rany || y1 == rany + 1 || y1 == rany + 2 || y1 == rany + 3 || y1 == rany + 4 || y1 == rany + 5)
            {
                ranx += 5000;
            }

Well...I have finished my ISU (SpaceShooter) to the point in which it is good enough to hand in!

I would like to thank everyone who posted in this thread. This program would not have worked out if it wasn't for people who devote some time in their lives for others with coding and computer problems. I would also like to thank sirlink99.(He told me I should make an account here and I did and because of that, my ISU is done! Thanks!)

Thank-you to everyone!

*Note: I'm not totally done with this program... I'm going to work on it whenever I have extra time so keep an eye out for more threads! (I'm bound to have problems!)
Also the next time I start a thread, any suggestions toward making my game better are welcome!

so many ors instead try using ands

if (x1 + length of bullet >= ranx && y1 + bullet height / 2 >= rany && y1 + height of bullet / 2 <= rany + height of monster){
ranx += 5000; 
}

you check if the x of the bullet and the x of the monster collide and then you check if the point of the bullet is in between the y and the y + monsters height and if it is then the monster dies

so many ors instead try using ands

if (x1 + length of bullet >= ranx && y1 + bullet height / 2 >= rany && y1 + height of bullet / 2 <= rany + height of monster){
ranx += 5000; 
}

you check if the x of the bullet and the x of the monster collide and then you check if the point of the bullet is in between the y and the y + monsters height and if it is then the monster dies

It's not a very efficient way of doing it at all. I would advise the OP to continue working on this in his spare time, tweaking it and finding new ways to achieve things much more quickly and elegantly. Just because you hand the assignment in, doesn't mean you just stop work on it.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.