Witam.
Jakiś czas temu zacząłem swoją przygodę z językiem Java. Próbuje napisać mini gierkę polegająca na przejściu labiryntu. Na razie jest to bardzo proste i "upośledzone". Obiekt już mi się porusza po planszy, jednak mam duży problem, z przeszkodami. Mianowicie chciałbym, aby mój obiekt nie przechodził, przez inne obiekty na planszy (czyt. ściany).
Proszę o jakieś podpowiedzi, instrukcje.
package maze;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class mm1 extends Applet implements KeyListener
{
public Point loc;
public Point loc1;
public Rectangle r1;
public Rectangle r2;
public Rectangle r3;
public Rectangle r4;
public Rectangle r5;
public Rectangle r6;
public Rectangle r7;
public Rectangle r8;
public Rectangle r9;
public Rectangle r10;
public Rectangle r11;
public Rectangle r12;
public void init()
{
this.addKeyListener(this);
r1 = new Rectangle(0,0,50,50);
r2 = new Rectangle(200,0,5,300);
r3 = new Rectangle(0,70,100,5);
r4 = new Rectangle(100,150,5,70);
r5 = new Rectangle(0,215,100,5);
r6 = new Rectangle(0,400,200,5);
r7 = new Rectangle(100,285,5,120);
r8 = new Rectangle(300,300,5,600);
r9 = new Rectangle(200,180,200,5);
r10 = new Rectangle(400,300,200,5);
r11 = new Rectangle(330,0,5,120);
r12 = new Rectangle(450,450,50,50);
}
public void paint (Graphics g)
{
setSize(500,500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(r1);
g2.fill(r2);
g2.fill(r3);
g2.fill(r4);
g2.fill(r5);
g2.fill(r6);
g2.fill(r7);
g2.fill(r8);
g2.fill(r9);
g2.fill(r10);
g2.fill(r11);
g2.fill(r12);
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
loc=r1.getLocation();
loc=r2.getLocation();
;
if(loc==loc1)
{
}
else
{
r1.setLocation(r1.x+2, r1.y);
}
}
else if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
r1.setLocation(r1.x-2, r1.y);
}
else if (e.getKeyCode()==KeyEvent.VK_UP)
{
r1.setLocation(r1.x, r1.y-2);
}
else if (e.getKeyCode()==KeyEvent.VK_DOWN)
{
r1.setLocation(r1.x, r1.y+2);
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}