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

📄 gamecanvas.java~160~

📁 扑克牌游戏
💻 JAVA~160~
字号:
package game03;

import java.util.Random;

import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;

/**
 * <p>Title:扑克牌游戏 </p>
 *状态:完成
 * 完成日期:2005.10.18
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class GameCanvas extends Canvas implements Runnable {
    GameCanvas my;
    public  int Minute = 5;
    public  int Second = 60;
    Timer timer = new Timer();
    Task task;
    Random rnd = new Random();
    Image off = null;
    Font font;
    private Image gameover;
    protected static final byte GAME_TITLE = 0;
    protected static final byte GAME_START = -1;
    protected static final byte GAME_PLAYING = 1;
    protected static final byte GAME_FINASH = 3;
    protected static final byte GAME_END = 2;
    protected static final int COL = 4;
    protected static final int ROW = 4;
    protected static final int WIDTH = 30;
    protected static final int HEIGHT = 35;
    byte gamestate;
    int x, y;
    Thread thread = null;
    String str = "16张牌(数字扑克)";
    protected short[] picer = new short[] {
                              1, 2, 3, 4,
                              5, 6, 7, 8,
                              9, 10, 11, 12,
                              13, 14, 15, -1};

    public GameCanvas() {
        my=this;
        task = new Task(my);
        if (!this.isDoubleBuffered()) {
            off = Image.createImage(this.getWidth(), this.getHeight());
        }
        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
        try {
            gameover = Image.createImage("/pic/game.png");

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

        gamestate = GAME_TITLE;
        if (thread == null) {
            thread = new Thread(this);
        }
        thread.start();
    }

    public void showNotify() {
        timer.schedule(task, 0, 1000);
    }

    public void hideNotify() {
        timer.cancel();
    }

    public void keyPressed(int key) {
        switch (this.getGameAction(key)) {
        case RIGHT:
            doRight();
            break;
        case LEFT:
            doLeft();
            break;
        case DOWN:
            doDown();
            break;
        case UP:
            doUp();
            break;
        }
        // repaint();
    }

    void doDown() {
        if ((y + 1) % ROW == 0) {
            return;
        } else if (picer[(x % ROW + y * ROW) + ROW] == -1) {
            picer[(x % ROW + y * ROW) + ROW] = picer[x % ROW + y * ROW];
            picer[x % ROW + y * ROW] = -1;
        }
        y += 1;

    }

    void doUp() {
        if (y % ROW == 0) {
            return;
        }
        if (picer[(x % ROW + y * ROW) - ROW] == -1) {
            picer[(x % ROW + y * ROW) - ROW] = picer[x % ROW + y * ROW];
            picer[x % ROW + y * ROW] = -1;
        }
        y -= 1;

    }

    void doRight() {
        if ((x + 1) % ROW == 0) {
            return;
        } else if (picer[(x % ROW + y * ROW) + 1] == -1) {
            picer[(x % ROW + y * ROW) + 1] = picer[x % ROW + y * ROW];
            picer[x % ROW + y * ROW] = -1;
        }

        x += 1;
    }

    void doLeft() {
        if (x % ROW == 0) {
            return;
        } else if (picer[(x % ROW + y * ROW) - 1] == -1) {
            picer[(x % ROW + y * ROW) - 1] = picer[x % ROW + y * ROW];
            picer[x % ROW + y * ROW] = -1;
        }
        x -= 1;
    }

    protected void paint(Graphics g) {
        Graphics old = g;
        if (off != null) {
            g = off.getGraphics();
        }
        g.setColor(0xffffff);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        switch (gamestate) {
        case GAME_TITLE:
            g.setFont(font);
            g.setColor(0xff0000);
            g.drawString(str, this.getWidth() / 2 - 50, this.getHeight() / 2,
                         20);
            break;
        case GAME_START:
        case GAME_PLAYING:
            g.setColor(0x000000);
            g.drawString("" +Integer.toString(Minute) + ":" + Integer.toString(Second),
                         120, 20, 20);
            g.setFont(Font.getDefaultFont());
            g.setColor(0x0000ff);
            g.drawRoundRect(25, 45, 190, 210, 45, 45);
            g.drawRoundRect(30, 50, 180, 200, 45, 45);
            g.setColor(0xffff00);
            g.fillRoundRect(45 + x * (WIDTH + 10), 65 + y * (HEIGHT + 10),
                            WIDTH, HEIGHT, 10, 10);
            for (int i = 0; i < COL; i++) {
                for (int j = 0; j < ROW; j++) {
                    if (picer[(i * ROW) + j] != -1) {
                        switch ((picer[(i * ROW) + j] - 1) / ROW) {
                        case 0:
                            g.setColor(0xff0000);
                            g.drawRoundRect(45 + j * (WIDTH + 10),
                                            65 + i * (HEIGHT + 10),
                                            WIDTH, HEIGHT, 10, 10);
                            break;
                        default:
                            g.setColor(0xff0000);
                            g.drawRoundRect(45 + j * (WIDTH + 10),
                                            65 + i * (HEIGHT + 10),
                                            WIDTH, HEIGHT, 10, 10);
                            break;
                        }
                        g.setColor(0x000000);
                        g.setFont(font);
                        g.drawString("" + picer[(i * ROW) + j],
                                     45 + j * (WIDTH + 10) + 10,
                                     65 + i * (HEIGHT + 10) + 10, 20);

                    } else { //draw -1
                        g.setColor(0xff0000);
                        g.drawRoundRect(45 + j * (WIDTH + 10),
                                        65 + i * (HEIGHT + 10),
                                        WIDTH, HEIGHT, 10, 10);

                    }

                }
            }

            break;
        case GAME_FINASH:
            g.setColor(0xff0000);
            g.setFont(font);
            g.drawString("finish", this.getWidth() / 2, this.getHeight() / 2,
                         20);
            break;
        case GAME_END:
            g.drawImage(gameover, this.getWidth() / 2, this.getHeight() / 2,
                        Graphics.HCENTER | Graphics.VCENTER);
            break;
        }
        if (old != g) {
            g.drawImage(off, 0, 0, 20);
        }

    }

    void newP() {
        short temp;
        short t;
        for (int i = 0; i < picer.length / 2; i++) {
            t = (short) rnd.nextInt(picer.length - 1);
            temp = picer[i];
            picer[i] = picer[t];
            picer[t] = temp;
        }
    }

    void doClearCheck() { //过关检查
        boolean isClear;
        short length = (short) (picer.length - 1);
        if (picer[length] != -1) {
            return;
        }
        for (int i = 0; i < length; i++) {
            if (picer[i] != i + 1) {
                return;
            }
        }
        gamestate = GAME_FINASH;
    }

    public void run() {
        while (thread != null) {
            switch (gamestate) {
            case GAME_TITLE:
                try {
                    Thread.sleep(2000);
                    gamestate = GAME_START;
                } catch (Exception e) {}
                break
                        ;
            case GAME_START:
                try {
                    Thread.sleep(2000);
                    gamestate = GAME_PLAYING;
                    newP();
                } catch (Exception e) {}
                break
                        ;
            case GAME_PLAYING:
//                try {
//                    Thread.sleep(40);
                    doClearCheck();
//                } catch (Exception e) {}
                break
                        ;
            case GAME_FINASH:
                try {
                    Thread.sleep(1000);
                    newP();
                    gamestate = GAME_PLAYING;
                } catch (Exception e) {}
                break
                        ;
            case GAME_END:
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {}
                break
                        ;
            }
            repaint();
        }
    }
}


class Task extends TimerTask {
    GameCanvas my;
 public Task(GameCanvas my){
     this.my=my;
 }
    public void run() {
        while (true) {
            my.Second--;
            if (my.Second == 0) {
                my.Minute--;
                my.Second = 60;
            }
        }
    }
}

⌨️ 快捷键说明

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