我用Bucky的Java教程做了一个简单的2D状态变化游戏,现在我修改了这个游戏,想在地图上设置碰撞,以便我的玩家不能穿过地图上的房子。我认为我有点明白碰撞是如何工作的:
你可以使用以下代码制作两个矩形:
然后,在创建这两个矩形之后,您可以设置一个if语句,例如:如果相交,则执行某些操作...
希望在这之后能够正常工作。关于游戏的更多信息,它是一款状态变化游戏,具有几种方法,例如init、render和update(我应该将我的矩形和if语句放在update方法中吗?)。此外,它是一款俯视视角的游戏,类似于口袋妖怪。如果您需要我的代码,请告诉我,我不想在这里放太多内容。
这是我的Play类,我只有另外两个类,一个是main类,另一个是menu类。我无法想象把矩形方法放在main或menu类中,所以唯一剩下的就是Play类了,但我不知道如何在我已经编写的代码中创建2个不同的矩形(一个用于玩家,另一个用于房子)。如果您需要我的main和menu类,请告诉我。
编辑3:
我尝试了您所说的,并制作了一个Rectanglebase类,并将您发布的if语句放在其中,但是我遇到了错误,它要求我在我的player类中编写getX和getY方法,同时构造函数前面的public也有错误。
我也按照你说的创建了一个Home和Player类,但是我有些困惑需要在其中放些什么内容。在Home类中我写了以下内容:
但我一直在遇到错误,不确定我的操作是否正确。此外,在玩家类的x、y位置中,我该如何从Play类设置我的浮点变量到玩家类?
你可以使用以下代码制作两个矩形:
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
对玩家和障碍物分别创建一个矩形,您应该如何将此放入代码中,以及如何告诉Java障碍物的矩形与玩家矩形不同?然后,在创建这两个矩形之后,您可以设置一个if语句,例如:如果相交,则执行某些操作...
希望在这之后能够正常工作。关于游戏的更多信息,它是一款状态变化游戏,具有几种方法,例如init、render和update(我应该将我的矩形和if语句放在update方法中吗?)。此外,它是一款俯视视角的游戏,类似于口袋妖怪。如果您需要我的代码,请告诉我,我不想在这里放太多内容。
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState{
Animation bucky, movingUp, movingDown, movingLeft, movingRight, movingBL, movingBR, movingFL, movingFR;
Image worldMap;
boolean quit = false;//gives user to quit the game
int[] duration = {200, 200};//how long frame stays up for
float buckyPositionX = 0;
float buckyPositionY = 0;
float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem
float shiftY = buckyPositionY + 160;//the numbers are half of the screen size
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/world.png");
Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation
Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")};
Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")};
Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")};
Image[] walkBL = {new Image("res/bl.png"), new Image("res/bl.png")};
Image[] walkBR = {new Image("res/br.png"), new Image("res/br.png")};
Image[] walkFL = {new Image("res/fl.png"), new Image("res/fl.png")};
Image[] walkFR = {new Image("res/fr.png"), new Image("res/fr.png")};
movingUp = new Animation(walkUp, duration, false);
movingDown = new Animation(walkDown, duration, false);
movingLeft = new Animation(walkLeft, duration, false);
movingRight = new Animation(walkRight, duration, false);
movingBL = new Animation(walkBL, duration, false);
movingBR = new Animation(walkBR, duration, false);
movingFL = new Animation(walkFL, duration, false);
movingFR = new Animation(walkFR, duration, false);
bucky = movingDown;//facing screen initially on startup
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0
bucky.draw(shiftX, shiftY);//makes him appear at center of map
g.drawString("Suraj's X: "+buckyPositionX+"\nSuraj's Y: "+buckyPositionY,400,20);//tells us the position
if(quit==true){
g.drawString("Resume(R)", 250, 100);
g.drawString("Main(M)", 250, 150);
g.drawString("Quit Game(Q)", 250, 200);
if(quit==false){
g.clear();//wipe off everything from screen
}
}
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
Input input = gc.getInput();
//up
if(input.isKeyDown(Input.KEY_UP)){
bucky = movingUp;//changes the image to his back
buckyPositionY += 10;;//increase the Y coordinates of bucky (move him up)
if(buckyPositionY>162){//if I reach the top
buckyPositionY -= 10;//stops any further movement in that direction
}
}
//down
if(input.isKeyDown(Input.KEY_DOWN)){
bucky = movingDown;
buckyPositionY -= 10;
if(buckyPositionY<-600){
buckyPositionY += 10;//basically change the direction if + make -
}}
//left
if(input.isKeyDown(Input.KEY_LEFT)){
bucky = movingLeft;
buckyPositionX += 10;
if(buckyPositionX>324){
buckyPositionX -= 10;//delta * .1f
}}
//right
if(input.isKeyDown(Input.KEY_RIGHT)){
bucky = movingRight;
buckyPositionX -= 10;
if(buckyPositionX<-840){
buckyPositionX += 10;
}}
//2 key combos start here
if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_UP)){
bucky = movingBR;
buckyPositionX -= delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
if(buckyPositionY>162){
buckyPositionY -= delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_UP)){
bucky = movingBL;
buckyPositionX -= delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
if(buckyPositionY>162){
buckyPositionY -= delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_RIGHT) && input.isKeyDown(Input.KEY_DOWN)){
bucky = movingFR;
buckyPositionX -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
if(buckyPositionX<-840){
buckyPositionX += delta * .1f;
}}}
if(input.isKeyDown(Input.KEY_LEFT) && input.isKeyDown(Input.KEY_DOWN)){
bucky = movingFL;
buckyPositionX -= delta * .1f;
if(buckyPositionY<-600){
buckyPositionY += delta * .1f;
if(buckyPositionX>324){
buckyPositionX -= delta * .1f;
}}}
//escape
if(input.isKeyDown(Input.KEY_ESCAPE)){
quit=true;
}
//when the menu is up
if(quit==true){//is the menu on the screen
if(input.isKeyDown(Input.KEY_R)){
quit = false;//resumes the game, makes menu dissapear
}
if(input.isKeyDown(Input.KEY_M)){
sbg.enterState(0);//takes you to the main menu
}
if(input.isKeyDown(Input.KEY_Q)){
System.exit(0);//quits the game
}
}
}
public int getID(){
return 1;
}
}
这是我的Play类,我只有另外两个类,一个是main类,另一个是menu类。我无法想象把矩形方法放在main或menu类中,所以唯一剩下的就是Play类了,但我不知道如何在我已经编写的代码中创建2个不同的矩形(一个用于玩家,另一个用于房子)。如果您需要我的main和menu类,请告诉我。
编辑3:
我尝试了您所说的,并制作了一个Rectanglebase类,并将您发布的if语句放在其中,但是我遇到了错误,它要求我在我的player类中编写getX和getY方法,同时构造函数前面的public也有错误。
public Rectanglebase{}//the public is saying syntax error
我也按照你说的创建了一个Home和Player类,但是我有些困惑需要在其中放些什么内容。在Home类中我写了以下内容:
return Rectangle(100,100,100,100);
但我一直在遇到错误,不确定我的操作是否正确。此外,在玩家类的x、y位置中,我该如何从Play类设置我的浮点变量到玩家类?
Rectangle2D#intersects
替代了JPanel
(因为移动时它会导致重绘次数激增)。我没有使用Shape
,因为那些被覆盖的方法让我感到困惑,例如 pathIterator 等 :O) - David KroukampShape
实现的完全通用性,那么Rectangle2D#intersects
可能会更有效率。 - trashgod