Link to home
Start Free TrialLog in
Avatar of Arkajyoti Pal
Arkajyoti Pal

asked on

I am creating a Java game where you fly a plane and save it from colliding with any obstacles, but I am facing problem with creating the Collisions and need assistance.

I am creating a Java game where you need to fly your plane, and save it from colliding with any obstacles. I have been able to do everything successfully, however the only problem I am facing is with creating Collisions.
Note: The compiler says 'Cannot find symbol' in class 'Bird' on lines 32-33.

//Fly Your Jet
import java.applet.*;
import java.awt.*;
public class Fly_Your_Jet_main extends Applet implements Runnable
{
    Thread thread = new Thread(this);
    boolean running = true;
    Player p;
    Bird b, b2, b3;
    Image dbImage;
    Graphics dbg;
    public void init()
    {
        setSize(1000,500);
        setBackground(Color.CYAN);
        p = new Player();
        b = new Bird(this);
        b2 = new Bird(900, 200, this);
        b3 = new Bird(100, 400, this);
    }
    public void start(){thread.start();}
    public void destroy(){running = false;}
    public void stop(){running = false;}
    public void run()
    {
        while(running)
        {
            repaint();
            p.update(this);
            b.update(this,p);
            b2.update(this,p);
            b3.update(this,p);
            try
            {
                Thread.sleep(20);
            }
            catch(InterruptedException e)
            {
                System.out.println("ERROR HAS OCCURRED");
            }
        }
    }
    public void update(Graphics g)
    {
        dbImage = createImage(1000, 500);
        dbg = dbImage.getGraphics();
        paint(dbg);
        g.drawImage(dbImage, 0, 0, this);
    }
    public void paint(Graphics g)
    {
        /*showStatus("Click on the ball and then press arrow keys to start playing!");
        g.setColor(Color.magenta);
        g.drawString("SAVE YOUR BALL!",170,15);*/
        p.paint(g);
        b.paint(g, this);
        b2.paint(g, this);
        b3.paint(g, this);
    }
}

Open in new window

//Fly Your Jet(Cont.)
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Player implements KeyListener
{
    private int x1 = 450, y1 = 300, x2 = 415, y2 = 325, x3 = 435, y3 = 370, velX = 0, velY = 0;
    public void update(Fly_Your_Jet_main mc)
    {
        mc.addKeyListener(this);
        x1+=velX;
        y1+=velY;
        x2+=velX;
        y2+=velY;
        x3+=velX;
        y3+=velY;
    }
    public void paint(Graphics g)
    {
        //JET
        //JET BODY
        g.setColor(Color.PINK);
        g.fillOval(x1,y1,10,90);
        //JET WINGS
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(x2,y2,80,20);
        g.fillRect(x3,y3,40,10);
    }
    public void keyPressed(KeyEvent e)
    {
        switch(e.getKeyCode())
        {
            case KeyEvent.VK_RIGHT:
            {
                velX = 10;
                break;
            }
            case KeyEvent.VK_LEFT:
            {
                velX = -10;
                break;
            }
            case KeyEvent.VK_UP:
            {
                velY = -10;
                break;
            }
            case KeyEvent.VK_DOWN:
            {
                velY = 10;
                break;
            }
        }
    }
    public void keyReleased(KeyEvent arg0)
    {
        
    }
    public void keyTyped(KeyEvent arg0)
    {
        
    }
    public int getX()
    {
        return x1;
    }
    public int getY()
    {
        return y1;
    }
}

Open in new window


//Fly_Your_Jet(Birds)
import java.awt.*;
import java.net.URL;
public class Bird
{
    private int x = 5, y = 10,speed=10;
    private URL url;
    private Image bird;
    public Bird(Fly_Your_Jet_main mc)
    {
        url = mc.getDocumentBase();
        bird = mc.getImage(url, "Bird.jpg");
    }
    public Bird(int i, int j, Fly_Your_Jet_main mc)
    {
        url = mc.getDocumentBase();
        bird = mc.getImage(url, "Bird.jpg");
        x = i;
        y = j;
    }
    public void update(Fly_Your_Jet_main mc, Player p)
    {
        x+=speed;
        if(x<=0)
        speed = 10;
        else if(x>=mc.getWidth()-50)
        speed = -10;
        Collision(p);
    }
    private void Collision(Player p)
    {
        int PX = p.getX;//compiler says 'cannot find symbol'
        int PY = p.getY;//compiler says 'cannot find symbol'
        if(PX-32 <= x && PX+32 >= x)
        {
            System.out.println("Collision Detected!!!");
        }
    }
    public void paint(Graphics g, Fly_Your_Jet_main mc)
    {
        g.drawImage(bird,x,y,mc);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arkajyoti Pal
Arkajyoti Pal

ASKER

@CEHJ As per your suggestion, I tried putting 'pX' instead of 'PX', however, it did not resolve my problem.
That was not the most important part of my comment. Have another look
Thanks a lot for resolving my problem! It was really a silly mistake on my part. I didn't notice the brackets in getX().
:)