gpt4 book ai didi

java - 碰撞检测 - 防止玩家在撞击物体后移动

转载 作者:行者123 更新时间:2023-12-02 07:21:37 24 4
gpt4 key购买 nike

我有以下代码:

if(input.isKeyDown(Input.KEY_RIGHT)){
player = movingRIGHT;
pposX -= 0.15f;
if(playerR.intersects(rec1)){
pposX += 0.15f;
}
}

if(input.isKeyDown(Input.KEY_LEFT)){
player = movingLEFT;
pposX += 0.15f;
if(playerR.intersects(rec1)){
pposX -= 0.15f;
}

还有两个玩家——playerR和rec1:

playerR = new Rectangle(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());
rec1 = new Rectangle(448+pposX, 640-128+pposY, 32, 32);

我的玩家撞到墙上后没有停下来。我想知道坐标是否足够好,所以我尝试在完全相同的坐标处绘制矩形,没有问题。然而,我的播放器刚刚通过。

我正在使用 java slick2d 和以下代码:

package javagame;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Play extends BasicGameState {

Animation player,movingUP,movingDOWN,movingLEFT,movingRIGHT;
Image worldMap;
Image cat;
boolean quit = false;
int [] duration = {200,200,200,200};
int [] duration2 = {200,200};
float pposX;
float pposY;
float shiftX = pposX + 430;
float shiftY = pposY + 320;
float height;
int x = 0;
int y = 0;
boolean jumping = false;
float verticalSpeed;
float mapPosX = 0;
float mapPosY = 0;
Rectangle rec1;
Rectangle rec2;
Rectangle rec3;
Rectangle rec4;
Rectangle rec5;
Rectangle rec6;
Rectangle rec7;
Rectangle rec8;
Rectangle playerR;
boolean Win;
String a = "Victory!";
public Play (int state){

}

public void init(GameContainer gc,StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/map.png");
Image[] walkUp = {new Image("res/buckysBack.png") , new Image("res/buckysBack.png")};
Image[] walkLeft = {new Image("res/1L.png") , new Image("res/2L.png"), new Image("res/3L.png"), new Image("res/4L.png")};
Image[] walkRight = {new Image("res/1R.png") , new Image("res/2R.png"), new Image("res/3R.png"), new Image("res/4R.png")};
Image[] walkDown = {new Image("res/buckysFront.png") , new Image("res/BuckysFront.png")};


movingLEFT = new Animation(walkLeft , duration , true);
movingRIGHT = new Animation(walkRight,duration,true);
movingUP = new Animation(walkUp,duration2,false);
movingDOWN = new Animation(walkDown,duration2,false);

rec1 = new Rectangle(448+pposX, 640-128+pposY, 32, 32);
rec2 = new Rectangle(480,640-160,32,32);
rec3 = new Rectangle(512,640-192,64,32);


player = movingRIGHT;
}

public void render(GameContainer gc,StateBasedGame sbg,Graphics g)throws SlickException{
worldMap.draw(pposX,pposY);
player.draw(shiftX-174,shiftY+200);
g.drawRect(448+pposX, 640-128+pposY, 32, 32);
g.drawRect(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());
g.setColor(Color.blue);
g.drawString("Your position x: " + pposX * -1 + "\n y: " + pposY , 600, 30);
int mx = Mouse.getX();
int my = Mouse.getY();
g.drawString("Your mouse x: " + mx + "y: " + my, 400,400);
playerR = new Rectangle(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());


if(quit == true){
g.drawString("Resume(R)", sbg.getContainer().getWidth()/2, 100);
g.drawString("Main Menu(M)", sbg.getContainer().getWidth()/2, 150);
g.drawString("Quit(Q)", sbg.getContainer().getWidth()/2, 200);
if(quit==false){
g.clear();
}
}
if(Win){
g.setColor(Color.red);
g.drawString(a,sbg.getContainer().getWidth()/2 - a.length()/2,300);
}


}

public void update(GameContainer gc,StateBasedGame sbg,int delta)throws SlickException{
Input input = gc.getInput();

if(input.isKeyDown(Input.KEY_UP)){
pposY += 0.15f;
}
if(input.isKeyDown(Input.KEY_DOWN)){
pposY -= 0.15f;
}

if(jumping){
verticalSpeed += .007f * delta;
}
pposY += verticalSpeed;



if(input.isKeyDown(Input.KEY_RIGHT)){
player = movingRIGHT;
pposX -= 0.15f;
if(playerR.intersects(rec1)){
pposX += 0.15f;
}
if(pposX>3100){
Win = true;
}
}
if(input.isKeyDown(Input.KEY_LEFT)){
player = movingLEFT;
pposX += 0.15f;
if(playerR.intersects(rec1)){
pposX -= 0.15f;
}
if(pposX> -1.5f){
pposX-= 0.15f;
}
}

}
public int getID(){
return 1;
}
}

如何让玩家停在墙上?

最佳答案

playerR = new Rectangle(shiftX-174,shiftY+200,player.getWidth(),player.getHeight());

首先,我不知道 shiftX 是什么。您必须每帧更新矩形。示例:

playerR = new Rectangle(pposX, pposY, player.getWidth(), player.getHeight());

现在将该代码放在碰撞检测之前。请注意,您应该将宽度和高度存储在变量中以使其更快,而不必每帧调用 getWidthgetHeight

关于java - 碰撞检测 - 防止玩家在撞击物体后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14138681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com