[英]Pacman Collisions in Java
我正在 Java 上制作吃豆人街機游戲,但我的碰撞有問題。 如果你在這里看到我的圖像,我的吃豆子精靈在訪問一個角落或試圖轉回它來的方向(即向左但不再向右)時靜止不動(沒有 x 或 y 移動)。 我理解這是因為如果檢測到碰撞,我將 xMovement 設置為 0,將 yMovement 設置為 0(請參閱collide() 下的第二個代碼塊)。
如果我的碰撞()不允許這樣做,我將如何允許吃豆子精靈以另一種方式移動(即它來自左側,我希望它向右移動)或穿過一個角落?
下面是我在 App.java 類中的 draw 方法,它根據用戶的輸入繪制播放器。 按鍵分別對應上、左、下、右。 this.direction 僅用於讓玩家在發生碰撞時不會浪費移動或穿過牆壁。 *注意,我輸入(this.player.x_ord + 2) % 16 == 0
因為我的(this.player.x_ord + 2) % 16 == 0
圖像不是 16x16 圖像,不像我的牆壁那樣。
public void draw() { // This is an infinite loop
mapDraw(); // this draws my map constantly so I do not have multiple pacman sprites drawn
this.player.tick(); // see second code below for details
if (keyPressed){ // Used wasd rather than arrow keys
if (key == 's' && this.direction != 's' && (this.player.x_ord + 2) % 16 == 0 &&
(this.player.y_ord + 4) % 16 == 0){ // We dont want Player to turn ever on a non 16 divisible area
this.player.p = this.loadImage("src/main/resources/playerDown.png");
this.player.yMovement = this.player.speed;
this.player.xMovement = 0;
this.direction = 's';
}
else if (key == 'w' && this.direction != 'w' && (this.player.x_ord + 2) % 16 == 0 &&
(this.player.y_ord + 4) % 16 == 0){
this.player.p = this.loadImage("src/main/resources/playerUp.png");
this.player.yMovement = -this.player.speed;
this.player.xMovement = 0; // I do not want my pacman to move diagonally so thats why
this.direction = 'w';
}
else if (key == 'd' && this.direction != 'd' && (this.player.x_ord + 2) % 16 == 0 &&
(this.player.y_ord + 4) % 16 == 0){
this.player.p = this.loadImage("src/main/resources/playerRight.png");
this.player.xMovement = this.player.speed;
this.player.yMovement = 0;
this.direction = 'd';
}
else if (key == 'a' && this.direction != 'a' && (this.player.x_ord + 2) % 16 == 0 &&
(this.player.y_ord + 4) % 16 == 0){
this.player.p = this.loadImage("src/main/resources/playerLeft.png");
this.player.xMovement = -this.player.speed;
this.player.yMovement = 0;
this.direction = 'a';
}
}
this.player.draw(this); //see second code below for details
}
下面是我的 player.java 類 *再次注意this.x_ord + 2 == w.x_pos + 16 && this.y_ord + 4 == w.y_pos
是偏移的,因為我的this.x_ord + 2 == w.x_pos + 16 && this.y_ord + 4 == w.y_pos
精靈比我的 16x16 牆大。
public void tick(){
// //logic
this.detectCollision();
if (this.isLiving){
this.y_ord += this.yMovement;
this.x_ord += this.xMovement;
}
}
public void draw(PApplet a){ //just draw the sprite
if (this.isLiving){
a.image(this.p, this.x_ord, this.y_ord);
}
}
public void collide(boolean isX){ // Is it an x or y collision
if (isX == true){ // If it moves left or right into a wall
this.xMovement = 0;
}
else if (isX == false){ // If it moves up or down into a wall
this.xMovement = 0;
}
}
public void detectCollision(){
for (Walls w: this.wallLocations){ // A list of wall locations from a .txt file
if (this.x_ord + 2 == w.x_pos + 16 && this.y_ord + 4 == w.y_pos){ // Detect left movement into right wall piece
collide(true);
}
if (this.x_ord + 2 + 16 == w.x_pos && this.y_ord + 4 == w.y_pos){ // Detect right movement into left wall piece
collide(true);
}
if (this.y_ord + 4 == w.y_pos + 16 && this.x_ord + 2 == w.x_pos){ // Detect up movement into bottom wall piece
collide(false);
}
if (this.y_ord + 4 + 16 == w.y_pos && this.x_ord + 2 == w.x_pos){ // Detect down movement into top wall piece
collide(false);
}
}
非常感謝對我的問題的任何幫助。
我沒有深入分析您的代碼,但在我看來,您的主要問題是collide
方法僅考慮兩種情況,垂直移動或水平移動。 如果你想在 4 個不同的方向上移動,那么該方法需要有 4 個不同的狀態。
為了實現這一點,您可以創建一個表示方向的枚舉。 然后在detectCollision
適當的Direction
傳遞給collide
。 最后,在collide
考慮 4 個不同的方向。 例如,如果右側有障礙,則xMovement
需要為非正數。 collide
方法可能如下所示:
public void collide(Direction direction){
if (direction == Direction.RIGHT){
this.xMovement = Math.min(0, this.xMovement);
}
if (direction == Direction.LEFT){
this.xMovement = Math.max(0, this.xMovement);
}
if (direction == Direction.UP){
this.yMovement = Math.min(0, this.yMovement);
}
if (direction == Direction.DOWN){
this.yMovement = Math.max(0, this.yMovement);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.