0

ゲーム

キーボードの矢印入力(上、下、左、右)で移動する2Dマップ上のプレーヤーの俯瞰図がある、単純なJavaゲームをコーディングしました。すべてのゲームコードを保持するPlayクラスが構成されています。いくつかの方法のうち、ゲーム開始時にプレーヤーがどのように向き合うべきかなどの初期ゲームオプションを保持するinit、実際のプレーヤーやマップなどのすべてを画面に描画するレンダリング、キーボード入力を処理する更新、プレイヤーを動かす、私がプレイヤーを動かすと言うとき、このゲームでは実際にはマップを動かすことです。マップが非常に大きいので、プレイヤーがマップを歩き回ることができ、画面がそれに追従し、最も簡単な方法が必要でした。それは、プレイヤーを静止させ、マップを動かして、プレイヤーが実際に動いているような錯覚を作り出していました。

問題

マップ以外はすべて正常に機能します。マップは大きな2D画像であり、家などの障害物があります。プレーヤーが家やその他の障害物を通り抜ける瞬間(今は家だけに集中しましょう)、私はプレイヤーが家の中を歩くのを止めるためのゲームが必要です。そこで、単純な長方形の衝突検出を使用して、衝突が発生した場合にプレーヤーの方向を変更することを計画したので、2つの長方形を作成しようとしましたが、renderメソッド(上記の1つ)を使用して画面に長方形を描画すると問題が発生しましたプレーヤー(rectOne)、家の上の別の(rectTwo))計画どおりではありません。ゲームをプレイするときに、一方の長方形をもう一方の長方形に移動しようとすると、これは問題になります。 2つの長方形の衝突は同時に移動します。

コード

示されているコードは、すべてのゲームコードを保持するPlayクラスの簡略化されたバージョンです。

    imports are here
    public class Play extends BasicGameState{
    Animation bucky, movingUp, movingDown, movingLeft, movingRight;
    Image worldMap;
    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
    java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90);
    java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150);
    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")};
    movingUp = new Animation(walkUp, duration, false);
    movingDown = new Animation(walkDown, duration, false);  
    movingLeft = new Animation(walkLeft, duration, false);  
    movingRight = new Animation(walkRight, 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.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight());
    g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight());
}
    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 += 2;;//increase the Y coordinates of bucky (move him up)
        if(buckyPositionY>162){//if I reach the top 
            buckyPositionY -= 2;//stops any further movement in that direction
        }
    }
    //down
    if(input.isKeyDown(Input.KEY_DOWN)){
        bucky = movingDown;
        buckyPositionY -= 2;
        if(buckyPositionY<-550){
            buckyPositionY += 2;//basically change the direction if + make -
    }}
    //left
    if(input.isKeyDown(Input.KEY_LEFT)){
        bucky = movingLeft;
        buckyPositionX += 2;
        if(buckyPositionX>324){
            buckyPositionX -= 2;//delta * .1f
    }}
    //right
    if(input.isKeyDown(Input.KEY_RIGHT)){
        bucky = movingRight;
        buckyPositionX -= 2;
        if(buckyPositionX<-776){
            buckyPositionX += 2;
    }}}   
    public int getID(){
        return 1;
    }}

コードは、2つの長方形(rectOneとrectTwo)を示しています。rectOneはプレーヤーの上にあり、rectTwoはマップ上の家の上にある必要があります。これは発生しません。両方のリアングルが一緒に移動します。覚えておいてください。マップが移動し、プレーヤーは静止しており、マップはbuckyPositionXとbuckyPositionYの座標に設定され、プレーヤーは画面の中心であるshiftXとshiftYに設定されています。これは、プレーヤーが動かないため、必要に応じて定数に置き換えることができます。 updateメソッドのステートメントはbuckyPositionXとbuckyPositonYを変更します。つまり、マップを移動するため、rectTwo(家)も移動する必要がありますが、rectOne(プレーヤーの長方形)は静止しています。これが、変数buckyPositionXとbuckyPoitionYを使用した理由です。 rectTwoの場合、しかし、私の問題はまだ発生しているので、何かが間違っているに違いありません。

////////////編集-投稿を短くするために段落を削除しました//////////////

前もって感謝します。

4

0 に答える 0