2012年6月7日 星期四

Java & Slick 遊戲開發(11) - How to Change States



Play.java


package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState
{
public Play(int State)
{
}
public void init(GameContainer gc,StateBasedGame sbg) throws SlickException
{
}
public void render(GameContainer gc,StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawString("This is the Play State!", 100, 100); 顯示字符串
}
public void update(GameContainer gc,StateBasedGame sbg, int delta) throws SlickException
{
}
public int getID()
{
return 1;
}

}

Menu.java


package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.lwjgl.input.Mouse;

public class Menu extends BasicGameState
{
public Menu(int State)
{
}
public void init(GameContainer gc,StateBasedGame sbg) throws SlickException
{
}
public void render(GameContainer gc,StateBasedGame sbg, Graphics g) throws SlickException
{
g.fillOval(75, 100, 100, 100); 繪製橢圓形
g.drawString("Play Now!", 80, 70);
}
public void update(GameContainer gc,StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();
if((xpos>75 && xpos<175)&&(ypos>160 && ypos<260)){
if(input.isMouseButtonDown(0)){ 當用戶單擊左鍵
sbg.enterState(1); 進入到狀態1(Play)
}
}
}
public int getID()
{
return 0;
}

}

當您單擊該橢圓,然後去Play.java:








沒有留言:

張貼留言