天天看點

JAVA遊戲程式設計之二----j2me MIDlet 手機遊戲入門開發--貪吃蛇

作者:雷神

QQ:38929568

QQ群:28048051JAVA遊戲程式設計(滿) 28047782(将滿)

與前一款掃雷比較,這個遊戲多了一個 類,用來顯示動畫,也是蛇要吃的物品類, 也有了代碼包,圖檔包,結構清晰,代碼量500行,地圖生成的簡單算法,和播放動畫的簡單算法!有矩形碰撞,更新就是增加蛇的移動速度,簡單做法就是把重新整理速度提高了,每次重新整理蛇要移動一個格子,出現的物品有的是增加蛇長度的,有的是減少蛇長度的。

本文給出的是程式代碼,需要圖檔才能編譯程式,

Gif.java 這個類是繪制動畫的類,也是蛇的食物類

package code;  

import javax.microedition.lcdui.Image;  

import javax.microedition.lcdui.Graphics;  

public class Gif   

...{  

    public String     name  = "gif";  

    public int         life  =  1;  

    public int         pos_x =  0;  

    public int         pos_y =  0;  

    public int         w       = 10;  

    public int         h       = 10;  

    public int         form  =  1;  

    private int        allTime;  

    private int     currForm;  

    private long     startTime;  

    public boolean isShow=true;  

    public int[] formTime=...{100};  

    private Image[] img;  

    public Gif( String _name, int _form, int _w, int _h )  

    ...{  

        w     = _w;  

        h     = _h;  

        setGif( _name, _form );  

        startTime = System.currentTimeMillis();  

        setPos();  

    }  

    public void setGif( String _name, int _form )  

        try 

        ...{  

            name     = _name;  

            form     = _form;  

            formTime= new int[form];  

            img        = new Image[form];  

            for( int i=0; i<form; i++ )  

            ...{  

                formTime[i]    =  100;  

                allTime        += formTime[i];  

                img[i]         =  Image.createImage("/pics/"+name+"_"+i+".png");  

            }  

//            Image temp     = Image.createImage("/pics/"+name+".png");  

//            form        = temp.getWidth()/w;  

//            formTime    = new int[form];  

//            img            = new Image[form];  

//            Graphics gn;  

//            for( int i=0; i<img.length; i++ )  

//            {  

//                formTime[i]    = 100;                  

//                allTime        +=formTime[i];      

//                img[i]         = Image.createImage(w, h);  

//                gn             = img[i].getGraphics();      

//                gn.drawImage(temp, -i*w, 0, gn.LEFT|gn.TOP);  

//            }  

//            gn     = null;  

//            temp = null;      

//            System.gc();  

        }catch(Exception e)...{ e.printStackTrace(); }          

    public void setPos()  

        pos_x = cGame.rand.nextInt(cGame.s_width-w)  + ( w>>1 );  

        pos_y = cGame.rand.nextInt(cGame.s_height-h) + ( h>>1 );  

    public void setFormTime(int[] _formTime)  

        formTime = _formTime;  

        allTime  = 0;  

        for( int i=0; i<formTime.length; i++ )  

            allTime    +=formTime[i];  

        }  

    public void paint(Graphics g)  

        if( isShow )  

            long now = System.currentTimeMillis();  

            long time= (now-startTime)%allTime;  

            for(int i=0; i<form; i++)  

                time -= formTime[i];  

                if( time<=0 )  

                ...{  

                    g.drawImage(img[i], pos_x, pos_y, g.HCENTER|g.VCENTER);  

                    break;  

                }  

cGame.java 這個類已經不陌生了, 作用功能和上一個遊戲雷同

////////////////////////////////////////////////////////////////////////////////  

//  

// cGame.java  

// Project: Minesweeper  

// Author(s): Gao Lei  

// Create: 2007-10-12  

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    = 100;  

    private int    snake_n        = 5;  

    private int    gameState     = STATEPLAY;  

    private int level        = 1;  

    private long sleepTime    =160;  

    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() );  

            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  

            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();  

        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.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_n; 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);  

            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();  

                            dung[i].pos_x,    dung[i].pos_y,    dung[i].w,     dung[i].h )   

                addSnake( dung[i].life );  

                dung[i].setPos();  

        if( 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.isShow =false;  

            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 )      

                else 

                    if( aspect_y <= 0)  

                    ...{  

                        aspect_x =  0;  

                        aspect_y = -1;  

                    }  

            break;  

            case KEY_NUM8:  

            case KEY_DOWN:  

                if( gameState != STATEPLAY )  

                    if( aspect_y >= 0)  

                        aspect_y = +1;  

            case KEY_NUM4:  

            case KEY_LEFT:  

                    if( aspect_x <= 0)  

                        aspect_y =  0;  

                        aspect_x = -1;  

            case KEY_NUM6:  

            case KEY_RIGHT:  

                    if( aspect_x >= 0)  

                        aspect_x = +1;  

            case KEY_FIRE:  

            case KEY_NUM5:  

                if( gameState == STATEPLAY )  

//                    addSnake();  

//                    System.out.println("snake_n="+snake_n);  

            case KEY_NUM1:          

            case KEY_NUM3:          

                isShowInfo = !isShowInfo;  

            case KEY_NUM0:          

                rePlay( level );          

        this.repaint();  

    private void addSnake(int life)  

        snake_n += life;  

        if( snake_n > snake_max )  

            snake_n = snake_max;  

            level++;  

        if( snake_n < snake_n )        //game over  

            snake_n = snake_n;  

    public void rePlay( int level )  

        sleepTime     = 150-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        = 5;  

        aspect_x    = 0;  

        aspect_y    = 0;  

        for( int i=0; i<map_x; i++ )    //draw bg  

                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);  

Snake.java這個類是之前的 Minesweeper.java這個類 基本上就該個名字,其他什麼都不用改!

///////////////////////////////////////////////////////////////////////////////////////////////////  

// Snake.java  

// Project: Snake  

// Create: 2007-10-08  

package code;                          

import javax.microedition.midlet.*;      

import javax.microedition.lcdui.*;      

public class Snake extends MIDlet   

    static Snake s_midlet;  

    static Display s_display = null;  

    static cGame s_game = null;      

    public Snake()  

        s_midlet = this;  

    public void startApp()               

        if (s_display == null)   

            s_display = Display.getDisplay(this);  

        if (s_game == null)   

            s_game = new cGame();  

            s_display.setCurrent(s_game);  

        }   

        else   

    public void pauseApp()           

    public void destroyApp(boolean unconditional)   

        notifyDestroyed();  

這次代碼上的注釋比較少的原因是我的IDE不支援中文輸入!是以即便是加注釋也是英文的!就沒加了

大多數函數方法和上個遊戲沒多大變化!

<a href="http://down.51cto.com/data/2358279" target="_blank">附件:http://down.51cto.com/data/2358279</a>

本文轉自 kome2000 51CTO部落格,原文連結:http://blog.51cto.com/kome2000/578507