1

ペイントを使用して迷路を描く非常に単純なプログラムを作成し (この方法fillRectは迷路の壁を作成するために使用されました)、 を使用して移動するスプライトを作成しましたkeyListener。スプライトが迷路の壁を通り抜けないようにするために、単純な (通勤科学の 1 年目なので) 衝突検出を実装したいと考えています。迷路は約 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座標を使用)、スプライトは動きを止めます。

4

3 に答える 3

1

これは衝突検出の簡単な方法です。マップは int のグリッドであり、各 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;
}
于 2013-03-15T02:26:13.180 に答える
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.
    }
}

それは、境界内に入ることができない長方形のボックスになります。

于 2013-03-15T02:41:52.880 に答える
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;
}

ただし、この方法は少し「ハッキー」に見えます。実装するより良い方法があると確信していますが、これは可能な迅速な解決策です。

于 2013-03-15T02:47:29.797 に答える