Hello all,
I am writing a simple game and so far I have 2 files, a Main file and a Dog file <-- please do not ask why dog, it was the first thing that came to my head.
anyway, here are the files:
Main Class
package Gamer; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Main extends JFrame{ private Image dbImage; private Graphics dbg; static Dog d1 = new Dog(200,200); static Dog d2 = new Dog(50,50); public Main(){ setSize(600,600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); addKeyListener(new AL()); } @Override public void paint(Graphics g){ dbImage = createImage(getWidth(), getHeight()); dbg = dbImage.getGraphics(); paintComponent(dbg); g.drawImage(dbImage, 0, 0, this); } public void paintComponent(Graphics g){ d1.Draw(g); g.drawImage(d1.getImage(), d1.getX(), d1.getY(), null); g.drawImage(d2.getImage(), d2.getX(), d2.getY(), null); repaint(); } public class AL extends KeyAdapter { @Override public void keyPressed(KeyEvent e){ d1.keyPressed(e); // d2.keyPressed(e); } @Override public void keyReleased(KeyEvent e){ d1.keyReleased(e); // d2.keyReleased(e); } } public static void main(String[] args) { Main shoot = new Main(); Thread d1t = new Thread(d1); d1t.start(); Thread d2t = new Thread(d2); d2t.start(); } }
Dog class
package Gamer; import java.awt.*; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; public class Dog implements Runnable{ int x, y, xDir, yDir, itX,itY; Image im; boolean MoveIt = false; public Dog(int xv, int yv){ im = new ImageIcon("C:\\OtherStuff\\enemy.png").getImage(); x = xv; y = yv; itX = 500; itY = 300; } public Rectangle getBounds() { return new Rectangle(x,y,40,40); } public void Draw(Graphics g) { g.setColor(Color.BLUE); g.fillRect(120, 120, 40, 10); g.fillRect(160, 120, 4, 7); g.drawImage(getImage(), getX(), getY(), null); } public Image getImage() { return im; } public int getX() { return x; } public int getY() { return y; } public void move(){ x += xDir; y += yDir; if(x <= 0){ x = 0; } if(x >= 550) { x = 550; } if(y <= 0){ y = 0; } if(y >= 550) { y = 550; } } public void setXDirection(int xdir){ xDir = xdir; } public void setYDirection(int ydir){ yDir = ydir; } public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode(); if(keyCode == e.VK_LEFT){ setXDirection(-1); } if(keyCode == e.VK_RIGHT){ setXDirection(1); } if(keyCode == e.VK_UP){ setYDirection(-1); } if(keyCode == e.VK_DOWN){ setYDirection(1); } if(keyCode == e.VK_SPACE){ MoveIt = true; } } public void keyReleased(KeyEvent e){ int keyCode = e.getKeyCode(); if(keyCode == e.VK_LEFT){ setXDirection(0); } if(keyCode == e.VK_RIGHT){ setXDirection(0); } if(keyCode == e.VK_UP){ setYDirection(0); } if(keyCode == e.VK_DOWN){ setYDirection(0); } } public void goTo() { if (x < itX) { setXDirection(1); } else if (x > itX) { setXDirection(-1); } else { setXDirection(0); } if (y < itY) { setYDirection(1); } else if (y > itY) { setYDirection(-1); } else { setYDirection(0); } if((x == itX) && (y == itY)) {MoveIt = false; } } @Override public void run() { try{ while(true){ move(); if(MoveIt) { goTo();} Thread.sleep(5); } }catch(Exception e){System.err.println(e.getMessage());} } }
Please note that the commented out parts are/were just random tests I was doing to see what would happen kind of things..
The problem I have is that I cannot seem to figure out how to put in a collision detection between 2 dog objects? where would I put it?
I was gonne use the if(d1.intersects(d2))... but I cannot put it into the dog class as d1 and d2 are not objects in that class, and the main class I did not put a while loop so I am a bit lost as to where it should go?
Should I move the move function to the main class?
Thank you for any help
Bob