1

I am trying to make a game for a school project. I am trying to detect when two images are 5 pixels apart. I cant seem to find an algorithm to determine what the distance is. Any help is appreciated.

import javax.swing.JFrame;


public class Main extends JFrame {
    Screen screen;

    public Main() {

        setSize(500, 400);
        setResizable(false);
        setTitle("The Adventures of BLANK") ;
        setLocationRelativeTo(null) ;

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();

    }


    public static void main(String[] args) {
        Main main = new Main();
    }

    public void init() {
        screen = new Screen();
        add(screen);
        addKeyListener(screen);
        setVisible(true);
    }
}

The Second Class

public void loadImages() {
    try {
        awesomeFace = ImageIO.read(getClass().getResource("/AwesomeFace.png"));
        Trollface = ImageIO.read(getClass().getResource("/Trollface.png")) ;
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public Screen() {
    loadImages();
    Thread thread = new Thread(this);
    thread.start();
}

public void run() {
    while(running) {
        //Game Loop Start
        if (trolly+trollx > x + y) {
            proximity = trolly+trollx - x + y ;
        } else {
            proximity = x+y - trollx +trolly ;
        }

        if(left) {
            if (proximity <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (x == 0) {

            } else {
                x--;
            }
        }

        if(right) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (x >=450) {

            } else {
                x++;
            }
        }

        if(up) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (y == 0) {
            } else {
            y--;
            }
        }

        if(down) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (y == 325) {
            } else {
            y++;
            }
        }

        if (trollup) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
        if (trolly == 0) {
            } else {
            trolly -- ;
            }
    }
        if (trolldown) {
            if (proximity - x <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (trolly == 325) {
                } else {
                trolly ++ ;
                }
        }
        if (trollleft) {
            if (proximity <= 3) {
                y = 700 ;
                x = 700 ;
            }
            if (trollx == 0) {
                } else {
                trollx -- ;
                }
        }
        if (trollright == true) {
            if (trollx - x <= 3) {
                y = 700 ;
                x = 700 ;
            }if (trollx == 450) {
                } else {
                trollx ++ ;
                }

        }




        //Game Loop End
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    //Graphical loop start
    g.drawImage(awesomeFace, x, y, null);
    g.drawImage(Trollface, trollx, trolly, null) ;


    //Graphical loop end
    repaint();
}

@Override
public void keyPressed(KeyEvent e) {

    if(e.getKeyCode() == 87) {
        trollup = true;
    }

    if(e.getKeyCode() == 65){
        trollleft = true;
    }

    if(e.getKeyCode() == 83) {
        trolldown = true;
    }

    if(e.getKeyCode() == 68) {
        trollright = true;
    }

    if(e.getKeyCode()==37) {
        //Left
        left = true;
    }

    if(e.getKeyCode()==38) {
        //Up
        up = true;
    }

    if(e.getKeyCode()==39) {
        //Right
        right = true;
    }
    if(e.getKeyCode()==40) {
        //Down
        down = true;
    }
}

public void keyReleased(KeyEvent e) {
    if(e.getKeyCode()==37) {
        //Left
        left = false;
    }

    if(e.getKeyCode()==38) {
        //Up
        up = false;
    }

    if(e.getKeyCode()==39) {
        //Right
        right = false;
    }
    if(e.getKeyCode()==40) {
        //Down
        down = false;
    }
    if(e.getKeyCode() == 87) {
        trollup = false;
    }

    if(e.getKeyCode() == 65){
        trollleft = false;
    }

    if(e.getKeyCode() == 83) {
        trolldown = false;
    }

    if(e.getKeyCode() == 68) {
        trollright = false;
    }
}

@Override
public void keyTyped(KeyEvent e) {

}
snickers10m
  • 1,709
  • 12
  • 28
Sumtinlazy
  • 337
  • 5
  • 17
  • 3
    Most users on here will find it difficult and probably not want to answer questions that have a code dump in them. Can you take out code you might think is irrelevant in your question and offer explanations for the remaining code? – snickers10m Apr 27 '15 at 01:10
  • what about distance formula? you'd have to define the "points" on the image (maybe the center works best) but you're in 2d space presumably so – D. Ben Knoble Apr 27 '15 at 01:21
  • @BrianTopping if OP wants, essentially, it to detect when a bounding boxes 5 pixels larger than the images on all sides collide, it's a duplicate. If he wants circular distance (as in real distance, so not 5 pixels up and 5 pixels right from the top right corner, for example), it might be a different question. – snickers10m Apr 27 '15 at 01:32
  • 2
    @snickers10m: This is the problem with not enough information and questions where the OP does not have specific requests. I hold that it's a duplicate, and his professor added a wrinkle that makes it harder to simply use the good answers that are already out there. Add two pixels to the bounding box of the images and use the techniques there such as intersecting the bounding boxes. – Brian Topping Apr 27 '15 at 01:36

0 Answers0