1
votes

I've created a very simple program where I drew a maze using paint (the method fillRect was used to create the walls of the maze), and I've created a sprite that is moved using keyListener. I want to implement a simple (as I'm in my first year of commuter science) collision detection to keep the sprite from moving through the walls of the maze. Because the maze is drawn in almost 400 lines of code, I won't include it.

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;
    }
}

I want to make it so that the sprite stops when it hits a wall (using x and y coordinates), the sprite will stop moving.

3

3 Answers

1
votes

here is a simple way of collision detection, the map is a grid of ints each int has the wall it contains. If you do this wrong you will get magical one way walls.

/* 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;
}

to do the collision detection just detect if the wall is present.

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

If your applet is double buffered, then you can do the following before they move:

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;
}

This method does seem a little "hacky" though, and I'm sure there are better ways to implement it, but this is a possible quick solution.

0
votes

The way I would do it is by using rectangular collision boxes. The playerX and playerY is the player's coords:

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

That would be a rectangular box that you cant go within the boundaries of.