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

📄 cgame.java~35~

📁 手机游戏贪吃蛇源码
💻 JAVA~35~
字号:
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import java.util.*;
import javax.microedition.media.control.*;



/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class CGame extends Canvas implements Runnable, Constance {
    int keyPressed;
    int newnodeX;
    int newnodeY;
    int tickcount;
    Player currentSound;
    Player currentSFX;
    Player p_bg;
    Player p_eat;
    Player p_die;
    Player p_down;
    Player p_up;
    int[] nodeX = new int[MAX_NODE];
    int[] nodeY = new int[MAX_NODE];
    int currentNode = 0;
    int direction;
    int volume = 50;

    Random Ran = new Random();
    public CGame() {
        try {
            setFullScreenMode(true);
            randomXY();
            p_bg = Manager.createPlayer("".getClass().getResourceAsStream(
                    "/bg.mid"), "audio/midi");
            p_eat = Manager.createPlayer("".getClass().getResourceAsStream(
                    "/eat.wav"), "audio/x-wav");
            p_die = Manager.createPlayer("".getClass().getResourceAsStream(
                    "/die.wav"), "audio/x-wav");
            p_down = Manager.createPlayer("".getClass().getResourceAsStream(
                    "/eat.wav"), "audio/x-wav");
            p_up = Manager.createPlayer("".getClass().getResourceAsStream(
                    "/eat.wav"), "audio/x-wav");

            snd_play(p_bg, -1);
            (new Thread(this)).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void snd_play(Player p, int loop){
        try{
            if(currentSound != null)
                currentSound.stop();

            currentSound = p;
            currentSound.realize();
            currentSound.prefetch();

            VolumeControl  vol = (VolumeControl) currentSound.getControl("VolumeControl");
            vol.setLevel(volume);
            currentSound.setLoopCount(loop);
            currentSound.start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    void sfx_play(Player p, int loop){
        try{
            if(currentSFX != null)
                currentSFX.stop();

            currentSFX = p;
            currentSFX.realize();
            currentSFX.prefetch();

            VolumeControl  vol = (VolumeControl) currentSFX.getControl("VolumeControl");
            vol.setLevel(volume);
            currentSFX.setLoopCount(loop);
            currentSFX.start();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    void randomXY() {
        newnodeX = Math.abs(Ran.nextInt() % (SCR_WIDTH / 5)) * 5;
        newnodeY = Math.abs(Ran.nextInt() % (SCR_HEIGHT / 5)) * 5;
    }

    public void paint(Graphics g) {
        updateSnake();
        updateVolume();

        drawBG(g);
        drawSnake(g);
        drawNewNode(g);
        drawVolume(g);

        keyPressed = 0;
    }

    void drawBG(Graphics g) {
        g.setColor(0x00ff00);
        g.fillRect(0, 0, SCR_WIDTH, SCR_HEIGHT);
        g.setColor(0xffffff);
        g.fillRect(0, SCR_HEIGHT, SCR_WIDTH, SCR_HEIGHT);
    }

    void drawSnake(Graphics g) {
        for (int i = 0; i <= currentNode; i++) {
            g.setColor(0);
            g.fillRect(nodeX[i], nodeY[i], NODE_LENGTH, NODE_LENGTH);
        }
    }

    void drawNewNode(Graphics g) {
        if (tickcount % 6 < 3){
            g.setColor(0);
            g.fillRect(newnodeX, newnodeY, NODE_LENGTH, NODE_LENGTH);
        }
    }

    void drawVolume(Graphics g){
        for(int i = 1;i < 11;i++){
            if(i * 10 <= volume){
                g.setColor(0xff0000);
                g.fillRect((SCR_WIDTH - 100) / 2 + i * 10, SCR_HEIGHT + 80 - i * 3, 5, i * 5);
            }
            g.setColor(0);
            g.drawRect((SCR_WIDTH - 100) / 2 + i * 10, SCR_HEIGHT + 80 - i * 3, 5, i * 5);
        }
    }

    void updateSnake() {
        try{
            rearrangeSnake();
            switch (direction) {
            case DIR_RIGHT:
                nodeX[0] += NODE_LENGTH;
                break;
            case DIR_LEFT:
                nodeX[0] -= NODE_LENGTH;
                break;
            case DIR_UP:
                nodeY[0] -= NODE_LENGTH;
                break;
            case DIR_DOWN:
                nodeY[0] += NODE_LENGTH;
                break;
            }

            if (hitMyself() || nodeX[0] < 0 || nodeX[0] > SCR_WIDTH ||
                nodeY[0] < 0 ||
                nodeY[0] > SCR_HEIGHT)
                die();

            changeDir(keyPressed);

            if (getNextX() == newnodeX && getNextY() == newnodeY) {
                sfx_play(p_eat, 1);
                addNode(newnodeX, newnodeY);
                randomXY();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    void updateVolume(){
        if(keyPressed == KEY_CODE_NUM1){
            volume -= 10;
            sfx_play(p_down, 1);
        }
        if(keyPressed == KEY_CODE_NUM3){
            volume += 10;
            sfx_play(p_up, 1);
        }
        if(volume < 0)
            volume = 0;
        if(volume > 100)
            volume = 100;

        if(currentSound != null){
            VolumeControl vol = (VolumeControl) currentSound.getControl("VolumeControl");
            vol.setLevel(volume);
        }
    }

    void rearrangeSnake() {
        for (int i = currentNode; i > 0; i--) {
            nodeX[i] = nodeX[i - 1];
            nodeY[i] = nodeY[i - 1];
        }
    }

    void changeDir(int key) {
        switch (key) {
        case KEY_CODE_RIGHT:
            direction = DIR_RIGHT;
            break;
        case KEY_CODE_LEFT:
            direction = DIR_LEFT;
            break;
        case KEY_CODE_UP:
            direction = DIR_UP;
            break;
        case KEY_CODE_DOWN:
            direction = DIR_DOWN;
            break;
        }
    }

    void addNode(int x, int y) {
        currentNode++;
        if (currentNode == MAX_NODE)
            currentNode = MAX_NODE - 1;
        rearrangeSnake();

        nodeX[0] = x;
        nodeY[0] = y;
    }

    int getNextX() {
        switch (direction) {
        case DIR_RIGHT:
            return nodeX[0] + NODE_LENGTH;
        case DIR_LEFT:
            return nodeX[0] - NODE_LENGTH;
        default:
            return nodeX[0];
        }
    }

    int getNextY() {
        switch (direction) {
        case DIR_UP:
            return nodeY[0] - NODE_LENGTH;
        case DIR_DOWN:
            return nodeY[0] + NODE_LENGTH;
        default:
            return nodeY[0];
        }
    }

    boolean hitMyself() {
        for (int i = 1; i < currentNode; i++) {
            if (nodeX[0] == nodeX[i] && nodeY[0] == nodeY[i])
                return true;
        }
        return false;
    }

    void die() {
        try{
            sfx_play(p_die, 1);
            currentNode = 0;
            nodeX[0] = 0;
            nodeY[0] = 0;
            direction = -1;
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            while (true) {
                Thread.sleep(100);
                repaint();
                tickcount++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void keyPressed(int code) {
        keyPressed = code;
    }
}

⌨️ 快捷键说明

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