?? gamescreen.java
字號:
break;
case game_Pause:
if(lx < 0)
lx+=2;
if(rx > screenW/2)
rx-=2;
break;
case game_Over:{
if(lx < 0)
lx+=2;
if(rx > screenW/2)
rx-=2;
if(done/*&&(!wait)*/){ //等待動畫播放完畢
//判斷是否進入前三甲,彈出提示菜單,記錄或返回主菜單
try {
if(gr.isNewRecord(score)==true)
addHero();//跳轉到登記界面
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
GameState = game_Menu;
}
}break;
case game_Quit:{
//if(!wait){
running = false;
gml.Quit(); //退出游戲
//}
}break;
case game_Setting:
break;
case game_Record:
break;
case game_About:
break;
default:
System.out.println("ERROR");
break;
}
}
/**
* 更新游戲分數
*/
void UpdateScore(){
if(isHit&&(!Add)){ //加分
if(++got>=3){
score *= 2;
got = 0;
job--;
sc_x = screenW/2+screenW/6;
sc_y = screenH/7;
}else{
score += 5;
miss = 0;
}
//播放聲音
if(sound_on_off==true)
p1.playAudio();
Add = true;
}else if(!isHit&&(!Minus)){ //減分
if(++miss>=3){
score /= 2;
miss = 0;
sc_x = screenW/2+screenW/6;
sc_y = screenH/7;
}else{
score -= 5;
got = 0;
}
Minus = true;
}
if(score<0)
score = 0;
}
/*********************************************************************/
/* 游戲按鍵處理 */
/*********************************************************************/
protected void keyPressed(int keyCode){
key = keyCode; //記錄鍵值
//任意鍵返回
//wait = false;
switch(GameState){
case game_Menu:{ //主菜單狀態
if(getGameAction(keyCode)==Canvas.UP)
menu_index--;
if(getGameAction(keyCode)==Canvas.DOWN)
menu_index++;
if(getGameAction(keyCode)==Canvas.FIRE){
switch(menu_index){
case 0:
GameState = game_Start;
break;
case 1:
GameState = game_Setting;
break;
case 2:
GameState = game_Record;
break;
case 3:
GameState = game_About;
break;
case 4:
GameState = game_Quit;
//wait = true;
break;
default:
System.out.println("NO SUCH SELECT");
}
//重新初始化資源
initPic();
initGame();
}
}break;
case game_Start:{ //游戲狀態
if(!hasMove){
//1~9
if(keyCode==Canvas.KEY_NUM1){
hasMove = true;
index = 0;
}
if(keyCode==Canvas.KEY_NUM2){
hasMove = true;
index = 1;
}
if(keyCode==Canvas.KEY_NUM3){
hasMove = true;
index = 2;
}
if(keyCode==Canvas.KEY_NUM4){
hasMove = true;
index = 3;
}
if(keyCode==Canvas.KEY_NUM5){
hasMove = true;
index = 4;
}
if(keyCode==Canvas.KEY_NUM6){
hasMove = true;
index = 5;
}
if(keyCode==Canvas.KEY_NUM7){
hasMove = true;
index = 6;
}
if(keyCode==Canvas.KEY_NUM8){
hasMove = true;
index = 7;
}
if(keyCode==Canvas.KEY_NUM9){
hasMove = true;
index = 8;
}
}// end if
//游戲暫停
if(keyCode==-7||keyCode==22){ //右軟件
GameState = game_Pause;
lx = -screenW/2;
rx = screenW;
}
}break;
case game_Pause:
//繼續游戲
if(keyCode==-6||keyCode==21||keyCode==Canvas.KEY_NUM1)
GameState = game_Start;
//返回菜單
if(keyCode==-7||keyCode==22||keyCode==Canvas.KEY_NUM3)
GameState = game_Menu;
break;
case game_Over:
break;
case game_Setting:{
if(keyCode==Canvas.KEY_NUM5||getGameAction(keyCode)==Canvas.FIRE)
sound_on_off = !sound_on_off;
if(keyCode==-6||keyCode==21||keyCode==Canvas.KEY_NUM1) //左軟件
GameState = game_Menu;
}break;
case game_Record:
if(keyCode==-6||keyCode==21||keyCode==Canvas.KEY_NUM1) //左軟件
GameState = game_Menu;
break;
case game_About:
if(getGameAction(keyCode)==Canvas.UP){
if(start>0)
start--;
}
if(getGameAction(keyCode)==Canvas.DOWN){
if(start<=94/n-5)
start++;
}
if(keyCode==-6||keyCode==21||keyCode==Canvas.KEY_NUM1)
GameState = game_Menu;
break;
case game_Quit:
break;
}
//show FPS
if(keyCode==Canvas.KEY_STAR)
show = !show;
}
protected void keyReleased(int keyCode){
key = -100; //使鍵值無效
}
/*********************************************************************/
/* 記錄游戲分數到排行榜 */
/*********************************************************************/
private void addHero(){
if(f_record==null){
f_record = new Form("記錄存儲");
final TextField tf = new TextField("請輸入姓名:","AAA",20,0);
f_record.append(tf);
f_record.addCommand(new Command("確定",Command.SCREEN,0));
CommandListener cl = new CommandListener(){ //通過內部類實現監聽器
public void commandAction(Command c, Displayable dis) {
if(dis==f_record){
if(c.getLabel().equals("確定"))
try {
gr.addRecord(tf.getString(), score); //添加記錄
Display.getDisplay(gml).setCurrent(gs);
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
f_record.setCommandListener(cl); //添加監聽器
}
//跳轉到記錄界面
Display.getDisplay(gml).setCurrent(f_record);
}
/*********************************************************************/
/* 實現字符繪制和圖片縮放 */
/*********************************************************************/
/**
* 畫字符的工具函數
* @param str 字符
* @param font 字體
* @param color 顏色
* @param x x坐標
* @param y y坐標
* @param g 畫筆
*/
public void drawSTR(String str,Font font,int color,int x,int y,Graphics g){
g.setFont(font); //設置字體
g.setColor(color); //設置顏色
g.setClip(0, 0, getWidth(), getHeight());
g.drawString(str, x, y, 20);
}
/**
* 橫向縮放圖片
* @param img 目標圖片
* @param scaleW 縮放后的寬
* @return 返回縮放后的圖片
*/
public static Image HScaleImage(Image img,int scaleW){
if(img==null){ //判斷是否為空
System.out.println("HScaleImage");
return null;
}
if(scaleW<1) //縮放大小不能小于1
scaleW = 1;
int imgW = img.getWidth(); //獲取圖片的寬
if(scaleW==imgW) //如果縮放的寬度等于原圖寬
return img; //直接返回原圖片
int imgH = img.getHeight(); //獲取圖片的高
Image temp = Image.createImage(scaleW, imgH); //創建臨時圖片,大小為改變后的寬高
Graphics g = temp.getGraphics(); //獲取臨時圖片的畫筆
//繪制新圖片
int pos = imgW/2;
for(int x=0; x<scaleW; x++){
int n = x*2+1;
g.setClip(x, 0, 1, imgH); //裁剪出一個寬為1,高度為圖像高的區域
g.drawImage(img, x-(pos*n/scaleW), 0, 20);
// pos*n/scaleW 是圖像偏移量
//如果兩次相差大于1,則略過了某個像素條(縮小),否則重復了某個像素條(放大)
}
return temp;
}
/**
* 縱向縮放圖片
* @param img 目標圖片
* @param scaleH 縮放后的高
* @return 返回縮放后的圖片
*/
public static Image VScaleImage(Image img,int scaleH){
if(img==null){
System.out.println("VScaleImage");
return null;
}
if(scaleH<1)
scaleH = 1;
int imgH = img.getHeight();
if(scaleH==imgH)
return img;
int imgW = img.getWidth();
Image temp = Image.createImage(imgW, scaleH);
Graphics g = temp.getGraphics();
int pos = imgH/2;
for(int y=0; y<scaleH; y++){
int n = y*2+1;
g.setClip(0, y, imgW, 1);
g.drawImage(img, 0, y-(pos*n/scaleH), 20);
}
return temp;
}
/**
* 圖片縮放
* @param img 目標圖片
* @param scaleW 縮放后的寬
* @param scaleH 縮放后的高
* @return 返回縮放后的圖片
*/
public static Image scaleImage(Image img,int scaleW,int scaleH){
return VScaleImage(HScaleImage(img,scaleW),scaleH); //先橫向縮放,再進行縱向縮放
}
/*********************************************************************/
/* 設置圖片透明 */
/*********************************************************************/
/**
* 使圖片中的特定顏色變為透明色
* @param img 目標圖片
* @param color_code 選定顏色 (白色 -1,黑色 )
* @return
*/
public static final Image alfImage(Image img,int color_code){
if(img == null){
System.out.println("alfImage");
return null;
}
int imgW = img.getWidth();
int imgH = img.getHeight();
int[] RGBData = new int[imgW*imgH];
img.getRGB(RGBData,0,imgW,0,0,imgW,imgH);
for(int i=0;i<RGBData.length;i++){
if(RGBData[i]==color_code)
RGBData[i] &= 0xffffff;
}
Image o_Img = Image.createRGBImage(RGBData,imgW,imgH,true);
return o_Img;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -