对不起,我不知道标题是否是最好的解释方式。我正在使用 libgdx 在 java 中制作 pacman 克隆。我已经在瓷砖上渲染了地图并进行了碰撞检测。唯一的问题是,吃豆人无论如何都应该继续移动。当他向右走时,您向下或向上按被阻挡时,他会停下来。我尝试了很多不同的东西,但似乎无法让它正常工作。
这是我的 pacman 播放器课程
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);
}
}