0

我想用java做一个游戏。该游戏是记忆游戏,游戏开始时显示所有卡片,然后单击时翻转所有卡片,然后每次单击它一次显示一张卡片,这样就可以了。

  1. 如果 2 张卡相同,则保持可见
  2. 如果两张卡不相同,它们会翻转

我只有 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;
}
}
4

1 回答 1

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);
    }
于 2013-11-01T11:50:21.800 回答