Java记忆游戏

时间:2013-11-01 11:44:09

标签: java

我想用java制作游戏。游戏是记忆游戏,在游戏开始时显示所有牌,然后点击时翻转所有牌,然后每次点击它一次显示一张牌。

  1. 如果2张牌相同则仍然可见
  2. 如果两张牌不一样,他们会翻回来
  3. 我只有2周大的Java观点,所以请尽量保持清晰。

    这是我实现了这样的票价而且当我点击时我被卡住翻了。

    我使用处理库。任何帮助,将不胜感激。

    package cards;
    import processing.core.PApplet;
    public class MemoryGame extends PApplet {
    
    Card[] cards;
    
    public void setup() {
        size(1260, 600); 
        cards = new Card[6];
        cards[0] = new Card(11, 0, 0);
        cards[1] = new Card(3, Card.WIDTH, 0);
        cards[2] = new Card(7, 2 * Card.WIDTH, 0);
        cards[3] = new Card(3, 0, Card.HEIGHT);
        cards[4] = new Card(7, Card.WIDTH, Card.HEIGHT);
        cards[5] = new Card(11, 2 * Card.WIDTH, Card.HEIGHT);
    
    }
    
    public void draw() {
        background(204);
        for (int i = 0; i < 6; i++) {
            cards[i].display(this);
        }
    }
    
    public void mousePressed() {
             for (int i = 0; i < 6; i++) {
            cards[i].display(this);
        }
                 }
    
    public static void main(String[] args) {
        PApplet.main("cards.MemoryGame");
    }
    }    
    

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++

    package cards;
    
    import processing.core.PApplet;
    
    public class Card {
    
    boolean shown=false;
    static final int WIDTH = 120;
    static final int HEIGHT = 180;
    static final int gap = 20;
    int value;
    float x;
    float y;
    
    Card(int _v, float _x, float _y) {
    
        value = _v;
        x = _x;
        y = _y;
    }
    
    public void display(PApplet applet) {
        if (shown) {
            applet.fill(0);
            applet.rect(x, y, WIDTH, HEIGHT);
        } else {
            applet.fill(255);
            applet.rect(x, y, WIDTH, HEIGHT);
        }
    }
    
    public void flip() {
        shown=!shown;
    }
    }
    

1 个答案:

答案 0 :(得分:1)

将mousePressed()更改为:

public void mousePressed() {
         for (int i = 0; i < 6; i++) {
        //first flip cards
        cards[i].flip();

        //then display them
        cards[i].display(this);
    }