简体   繁体   English

Java Game Dev:检查玩家是否介于两个y coords之间

[英]Java Game Dev: Checking if player is in-between two y coords

I'm making a game 我正在制作游戏 看起来像这样 with Slick2D. 使用Slick2D。 When a player get's under a window with an enemy in it, they can shoot, and points will be added. 当玩家进入一个有敌人的窗口时,他们可以射击,并且会加入积分。 I have every mechanic completed besides the shooting one. 除了射击之外,我还有完成所有技工。 Here is my "plan" on how it'll work. 这是我关于它如何运作的“计划”。

When the player gets below the window(which the program picks up on via y coordinate) and fires, points will be added to a counter. 当玩家到达窗口下方(程序通过y坐标拾取)并触发时,点数将被添加到计数器中。

How can I get my program to realize that the player is indeed below a window? 我怎样才能让我的程序意识到玩家确实在窗户下面?

Thanks, and here's my PlayState code. 谢谢,这是我的PlayState代码。

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.Image;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import java.util.Random;
import java.util.TimerTask;
import java.util.Timer;

public class PlayState extends BasicGameState{

int stateID = -1;

int w = SwegBoi.WIDTH;
int h = SwegBoi.HEIGHT;
static int enemylocation;
float s = SwegBoi.SCALE;
Image playbackground;
Image swegboiplayer;
Image quit;
Image enemy1;
Image enemy2;
float playery;
int score = 0;

final static Random ran = new Random();
static Timer tm = new Timer();
static long startTime = System.currentTimeMillis();

public static void main(String args){

}


public PlayState(int stateID){
    this.stateID = stateID;
}

@Override
public void init(GameContainer gc, StateBasedGame sbg)
        throws SlickException {
    swegboiplayer = new Image("resources/swegboibackgun.png");
    playbackground = new Image("resources/playstatebackground.png");
    quit = new Image("resources/mainmenuquit.png");
    enemy1 = new Image("resources/enemy1.png");
    enemy2 = new Image("resources/enemy1.png");

    tm.schedule(new TimerTask() {

        @Override
        public void run() {
            enemylocation = ran.nextInt(4) + 1;
        }
    }, 1, 2000);


}




@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
        throws SlickException {
    playbackground.setFilter(Image.FILTER_NEAREST);
    playbackground.draw(0, 0, s*10);
    quit.draw((w-175*s),5 *s,s/2);

    if(enemylocation==1){
        enemy1.setFilter(Image.FILTER_NEAREST);
        enemy1.draw(200,170,s*10);
    }
    if(enemylocation==2){
        enemy1.setFilter(Image.FILTER_NEAREST);
        enemy1.draw(200,360,s*10);  
    }
    if(enemylocation==3){
        enemy1.setFilter(Image.FILTER_NEAREST);
        enemy1.draw(950,170,s*10);
    }
    if(enemylocation==4){
        enemy1.setFilter(Image.FILTER_NEAREST);
        enemy1.draw(950,360,s*10);
    }

    swegboiplayer.setFilter(Image.FILTER_NEAREST);
    swegboiplayer.draw((w*s)/2-(playery*s), 450*s, s*5);

    g.drawString("Alpha V0.1",6,6);
}



@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    Input input = gc.getInput();
    if(input.isKeyDown(Input.KEY_LEFT)){playery += 17;}
    if(input.isKeyDown(Input.KEY_RIGHT)){playery -= 17;}
    int mouseX = input.getMouseX();
    int mouseY = input.getMouseY();

    if(mouseHover(mouseX,mouseY,(w-175*s),5*s,quit.getHeight()/2,quit.getWidth()) == true){
        if(input.isMousePressed(0)){
            sbg.enterState(SwegBoi.MAINMENUSTATE,new FadeOutTransition(), new FadeInTransition());
        }
        quit = new Image("resources/mainmenuquithover.png");
    }else{
        quit = new Image("resources/mainmenuquit.png");

    }}



@Override
public int getID() {
    return stateID;
}


public boolean mouseHover(int mouseX, int mouseY, float x, float y, float height, float width){
    if((mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height)){
        return true;
    }else{
        return false;
    }

}} }}

Something like this should work. 这样的事情应该有效。 Every time you move, check if you are in a range of an enemy x position and determine if shooting should be enabled or disabled. 每次移动时,检查您是否处于敌人x位置范围内,并确定是应启用还是禁用拍摄。 Define some kind of range for each enemies X position. 为每个敌人X位置定义某种范围。

   private void checkShootStatus()
   {
      // Calculate image bounds X Position + width
      float swegboiplayerEndBound = swegboiplayer.getX() + swegboiplayer.getWidth();
      float enemyEndBound = enemy.getX() + enemy.getWidth();

      // Check enemy 1
      if (swegboiplayerEndBound > enemy.getX() && swegboiplayer.getX() < enemyEndBound)
      {
         canShoot = true;
      }
      else
      {
         canShoot = false;
      }
   }

Since you cannot get the x location of an image, create a wrapper class to track the x/y positions of the player and enemy. 由于无法获取图像的x位置,因此请创建一个包装类来跟踪玩家和敌人的x / y位置。

public class Player extends Image
{
   private String image;
   private float x = 0;
   private float y = 0;

   public Player(String image) throws SlickException
   {
      super(image);

      this.setImage(image);
   }

   public float getY()
   {
      return y;
   }

   public void setY(float y)
   {
      this.y = y;
   }

   public String getImage()
   {
      return image;
   }

   public void setImage(String image)
   {
      this.image = image;
   }

   public float getX()
   {
      return x;
   }

   public void setX(float x)
   {
      this.x = x;
   }
}

Enemy 敌人

public class Enemy extends Image
{
   private String image;
   private int x;
   private int y;

   public Enemy(String image) throws SlickException
   {
      super(image);

      this.setImage(image);
   }

   public int getY()
   {
      return y;
   }

   public void setY(int y)
   {
      this.y = y;
   }

   public String getImage()
   {
      return image;
   }

   public void setImage(String image)
   {
      this.image = image;
   }

   public int getX()
   {
      return x;
   }

   public void setX(int x)
   {
      this.x = x;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM