2
votes

Sorry guys, I don't know if the title is the best way to explain it. I'm making a pacman clone in java with libgdx. I've got the map rendered and collision detection on the tiles working. The only problem, the pacman is suppose to keep moving no matter what. When he is going right, and you press down or up which is blocked, he stops. I've tried a bunch of different things and can't seem to get it to work right.

This is my pacman player class

package com.psillidev.pacman1;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;

public class player {
    private static int height = 16;
    private static int width = 16;

    private int playerX , playerY;
    private int direction;
    private Texture playerTexture;
    private boolean isAlive; 


    public player(int startX,int startY) {
        playerTexture = new Texture(Gdx.files.internal("data/pacman.png"));
        playerX = startX;
        playerY = startY;
    }

    public void move(Map map) {
        if (direction == 0) {
            if (isNoTile(map,direction,getX(),getY())) {
                setY(getY() + (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

        if (direction ==1) {
            if (isNoTile(map,direction,getX(),getY())) {
                setX(getX() + (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

        if (direction ==2) {
            if (isNoTile(map,direction,getX(),getY())) {
                setY(getY() - (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

        if (direction ==3) {
            if (isNoTile(map,direction,getX(),getY())) {
                setX(getX() - (int)(Gdx.graphics.getDeltaTime() + 2));
            }
        }

    }

    private boolean isNoTile(Map map, int dir, int translated_x, int translated_y) {
        System.out.println("X: " + translated_x + " Y: " + translated_y);
        for (Rectangle r : map.getBlockedTiles()) {

            switch (dir) {
            case 0:
                Rectangle up = new Rectangle(translated_x,translated_y + 2 , 16, 16);

                if (up.overlaps(r)) {

                    return false;

                }
                break;

            case 1:
                Rectangle right = new Rectangle(translated_x+2,translated_y, 16, 16);
                if (right.overlaps(r)) {
                    return false;
                }
                break;

            case 2:
                Rectangle down = new Rectangle(translated_x,translated_y - 2, 16, 16);
                if (down.overlaps(r)) {
                    return false;
                }
                break;

            case 3:
                Rectangle left = new Rectangle(translated_x-2,translated_y, 16, 16);
                if (left.overlaps(r)) {
                    return false;
                }
                break;

            default:
                break;
            }
        }

        return true;
    }

    public void render(SpriteBatch batch) {
        batch.draw(playerTexture, playerX, playerY);
    }

    public int getX() {
        return playerX;
    }

    public void setX(int x) {
        playerX = x;
    }

    public int getY() {
        return playerY;
    }

    public void setY(int y) {
        playerY = y;
    }

    public int getDirection() {
        return direction;
    }

    public void setDirection(int dir) {

        direction = dir;
    }

    public Rectangle getRect() {
        return new Rectangle(playerX,playerY,width,height);
    }
}
3
You have to handle direction when pacman come other tile. Otherwise it keep moving.Muzaffer

3 Answers

1
votes

I can't answer from the code that you've supplied, but my suggestion is that you need to make an adjustment to your KeyListener class. I'm assuming that you have a KeyListener that is listening for the user pressing an arrow key. When a key is pressed, are you doing something like Pacman.setDirection()?

What you would actually need to do is detect whether the direction is valid before calling setDirection(). So your code of the KeyListener would be something like this...

  1. Determine whether the user has pressed up, down, left, or right
  2. Check the game grid/map to see if the direction is valid based on the current position of the Pacman
  3. If the direction is valid, call setDirection(). If not, don't call anything, and the Pacman will continue in the current direction.
0
votes

This really shouldn't be handled by the player itself but maybe by a movement manager, makes for easier testing/expansion of the game. In your code you only allow him to move to empty tiles in the direction your input decides, so he automatically stops moving if you press UP when walking to the right if UP is a wall, regardless of what is to the right.

What you can do is on input: If requested to move up, check if up is empty, then move up, if up is not empty then keeping moving in the direction he was previously moving. Same for other directions.

0
votes

Your setDirection Method is incorrect it needs to be

public void setDirection(int dir) {
    // only change direction if we can.
    if (isValidDirection(dir)){
        direction = dir;
    }
}