gpt4 book ai didi

Java内存游戏如何翻转单张牌?

转载 作者:行者123 更新时间:2023-12-01 18:41:35 26 4
gpt4 key购买 nike

当我点击单个卡片时,我试图翻转它们。当我单击时,所有卡片同时翻转。

这是我的代码。如有任何帮助,我们将不胜感激,并提前非常感谢您。

package cards;

import processing.core.PApplet;

public class MemoryGame extends PApplet {

static final int COLS=2;
static final int RAWS=3;
Card[] cards;

public void setup() {
size(RAWS*Card.WIDTH, COLS*Card.HEIGHT); // card witdth 60*9+(10*9) for the gaps=630
cards = new Card[COLS*RAWS];
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].flip();
}
}

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,y;

Card(int _v, float _x, float _y) {

value = _v;
x = _x;
y = _y;
}

public void display(PApplet applet) {
applet.stroke(0);
applet.strokeWeight(2);
if (shown) {
applet.fill(100);
applet.rect(x, y, WIDTH, HEIGHT,WIDTH/5);
applet.textAlign(applet.CENTER,applet.CENTER);
applet.textSize(WIDTH/2);
applet.fill(0);
applet.text(value,x+WIDTH/2,y+HEIGHT/2);
} else {
applet.fill(255);
applet.rect(x, y, WIDTH, HEIGHT,WIDTH/5);
}
}



public void flip() {
shown=!shown;
}
}

最佳答案

按下鼠标按钮时,您将翻转所有卡片。你必须检查鼠标位置是否与卡片相交并且鼠标被按下,如果是,则翻转该卡片。创建 mouseX() 和 mouseY() 方法,为您提供鼠标坐标。只需添加 if(鼠标位置与卡片相交):

    public void mousePressed() {
for (int i = 0; i < 6; i++) {
if(mouseX() >= cards[i].x && mouseX() <= cards[i].x+cards[i].width && mouseY() >= cards[i].y && mouseY() <= cards[i].y+card[i].height) {
cards[i].flip();
}
}
}

关于Java内存游戏如何翻转单张牌?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19765252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com