gamecanvas.java
来自「j2me 手机游戏」· Java 代码 · 共 946 行 · 第 1/2 页
JAVA
946 行
if(menuBK==null){
offG.setColor(0xFFFFFF);
offG.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
}else{
offG.drawImage(menuBK, 0, 0, 0);
}
if(this.gameState==PAUSE){
offG.setColor(0);
offG.fillRect(0, 0, 176, 208);
offG.setColor(0, 255, 128);
}
}
public void drawSound(Graphics offG){
offG.setClip(0, 0, this.getWidth(), this.getHeight());
offG.setColor(0);
offG.fillRect(0, 0, this.getWidth(), this.getHeight());
offG.setColor(0, 255, 128);
offG.drawString("sound setting", 20, 20, 0);
if(this.soundOn){
offG.drawString("sound on-off:on", 25, 40, 0);
}else{
offG.drawString("sound on-off:off", 25, 40, 0);
}
}
public void drawmenu(Graphics offG){
offG.setClip(0, 178, 130, 30);
offG.drawImage(menu, 0, 178, 0);
offG.setClip(10, 182, 100, 20);
offG.drawImage(menu, 20, 182-30-20*nowFramemenu, 0);
}
public static String strHelp[]=new String[]{
"key_num_2: up",
"key_num_8: down",
"key_num_4: left",
"key_num_6: right",
"key_num_7 and key_num_9 ",
"**customer service**",
"telephone: 010-85895xxx",
"E-mail: tantoxxx@163.com"
};
public void drawhelp(Graphics offG){
offG.setColor(0);
offG.fillRect(0, 0, this.getWidth(), this.getHeight());
offG.setColor(0, 255, 128);
offG.drawString("HELP", 20, 20, 0);
for(int i=0;i<strHelp.length;i++){
offG.drawString(strHelp[i], 5, 40+i*20, 0);
}
}
public int aboutIndex=0;
public static String strAbout[]=new String[]{
"Ij2me.cn",
"manager for customer service",
"name kewei",
"customer service telephone 0311xxxxxx",
"fax 0311xxxxxx",
"mobile telephone 13739745764",
"EMAIL",
"kwer159753@163.com",
"servicephone 02128239xxx",
"mobile telephone for customer service 136817172xx",
"WEB address",
"www.j2me.cn"
};
public void drawabout(Graphics offG){
offG.setColor(0);
offG.fillRect(0, 0, this.getWidth(), this.getHeight());
offG.setColor(0, 255, 128);
offG.drawString("ABOUT",20,20,0);
for(int i=aboutIndex;i<strAbout.length;i++){
if(i>aboutIndex+5)
{break;}
offG.drawString(strAbout[i], 5, 60+(i%6)*20, 0);
}
}
public void drawBack(){
offG.setClip(0,0,SCREENWIDTH,SCREENHEIGHT);
switch(this.level_Mode){
case 1:
offG.drawImage(mapbk, 0, mappy, 0);
offG.drawImage(mapbk, 0, mappy-208, 0);
}
mappy+=1;
if(mappy>=208){
mappy=0;
num++;
}
}
public void initLevel(int n){
levelStart=System.currentTimeMillis();
switch(n){
case 1:
try {
mapbk=Image.createImage("/dyd.png");
this.mapLength=mapbk.getHeight();
mappy=0;
// loadLogicData(1);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
//....
case 3:
//....
case 4:
//....
}
}
public void paint(Graphics g) {
offG.setClip(0, 0,SCREENWIDTH,SCREENHEIGHT);
switch(gameState){
case PAUSE:
case MENU:
switch(this.menuState){
case 0:
try {
d_logo=Image.createImage("/logo1.png");
} catch (Exception e) {
e.printStackTrace();
}
offG.drawImage(d_logo, 0, 0, 0);
break;
case 1:
try {
d_logo=null;
System.gc();
d_logo=Image.createImage("/logo2.png");
} catch (Exception e) {
e.printStackTrace();
}
offG.fillRect(0, 0, this.SCREENHEIGHT, this.SCREENWIDTH);
offG.drawImage(d_logo, 0, 0, 0);
break;
case 2:
try {
d_logo=null;
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
offG.drawImage(menuBK, 0, 0, 0);
break;
case 3:
drawmenubk(offG);
drawmenu(offG);
break;
case 4:
drawhelp(offG);
break;
case 5:
drawSound(offG);
break;
case 6:
drawabout(offG);
}
break;
case LOAD:
offG.setClip(0, 0,SCREENWIDTH,SCREENHEIGHT);
offG.setColor(0);
offG.fillRect(0, 0, SCREENWIDTH,SCREENHEIGHT);
offG.setColor(0,255,128);
offG.drawString("the"+level_Mode+" gate loading", 30, 50, 0);
offG.drawString(proess+"%", 60, 70, 0);
if(proess==0&&this.mapbk==null){
this.initLevel(level_Mode);
}
break;
case GAMEING:
drawBack();
drawPlane();
drawBullet();
paintEnemyPlane(offG, enemyPlaneImg);
paintEnemyBullet(offG, bulletImg);
offG.setColor(0xff0000);
offG.setClip(0,0,70,15);
break;
case GAMEOVER:
offG.setClip(0, 0,SCREENWIDTH,SCREENHEIGHT);
offG.setColor(0);
offG.fillRect(0, 0,SCREENWIDTH,SCREENHEIGHT);
if(level_Mode>3){
offG.fillRect(0, 0,SCREENWIDTH,SCREENHEIGHT);
endCount++;
if(endCount>20){
ISRUN=false;
gameStart.midlet.notifyDestroyed();
}
}
offG.setClip(0, 0,SCREENWIDTH,SCREENHEIGHT);
offG.setColor(0, 255, 128);
offG.drawString("gameover", 50, 50, 0);
offG.drawString("gamestart", 30, 80, 0);
break;
}
g.drawImage(im, 0, 0, 0);
}
public void drawPlane(){
offG.setClip(px, py, 30, 30);
offG.drawImage(imPlane, px, py, 0);
offG.setClip(0, 0, 176, 208);
}
public void drawBullet(){
for(int i=0;i<allBullet.size();i++){
int tmp[]=(int [])allBullet.elementAt(i);
offG.setClip(tmp[0], tmp[1], 7, 7);
offG.drawImage(imBullet, tmp[0]-tmp[2]*7, tmp[1], 0);
}
offG.setClip(0, 0, 176, 208);
}
public void keyPressed(int n){
System.out.println("nkey"+n);
if(gameState==5){
if(n==-6||n==6){
gameState=1;
}
this.repaint();
this.serviceRepaints();
return;
}
if(gameState==GAMEING){
int k=this.getGameAction(n);
switch(k){
case Canvas.UP:
direct=1;
// py-=8;
break;
case Canvas.DOWN:
direct=2;
// py+=8;
break;
case Canvas.LEFT:
direct=3;
// px-=8;
break;
case Canvas.RIGHT:
direct=4;
// px+=8;
break;
// case Canvas.FIRE:
// direct=0;
// this.isFire=true;
//
}
}
else if(gameState==MENU||gameState==PAUSE){
if(menuState==5&&(n==Canvas.KEY_NUM5||n==-5)){
this.soundOn=!this.soundOn;
this.repaint();
return;
}
if(menuState==6&&(n==Canvas.KEY_NUM2||n==-1||n==Canvas.KEY_NUM8||n==-2)){
if(aboutIndex==0)
aboutIndex=6;
else
aboutIndex=0;
this.repaint();
return;
}
if(menuState>=3){
if(n==Canvas.KEY_NUM2||n==-1||n==1&&(menuState!=4&&menuState!=6)){
nowFramemenu--;
if(nowFramemenu<=0){
nowFramemenu=0;
}
}
if(n==Canvas.KEY_NUM8||n==-2||n==2&&(menuState!=4&&menuState!=6)){
nowFramemenu++;
if(nowFramemenu>=4){
nowFramemenu=4;
}
}
if((n==-7||n==7)&&(this.menuState!=3)){
this.menuState=3;
this.repaint();
return;
}
boolean b1=(gameState==MENU)&&(n==6||n==-6)&&(this.menuState==3);
boolean b2=(gameState==PAUSE)&&(n==6||n==-6)&&(this.menuState==3);
if(menuState==3&&n==Canvas.KEY_NUM5||(n==-5)||(n==5)||(b1)||(b2)){
if(nowFramemenu==0){
if(gameState==MENU){
System.gc();
gameState=LOAD;
proess=0;
}else if(gameState==PAUSE){
gameState=GAMEING;
}
}
if(nowFramemenu==1){
this.menuState=4;
}
if(nowFramemenu==2){
this.menuState=6;
}
if(nowFramemenu==3){
this.menuState=5;
}
if(nowFramemenu==4){
ISRUN=false;
gameStart.midlet.notifyDestroyed();
}
}
this.repaint();
}
return;
}
else if(n==-7||n==7){
gameState=PAUSE;
this.repaint();
return;
}else if(n==-6||n==6){
gameState=5;
this.repaint();
return;
}
}
public void keyReleased(int n){
if(this.getGameAction(n)==Canvas.FIRE){
this.direct=0;
this.isFire=false;
}
if((this.getGameAction(n)==Canvas.UP)||(this.getGameAction(n)==Canvas.DOWN)||
(this.getGameAction(n)==Canvas.LEFT)||(this.getGameAction(n)==Canvas.RIGHT)){
this.direct=0;
}
}
public boolean flag=true;
public void run() {
while(ISRUN){
if(flag){
this.repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
if(this.menuState<3)
this.menuState++;
else{
flag=false;
}
continue;
}
switch(this.gameState){
case LOAD:
Thread.yield();
System.gc();
this.proess+=5;
if(this.proess>=100){
this.gameState=GAMEING;
}
this.repaint();
this.serviceRepaints();
break;
case GAMEOVER:
case GAMEING:
while(true){
if(this.direct!=0){
if(this.direct==1){
py-=16;
if(py<=0)
py=0;
this.isFire=true;
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();
this.serviceRepaints();
}
if(this.direct==2){
py+=16;
if(py>=208-30)
py=208-30;
this.isFire=true;
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();
this.serviceRepaints();
}
if(this.direct==3){
px-=16;
if(px<=0)
px=0;
this.isFire=true;
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();
this.serviceRepaints();
}
if(this.direct==4){
px+=16;
if(px>=176-30)
px=176-30;
this.isFire=true;
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();
this.serviceRepaints();
}
}
}
// break;
case PAUSE:
break;
}
repaint();
serviceRepaints();
long currTime=System.currentTimeMillis();
long delay=100-(currTime-lastTime);
lastTime=currTime;
try {
if(delay<=0)
delay=1;
Thread.sleep(delay);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?