我正在用 Slick2D 制作游戏。当玩家进入有敌人的窗户下时,他们可以射击,并且会增加分数。除了射击之外,我已经完成了所有机械师。这是我关于它如何工作的“计划”。
当玩家到达窗口下方(程序通过 y 坐标拾取)并开火时,点数将被添加到计数器中。
如何让我的程序意识到播放器确实位于窗口下方?
谢谢,这是我的 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;
}
}}