[英]Pacman method for move Java libGDX
我正在使用 Java 和 libGDX 研究 Pacman 克隆 如何按一個按鈕使 Pacman 移動?
這是我的移動代碼,但我認為這不是最佳選擇:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
public class Hero {
private int rightBorder;
private int leftBorder;
private int top;
private int bottom;
private GameController gc;
private float cellX;
private float cellY;
private TextureRegion texture;
private float speed;
private boolean isPressedD;
private boolean isPressedA;
private boolean isPressedW;
private boolean isPressedS;
private float offset;
private StringBuilder stringBuilder;
private int score;
public Hero(TextureAtlas atlas, GameController gc){
this.texture = atlas.findRegion("Pacman");
this.cellX = 1;
this.cellY = 1;
this.speed = 5f;
this.isPressedD = false;
this.isPressedA = false;
this.isPressedS = false;
this.isPressedW = false;
this.offset = 36;
this.stringBuilder = new StringBuilder();
this.score = 0;
this.gc = gc;
this.leftBorder = 0;
this.rightBorder = 15;
this.top = 8;
this.bottom = 0;
}
public void update(float dt){
movement(dt);
checkBounds();
}
private void checkBounds(){
if (cellX > rightBorder){
cellX = rightBorder;
}
if (cellX < leftBorder){
cellX = leftBorder;
}
if (cellY > top){
cellY = top;
}
if (cellY < bottom){
cellY = bottom;
}
// if (position.x > 1280 - offset){
// position.x = 1280 - offset;
// }
// if (position.x < 0 + offset){
// position.x = 0 + offset;
// }
// if (position.y > 720 - offset){
// position.y = 720 - offset;
// }
// if (position.y < 0 + offset){
// position.y = 0 + offset;
// }
}
public void movement(float dt){
if (Gdx.input.isKeyJustPressed(Input.Keys.D)){
isPressedD = true;
isPressedA = false;
isPressedS = false;
isPressedW = false;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.A)){
isPressedD = false;
isPressedA = true;
isPressedS = false;
isPressedW = false;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.S)){
isPressedD = false;
isPressedA = false;
isPressedS = true;
isPressedW = false;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.W)){
isPressedD = false;
isPressedA = false;
isPressedS = false;
isPressedW = true;
}
if (isPressedA){
cellX -= speed * dt;
}
if (isPressedD){
cellX += speed * dt;
}
if (isPressedS){
cellY -= speed * dt;
}
if (isPressedW){
cellY += speed * dt;
}
}
public void renderGUI(SpriteBatch batch, BitmapFont font){
stringBuilder.setLength(0);
stringBuilder.append("Score: " + score);
font.draw(batch, stringBuilder, 10, 700);
}
public void render(SpriteBatch batch){
batch.draw(texture, cellX * GameMap.CELL_SIZE, cellY * GameMap.CELL_SIZE);
}
}
這里是直接移動方法:
public void movement(float dt){
if (Gdx.input.isKeyJustPressed(Input.Keys.D)){
isPressedD = true;
isPressedA = false;
isPressedS = false;
isPressedW = false;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.A)){
isPressedD = false;
isPressedA = true;
isPressedS = false;
isPressedW = false;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.S)){
isPressedD = false;
isPressedA = false;
isPressedS = true;
isPressedW = false;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.W)){
isPressedD = false;
isPressedA = false;
isPressedS = false;
isPressedW = true;
}
if (isPressedA){
cellX -= speed * dt;
}
if (isPressedD){
cellX += speed * dt;
}
if (isPressedS){
cellY -= speed * dt;
}
if (isPressedW){
cellY += speed * dt;
}
}
它正在工作,但我無法檢查與牆壁的碰撞。
如何改變搬家方式?
我需要按下按鈕並立即釋放。
無需創建布爾值然后對其進行檢查。 只需檢查是否按下了該鍵,然后將您的代碼放入該 if 語句中。
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
public class Hero {
private int rightBorder;
private int leftBorder;
private int top;
private int bottom;
private GameController gc;
private float cellX;
private float cellY;
private TextureRegion texture;
private float speed;
private float offset;
private StringBuilder stringBuilder;
private int score;
public Hero(TextureAtlas atlas, GameController gc){
this.texture = atlas.findRegion("Pacman");
this.cellX = 1;
this.cellY = 1;
this.speed = 5f;
this.offset = 36;
this.stringBuilder = new StringBuilder();
this.score = 0;
this.gc = gc;
this.leftBorder = 0;
this.rightBorder = 15;
this.top = 8;
this.bottom = 0;
}
public void update(float dt){
movement(dt);
checkBounds();
}
private void checkBounds(){
if (cellX > rightBorder){
cellX = rightBorder;
}
if (cellX < leftBorder){
cellX = leftBorder;
}
if (cellY > top){
cellY = top;
}
if (cellY < bottom){
cellY = bottom;
}
// if (position.x > 1280 - offset){
// position.x = 1280 - offset;
// }
// if (position.x < 0 + offset){
// position.x = 0 + offset;
// }
// if (position.y > 720 - offset){
// position.y = 720 - offset;
// }
// if (position.y < 0 + offset){
// position.y = 0 + offset;
// }
}
public void movement(float dt){
if (Gdx.input.isKeyJustPressed(Input.Keys.D)){
cellX += speed * dt;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.A)){
cellX -= speed * dt;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.S)){
cellY -= speed * dt;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.W)){
cellY += speed * dt;
}
}
public void renderGUI(SpriteBatch batch, BitmapFont font){
stringBuilder.setLength(0);
stringBuilder.append("Score: " + score);
font.draw(batch, stringBuilder, 10, 700);
}
public void render(SpriteBatch batch){
batch.draw(texture, cellX * GameMap.CELL_SIZE, cellY * GameMap.CELL_SIZE);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.