⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pangxie.java

📁 为解压缩文件
💻 JAVA
字号:
package pangxie;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.util.Timer;


class PangXie {
    public static Image[][] images;
    public static int total = 0;
    int _y = 0;
    int x, y;
    int type; //type==0的螃蟹会走斜线
    private int dir; //表方向 -1:left ,1:right,2:up,3:right_up,4:left_up,5:down
    int rate = 3;
    byte frame;
    Timer timer;
    GameCanvas gc;
    Flower deadFlower;


    public static final int addscore[] = {3, 4, 5};
    int crashX, crashY;
    public static final int Left_X = -38;
    public static final int Right_X = 202;
    public static final int Base_Y = 150;
    //方向常量
    public static final int Dir_Left = -1;
    public static final int Dir_Right = 1;
    public static final int Dir_Up = 2;
    public static final int Dir_RightUp = 3;
    public static final int Dir_LeftUp = 4;
    public static final int Dir_Down = 5;
    public static final int[] BasePointX = {37, 82, 127};


    static {
        images = new Image[5][2];
        int i = 0, k = 0;
        try {
            for (i = 0; i < 5; i++) {
                for (k = 0; k < 2; k++) {
                    images[i][k] = Image.createImage("/res/p" + i + k + ".png");
                }
            }
        } catch (Exception ex) {
        }
    }

    public PangXie(int tt, int x, int y, int dir, GameCanvas gc) {
        this.x = x;
        this.y = y;
        this.type = tt;
        if (this.type == 4) {
            rate = 5;
        }
        this.dir = dir;
        this.gc = gc;
    }

    public void paint(Graphics g) {

        g.drawImage(images[type][frame], x, y + 31,
                    Graphics.LEFT | Graphics.TOP);
        if (frame == 0) {
            frame = 1;
        } else {
            frame = 0;
        }

        if (this.crashX != 0 && this.crashY != 0) {
            if (type == 0) {
                g.drawString("+" + addscore[1], this.crashX, this.crashY,
                             Graphics.HCENTER | Graphics.TOP);
            } else if (type == 4) {
                g.drawString("+" + addscore[2], this.crashX, this.crashY,
                             Graphics.HCENTER | Graphics.TOP);
            } else {
                g.drawString("+" + addscore[0], this.crashX, this.crashY,
                             Graphics.HCENTER | Graphics.TOP);
            }
        }

    }


    public void crawl() {

        //改变方向的代码
        if (dir != PangXie.Dir_Down) {
            if (x == BasePointX[0]) {
                switch (dir) {
                case PangXie.Dir_Left:
                    if (type == 0) {
                        if (gc.man.logicPos == 0) {
                            dir = PangXie.Dir_Up;
                        } else {
                            //随机决定是向右上还是向上

                            dir = GameLogic.random.nextInt() % 2 == 0 ?
                                  PangXie.Dir_RightUp : PangXie.Dir_Right;

                        }
                    } else {
                        dir = PangXie.Dir_Up;
                    }
                    break;
                case PangXie.Dir_Right:
                    if (type == 0) {
                        if (gc.man.logicPos == 0) {
                            dir = PangXie.Dir_RightUp;
                        } else {
                            //随机决定是向右还是向上

                            dir = GameLogic.random.nextInt() % 2 == 0 ?
                                  PangXie.Dir_RightUp : PangXie.Dir_Right;
                        }
                    } else {
                        //随机决定是向上还是继续向右

                        dir = GameLogic.random.nextInt() % 2 == 0 ?
                              PangXie.Dir_Up : PangXie.Dir_Right;
                    }
                    break;

                case PangXie.Dir_Up:
                case PangXie.Dir_LeftUp:
                    if (type == 0) {
                        if (gc.man.logicPos == 0) {
                            dir = PangXie.Dir_RightUp;
                        } else {
                            dir = PangXie.Dir_Up;
                        }
                    }
                    break;

                }
            } else if (x == BasePointX[1]) {
                switch (dir) {
                case PangXie.Dir_Left:
                    if (type == 0) {
                        if (gc.man.logicPos == 0) {
                            dir = PangXie.Dir_Up;
                        } else if (gc.man.logicPos == 1) {
                            dir = PangXie.Dir_RightUp;
                        } else {
                            dir = PangXie.Dir_LeftUp;
                        }
                    } else {
                        //随机决定是向上还是继续向左
                        dir = GameLogic.random.nextInt() % 2 == 0 ?
                              PangXie.Dir_Up : PangXie.Dir_Left;
                    }
                    break;
                case PangXie.Dir_Right:
                    if (type == 0) {
                        if (gc.man.logicPos == 0) {
                            dir = PangXie.Dir_RightUp;
                        } else if (gc.man.logicPos == 1) {
                            dir = PangXie.Dir_Up;
                        } else {
                            dir = PangXie.Dir_Up;
                        }
                    } else {
                        //随机决定是向上还是继续向右

                        dir = GameLogic.random.nextInt() % 2 == 0 ?
                              PangXie.Dir_Up : PangXie.Dir_Right;
                    }
                    break;

                case PangXie.Dir_Up:
                case PangXie.Dir_RightUp:
                case PangXie.Dir_LeftUp:
                    if (type == 0) {
                        if (gc.man.logicPos == 2) {

                            dir = PangXie.Dir_LeftUp;
                        }
                        if (gc.man.logicPos == 0) {
                            dir = PangXie.Dir_Up;
                        }
                    }
                    break;

                }

            } else if (x == BasePointX[2]) {
                switch (dir) {
                case PangXie.Dir_Left:
                    if (type == 0) {
                        if (gc.man.logicPos == 2) {
                            dir = PangXie.Dir_LeftUp;
                        } else {
                            //随机决定是向左上还是继续向左

                            dir = GameLogic.random.nextInt() % 2 == 0 ?
                                  PangXie.Dir_Up : PangXie.Dir_LeftUp;

                        }
                    } else {
                        //随机决定是向左还是上
                        dir = GameLogic.random.nextInt() % 2 == 0 ?
                              PangXie.Dir_Up : PangXie.Dir_Left;
                    }
                    break;
                case PangXie.Dir_Right:
                    if (type == 0) {
                        if (gc.man.logicPos == 2) {
                            dir = PangXie.Dir_Up;
                        } else {
                            //随机决定是向左上还是向上

                            dir = GameLogic.random.nextInt() % 2 == 0 ?
                                  PangXie.Dir_LeftUp : PangXie.Dir_Up;
                        }
                    } else {
                        dir = PangXie.Dir_Up;
                    }
                    break;

                case PangXie.Dir_Up:
                case PangXie.Dir_RightUp:
                    if (type == 0) {
                        if (gc.man.logicPos != 2) {
                            dir = PangXie.Dir_LeftUp;
                        } else {
                            dir = Dir_Up;
                        }
                    }
                    break;

                }

            }
        }

        switch (dir) {
        case PangXie.Dir_Left:
            x -= rate;
            break;
        case PangXie.Dir_LeftUp:
            x -= rate;
            y -= rate - 1;
            break;
        case PangXie.Dir_Right:
            x += rate;
            break;
        case PangXie.Dir_RightUp:
            x += rate;
            y -= rate - 1;
            break;
        case PangXie.Dir_Up:
            y -= rate;
            break;
        case PangXie.Dir_Down:
            y += deadFlower.rate;
            break;

        }

        //分数上升
        if (this.crashX != 0 && this.crashY != 0) {
            crashY -= 2;
        }

        if (y < 60) {
            gc.logic.gameOver();
        }
        gc.repaint();

    }

    void fall() {
        dir = PangXie.Dir_Down;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -