2013-03-15 2 views
1

나는 페인트를 사용하여 미로를 그렸던 아주 간단한 프로그램을 만들었습니다. (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; 
    } 
} 

는 나는 (xy 좌표 사용) 벽을 칠 때 스프라이트가 정지되도록 스프라이트 이동 중지를 만들고 싶어.

답변

1

여기에 간단한 충돌 감지 방법이 있습니다.지도에는 각 int에 각각 벽이 포함되어 있습니다. 이렇게하면 잘못된 일방 통행 벽이 생깁니다.

/* could use enums for this */ 
public static int WALL_LEFT = 1; 
public static int WALL_RIGHT = 2; 
public static int WALL_TOP = 4; 
public static int WALL_BOTTOM = 8; 


public int[][] createSimpleMap(){ 
    int[][] map = new int[2][2]; 
    map[0][0] = WALL_LEFT | WALL_RIGHT | WALL_TOP; 
    map[0][1] = WALL_LEFT | WALL_RIGHT | WALL_TOP; 
    map[1][0] = WALL_LEFT | WALL_BOTTOM; 
    map[1][1] = WALL_RIGHT | WALL_BOTTOM; 
    return map; 
} 

충돌 감지를 수행하면 벽이 있는지 감지 할 수 있습니다.

public boolean canMoveUp(x,y){ 
    return (this.map[x][y] & WALL_TOP) ==0; 
} 
0

나는 직사각형의 충돌 상자를 사용합니다. playerX와 playerY는 플레이어의 좌표입니다.

while(true) { 
    while(!((playerX > 50 && playerX < 100) && (playerY > 50 && playerY < 100))){ 
     //Put code to let they players walk here, and boundaries will be enforced. 
    } 
} 

이것은 경계선 내에서 이동할 수없는 직사각형 상자입니다. 애플릿 이중 버퍼링 경우 그들이 이동하기 전에

0

는, 당신은 다음과 같은 작업을 수행 할 수

Color c = new Color(buffer.getRGB(desiredPlayerX, desiredPlayerY)); 
if(c.equals(<Whatever color you used for the maze walls>)){ 
    // Don't allow the movement of the player 
}else{ 
    x = desiredPlayerX; 
    y = desiredPlayerY; 
} 

이 방법은 비록 작은 "해키"보인다, 내가 구현하는 더 나은 방법이 확신 그러나 이것은 가능한 빠른 해결책입니다.