Thứ Năm, 16 tháng 5, 2013

game tính nhẩm

game này chỉ là một phần nhỏ, trong đây người chơi sẽ xác định xem biều thức tính toán là đúng hay sai, game dùng hình ảnh để hiện thị những con số.
và game bao gồm: một class dùng vẽ chuỗi các phép tính:

C:\Users\thanhliem\Documents\NetBeansProjects\mathgame\src\font.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class font {
    //chuỗi kí tự dùng trong hình
    String c = "=+-0123456789";
//vị trí bắt đầu của từng kí tự trong hình
    int[] pos = {0, 15, 34, 47, 67, 80, 97, 114, 133, 150, 169, 186, 205, 222};
    Image im;

    public font() {
        try {
            im = Image.createImage("/math.png");
        } catch (Exception e) {
        }
    }
//hàm vẽ chuỗi
    void drawString(Graphics g, int x, int y, String ch) {
        for (int i = 0; i < ch.length(); i++) {
            int cp = c.indexOf(ch.charAt(i));
            drawChar(g, x, y, cp);
            x += pos[cp + 1] - pos[cp];
        }
    }
//hàm vẽ kí tự
    void drawChar(Graphics g, int x, int y, int c) {
        g.drawRegion(im, pos[c], 0, pos[c + 1] - pos[c], im.getHeight(), 0, x, y, 0);
    }
}
-->
và một class xử lí game, sự kiện người dùng:
C:\Users\thanhliem\Documents\NetBeansProjects\mathgame\src\mathgame.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class mathgame extends Canvas implements Runnable {

    int w, h, count = 10, score = 0;
    Random rd = new Random();
    int k;
    font f = new font();
    int mode = 0;
    int a, b, c, o;
    String dis, dau, ok;
    boolean t;

    public mathgame() {
        setFullScreenMode(true);
        w = getWidth();
        h = getHeight();
        general();
    }

    public void paint(Graphics g) {
        //check xem chọn đúng hay sai
        if (mode == 0) {
            if (k == -6) {
                if (!t) {
                    ok = "ok";
                    score++;
                } else {
                    ok = "bad";
                }
                mode = 1;
            }
            if (k == -7) {
                if (t) {
                    ok = "ok";
                    score++;
                } else {
                    ok = "bad";
                }
                mode = 1;
            }
            k = 0;
        }
        g.setColor(0);
        g.fillRect(0, 0, w, h);
        g.setColor(0xf0f0);
        switch (mode) {
            case 0: {//xem xét phép toán
                f.drawString(g, 5, w / 2, dis);
                g.drawString("còn lại " + count + " câu", 0, 40, Graphics.LEFT | Graphics.BOTTOM);
                g.drawString("sai", 0, h, Graphics.LEFT | Graphics.BOTTOM);
                g.drawString("đúng", w, h, Graphics.RIGHT | Graphics.BOTTOM);
            }
            break;
            case 1: {//hiện thị kết quả chọn
                g.drawString(ok, 0, h / 2, Graphics.LEFT | Graphics.BOTTOM);
                if (count > 0) {
                    general();
                    mode = 0;
                } else {
                    mode = 2;
                }
                slept(2500);
            }
            break;
            case 2: {
                g.drawString("Điểm " + score, 0, h / 2, Graphics.LEFT | Graphics.BOTTOM);
                slept(1000);
            }
            break;
        }
    }
    
    public void run(){
    while(true){
    repaint();}}
//hàm tổng hợp bài toán tiếp theo.
    void general() {
        count--;
        a = hai();
        o = hai() % 2;
        b = hai();
        if (o == 0) {
            dau = "+";
            c = a + b;
        } else {
            dau = "-";
            c = a - b;
        }
        t = hai() % 2 == 0 ? true : false;
        if (t) {
            dis = a + dau + b + "=" + c;
        } else {
            dis = a + dau + b + "=" + (c + hai() % 8 + 1);
        }
    }
//hàm dừng lại một khoảng thời gian
    void slept(long t) {
        try {
            Thread.sleep(t);
        } catch (Exception e) {
        }
    }
//hàm lấy về số nguyên 2 chữ số

    int hai() {
        slept(20);
        return Math.abs(rd.nextInt()) % 99;
    }
//hàm nhận biết phím bấm

    public void keyPressed(int k) {
        this.k = k;
    }
}

--> cuối cùng là một Midlet để hiện thị:
C:\Users\thanhliem\Documents\NetBeansProjects\mathgame\src\Midlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Midlet extends MIDlet {
    public void startApp() {
        mathgame mg=new mathgame();
        new Thread(mg).start();
    Display.getDisplay(this).setCurrent(mg);}

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

--> hình ảnh dùng trong game làm font số là:

source

Không có nhận xét nào: