我刚刚开始制作游戏。我确实按照CodeNMore的Youtube教程,但我不喜欢基于像素的移动系统。我想制作一个基于网格的移动系统,但我无法解决这个问题。 我不够先进,无法阅读其他代码,了解它是如何工作的,这就是我问你们的原因!
我的图块是64x64,所以我所做的是将x
值更改为64.它对我没有帮助,因为我的角色开始一次传送所有64个像素而不是逐像素地移动对于64像素(图块大小)。我尝试使用for循环,定时器和thread.sleep,但它并没有真正帮助我。
//Activate both of the movement methods.
public void move() throws InterruptedException {
if (!checkEntityCollisions(xMove, 0f)) {
moveX();
}
if (!checkEntityCollisions(0f, yMove)) {
moveY();
}
System.out.println("Pos x: " + x + "\n Pos y: " + y);
}
//MOVEMENT METHOD
public void moveX() throws InterruptedException {
timer += System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
if (xMove > 0) { // MOVING RIGHT
int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;
//Checks for collisions
if (!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT)
&& !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
//tileSize = 64
x += tileSize;
} else {
x = tx * Tile.TILEWIDTH - bounds.x - bounds.width - 1;
}
} else if (xMove < 0) { //MOVING LEFT
int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;
if (!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT)
&& !collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x -= tileSize;
} else {
x = tx * Tile.TILEWIDTH + Tile.TILEWIDTH - bounds.x;
}
}
}
//图形代码:
private Display display;
private int width, height;
public String title;
public boolean running = false;
private Thread thread;
private BufferStrategy bs;
private Graphics g;
//STATES
private State gameState;
private State menuState;
//INPUT
private KeyManager keyManager;
//CAMERA
private GameCamera gameCamera;
//Handler
private Handler handler;
//GAME ENGINE
public Game(String title, int width, int height) {
this.height = height;
this.width = width;
this.title = title;
keyManager = new KeyManager();
}
public void init() {
display = new Display(title, width, height);
display.getFrame().addKeyListener(keyManager);
Assets.init();
gameCamera = new GameCamera(this, 0, 0);
handler = new Handler(this);
gameState = new GameState(handler);
menuState = new MenuState(handler);
State.setState(gameState);
}
public void tick() {
keyManager.tick();
if (State.getState() != null) {
State.getState().tick();
}
}
public void render() {
bs = display.getCanvas().getBufferStrategy();
if (bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//Clear Screen
g.clearRect(0, 0, width, width);
//Draw here!
if (State.getState() != null) {
State.getState().render(g);
}
//End Drawing
bs.show();
g.dispose();
}
public void run() {
init();
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
while (running) {
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
lastTime = now;
if (delta >= 1) {
tick();
render();
delta--;
}
}
stop();
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public KeyManager getKeyManager() {
return keyManager;
}
public GameCamera getGameCamera() {
return gameCamera;
}
public synchronized void start() {
if (running) {
return;
}
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if (!running) {
return;
}
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}