📄 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 + -