JAVA游戏编程之二----j2me MIDlet 手机游戏入门开发--贪吃蛇_1
作者:雷神
QQ:38929568
QQ群:28048051JAVA游戏编程(满) 28047782(将满)
修改贪吃蛇一些BUG,主要在cGame.java Gif.java这连个类其他的可以参考上一片
JAVA游戏编程之二----j2me MIDlet 手机游戏入门开发--贪吃蛇
本文给出的是程序代码,需要图片才能编译程序,
工程文件已经打包上传到csdn,地址如下
download.csdn.net/user/kome2000/
Gif.java 这个类是绘制动画的类,也是蛇的食物类
cGame.java
//
// cGame.java
//
// Project: Minesweeper
// Author(s): Gao Lei
// Create: 2007-10-12
package code;
import java.util.Random;
import javax.microedition.lcdui. * ;
class cGame extends Canvas implements Runnable
... {
private static final int STATEPLAY = 0;
private static final int STATELOST = 1;
private static final int STATEWIN = 2;
private static final int KEY_UP = 1;
private static final int KEY_DOWN = 2;
private static final int KEY_LEFT = 3;
private static final int KEY_RIGHT = 4;
private static final int KEY_FIRE = 5;
public static int s_width = 0;
public static int s_height = 0;
public static long updates = 0;
public static Random rand;
private int maxRand = 1000;
private int map_x = 10;
private int map_y = 10;
private int map_w = 16;
private int map_h = 16;
private int key_x = map_x / 2;
private int key_y = map_y / 2;
private int snake_w = 8;
private int snake_h = 8;
private int pos_x = map_x / 2;
private int pos_y = map_y / 2;
private int aspect_x= 0;
private int aspect_y= 1;
private int snake_max = 50;
private int snake_min = 5;
private int snake_n = snake_min;
private int gameState = STATEPLAY;
private int level = 1;
private long sleepTime =300;
private int[] snake_x = new int[ snake_max ];
private int[] snake_y = new int[ snake_max ];
private int[][] map;
private boolean isShowInfo = false;
private Font font = Font.getFont( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE );
private Thread thread;
private Image[] imgGameBg = new Image[4];
private Image[] imgItem = new Image[3];
private Gif[] gold = new Gif[5];
private Gif[] dung = new Gif[2];
private Gif fungus;
cGame()
...{
setFullScreenMode(true);
s_width = getWidth();
s_height= getHeight();
rand = new Random( System.currentTimeMillis() );
try
...{
for( int i=0; i<gold.length; i++ )
...{
gold[i]= new Gif("gold", 5, 19, 15 );
}
for( int i=0; i<dung.length; i++ )
...{
dung[i]= new Gif("dung", 6, 21, 30 );
dung[i].life = -2; // --life
}
fungus = new Gif("fungus", 12, 25, 23 );
fungus.setFormTime
(
new int[] ...{ 2000, 150, 150, 150,
150, 200, 100, 100,
200, 150, 200, 200
}
);
fungus.life = 5; // --life +5
fungus.isShow = false;
for( int i=0; i<imgItem.length; i++ )
...{
imgItem[i]=Image.createImage("/pics/item_16_"+i+".png");
}
Image temp = Image.createImage("/pics/bg_tile_0.png");
Graphics gn;
for( int i=0; i<imgGameBg.length; i++ )
...{
imgGameBg[i] = Image.createImage(16, 16);
gn = imgGameBg[i].getGraphics();
gn.drawImage(temp, -i*16, 0, gn.LEFT|gn.TOP);
}
gn = null;
temp = null;
System.gc();
}catch(Exception e)...{ e.printStackTrace(); }
rePlay( level );
thread = new Thread(this);
thread.start();
}
public void run()
...{
while( true )
...{
try
...{
updates++;
repaint();
serviceRepaints();
thread.sleep(sleepTime);
}catch(Exception e)
...{
e.printStackTrace();
}
}
}
public void paint(Graphics g)
...{
g.setClip(0, 0, s_width, s_height);
g.setColor(0x000000);
g.fillRect(0, 0, s_width, s_height);
for( int i=0; i<map_x; i++ )
...{
for( int j=0; j<map_y; j++ )
...{
g.drawImage(imgGameBg[ map[i][j] ], j*map_h, i*map_w, g.LEFT|g.TOP);
}
}
for( int i=0; i<gold.length; i++ )
...{
gold[i].paint(g);
}
for( int i=0; i<dung.length; i++ )
...{
dung[i].paint(g);
}
if( snake_n>20 )
...{
fungus.isShow = true;
fungus.paint( g );
}
paintSnake( g );
if( isShowInfo || gameState != STATEPLAY )
...{
g.setColor(0xFFFFFF);
for( int i=0; i<=map_y; i++ ) // |||
...{
g.drawLine(i*map_w, 0, i*map_w, map_h*map_x);
}
for( int i=0; i<=map_x; i++ ) // ===
...{
g.drawLine(0, i*map_h, map_y*map_w, i*map_h);
}
g.setFont( font );
}
g.setColor( 0xff0000 );
g.setFont( font );
g.drawString( "life:"+snake_n, 2, 2, 0 );
g.drawString( "level:"+level, 2, 18, 0 );
}
void paintSnake( Graphics g )
...{
g.setColor(0x0000FF);
for( int i=snake_n; i>0; i-- )
...{
snake_x[i] = snake_x[i-1];
snake_y[i] = snake_y[i-1];
g.fillRect(snake_x[i]-snake_w/2, snake_y[i]-snake_h/2, snake_w, snake_h);
}
snake_x[0] += aspect_x*8;
snake_y[0] += aspect_y*8;
g.setColor(0x6666FF);
g.fillRect(snake_x[0]-snake_w/2, snake_y[0]-snake_h/2, snake_w, snake_h);
if( snake_x[0]<0 || snake_x[0]>s_width || snake_y[0]<0 ||snake_y[0]>s_height )
...{
rePlay(level);
}
for( int i=snake_min; i<snake_n; i++ )
...{
if( isIntersect(snake_x[0], snake_y[0], snake_w, snake_h,
snake_x[i], snake_y[i], snake_w, snake_h )
)
...{
rePlay(level);
}
}
for( int i=0; i<gold.length; i++ )
...{
if( isIntersect(snake_x[0], snake_y[0], snake_w, snake_h,
gold[i].pos_x, gold[i].pos_y, gold[i].w, gold[i].h )
)
...{
addSnake( gold[i].life );
gold[i].setPos();
}
}
for( int i=0; i<dung.length; i++ )
...{
if( isIntersect(snake_x[0], snake_y[0], snake_w, snake_h,
dung[i].pos_x, dung[i].pos_y, dung[i].w, dung[i].h )
)
...{
addSnake( dung[i].life );
dung[i].setPos();
}
}
if( fungus.isShow && isIntersect(snake_x[0], snake_y[0], snake_w, snake_h,
fungus.pos_x, fungus.pos_y, fungus.w, fungus.h )
)
...{
addSnake( fungus.life );
fungus.setPos();
}
}
boolean isIntersect(int x1,int y1, int w1, int h1, int x2, int y2, int w2, int h2)
...{
if( Math.abs(x2-x1) < (w1+w2)/2 && Math.abs(y2-y1) < (h1+h2)/2 )
...{
return true;
}
else
return false;
}
public void keyPressed(int key)
...{
key = Math.abs(key);
System.out.println("key="+key);
switch( key )
...{
case KEY_NUM2:
case KEY_UP:
if( gameState != STATEPLAY )
break;
else
...{
if( aspect_y <= 0 )
...{
aspect_x = 0;
aspect_y = -1;
}
}
break;
case KEY_NUM8:
case KEY_DOWN:
if( gameState != STATEPLAY )
break;
else
...{
if( aspect_y >= 0 )
...{
aspect_x = 0;
aspect_y = +1;
}
}
break;
case KEY_NUM4:
case KEY_LEFT:
if( gameState != STATEPLAY )
break;
else
...{
if( aspect_x <= 0 )
...{
aspect_y = 0;
aspect_x = -1;
}
}
break;
case KEY_NUM6:
case KEY_RIGHT:
if( gameState != STATEPLAY )
break;
else
...{
if( aspect_x >= 0 )
...{
aspect_y = 0;
aspect_x = +1;
}
}
break;
case KEY_FIRE:
case KEY_NUM5:
if( gameState == STATEPLAY )
...{
// addSnake();
// System.out.println("snake_n="+snake_n);
}
break;
case KEY_NUM1:
break;
case KEY_NUM3:
isShowInfo = !isShowInfo;
break;
case KEY_NUM0:
rePlay( level );
break;
}
repaint();
}
private void addSnake(int life)
...{
int s_n = snake_n;
snake_n += life;
if( snake_n >= snake_max )
...{
level++;
rePlay(level);
}
else if( snake_n < snake_min ) //game over
...{
rePlay(level);
}
else if( life>0 )
...{
for( int i=s_n; i<snake_n; i++ )
...{
snake_x[i] = -snake_w;
snake_y[i] = -snake_h;
}
}
}
public void rePlay( int level )
...{
sleepTime = 300-level*20;
map_x = s_height/16;
map_y = s_width/16;
key_x = map_x>>1;
key_y = map_y>>1;
gameState = STATEPLAY;
map = new int[map_x][map_y];
isShowInfo = false;
snake_n = snake_min;
aspect_x = 0;
aspect_y = 0;
try
...{
Image temp = Image.createImage("/pics/bg_tile_"+(level%2)+".png");
Graphics gn;
for( int i=0; i<imgGameBg.length; i++ )
...{
imgGameBg[i] = Image.createImage(16, 16);
gn = imgGameBg[i].getGraphics();
gn.drawImage(temp, -i*16, 0, gn.LEFT|gn.TOP);
}
gn = null;
temp = null;
System.gc();
}catch(Exception e)...{ e.printStackTrace(); }
for( int i=0; i<map_x; i++ ) //draw bg
...{
for( int j=0; j<map_y; j++ )
...{
int r = rand.nextInt(maxRand);
for( int k=0; k<imgGameBg.length; k++ )
...{
if( r < (maxRand>>(k+1)) )
...{
map[i][j] = k;
}
}
}
}
for( int i=0; i<snake_n; i++ ) //init snake
...{
snake_x[i] = s_width >>1;
snake_y[i] = s_height>>1; //-(i*snake_h);
}
}
}