나는 페인트를 사용하여 미로를 그렸던 아주 간단한 프로그램을 만들었습니다. (fillRect
메서드는 미로의 벽을 만드는 데 사용되었습니다) keyListener
을 사용하여 이동 한 스프라이트를 만들었습니다. 나는 스프레이가 미로의 벽을 통과하지 못하도록 단순한 (통근 과학의 첫해에 있었던 것처럼) 충돌 감지를 구현하고 싶다. 미로는 거의 400 줄의 코드로 그려지기 때문에 포함하지 않을 것입니다.간단한 충돌 감지 만들기
import java.awt.*;
import java.applet.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class IndeProj extends Applet implements KeyListener {
//KEY LISTENER INFO FOR MY SPRITE
public int x = 10;
public int y = 575;
public boolean keyUp;
public boolean keyDown;
public boolean keyLeft;
public boolean keyRight;
public void paint(Graphics g) {
//drawMaze
drawMazeHorizontalLines(g);
drawMazeVerticalLines(g);
//SPRITE STUFF
addKeyListener(this);
this.MoveRect(g,x,y);
}
public void drawMazeHorizontalLines(Graphics g)
{
//This method draws the horizontal lines of the maze using the method `fillRect(x,y,w,h)`
}
public void drawMazeVerticalLines (Graphics g)
{
//This method draws the vertical lines of the maze using `fillRect(x,y,w,h)`
}
public void MoveRect(Graphics g, int x, int y) //Draws Sprite
{
g.setColor(Color.green);
g.fillRect(x,y,20,20);
g.setColor(Color.yellow); //Sprite body
g.fillRect(x,y,20,20);
g.setColor(Color.green); //Sprite eyes
g.fillRect(x,y,7,7);
g.fillRect((x+13),y,7,7);
g.setColor(Color.blue); //Sprite pants
g.fillRect(x,(y+13),20,7);
g.setColor(Color.black); //Sprite mouth
g.fillRect((x+6),(y+9),8,2);
}
public void keyPressed(KeyEvent e) //Moves Sprite
{
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
y+=1;
y+=0;
} if (e.getKeyCode() == KeyEvent.VK_UP) {
y-=1;
y-=0;
} if (e.getKeyCode() == KeyEvent.VK_LEFT) {
x-=1;
x-=0;
} if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
x+=1;
x+=0;
}
repaint();
}
public void keyReleased(KeyEvent e) //Stops Sprite
{
keyUp = keyDown = keyLeft = keyRight = false;
}
}
는 나는 (x
및 y
좌표 사용) 벽을 칠 때 스프라이트가 정지되도록 스프라이트 이동 중지를 만들고 싶어.