Use Greenfoot to make a simple game-memory flop game (2)

If you haven't read the first part, click here to
remember the flop game (1)


Continue from the previous part, today I will talk about how to achieve the flop.

Let's take a look at the final result first

Insert picture description here

The first part has completed the initial interface of the flop game. as the picture shows

Insert picture description here

For the function of realizing the flop effect, the mouse click event needs to be operated, which can be realized by the mouseClicked() method provided by the Greenfoot class.

Add the following code in the Card class

if (Greenfoot.mouseClicked(this)) {
    
       //如果鼠标单击了这张牌
	if (!isFaceUp) {
    
                      //如果扑克牌背面朝上
		setImage(faceUpImage);        //将扑克牌翻至正面朝上
		isFaceUp = true;
	}
}



Complete code of the improved Card class

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 扑克牌类,可以被翻开
 */
public class Card extends Actor{
    
    
    private int value = -1;    //初始点数为-1,表示还没有生成确定的扑克牌。一旦生成了一张牌,其点数就不为-1
    private boolean isFaceUp = false;	//isFaceUp=true,则牌正面朝上,否则背面朝上
    private GreenfootImage faceUpImage = null;//faceUpImage表示牌的正面图案文件
    private GreenfootImage faceDownImage = null;//faceDownImage表示牌的背面面图案文件

    //Card类的构造方法
    public Card(int cardValue) {
    
    //cardValue是构造一张Card对象时传入的牌的点数
        value = cardValue;
        isFaceUp = false;	//所有被构造的牌都是背面朝上的
        String fileName = "hearts" + value + ".png";  //根据牌点数匹配的正面图案文件名
        //生成牌的正面图像对象
        faceUpImage = new GreenfootImage(fileName.toLowerCase());
        faceDownImage = new GreenfootImage("blueflip.png");	//生成牌的背面图案对象
        setImage(faceDownImage);	//让牌背面朝上放在牌桌上
    }

    //act()方法是游戏单步执行的动作
    public void act() {
    
    
        // 如果鼠标点击了这张牌
        if (Greenfoot.mouseClicked(this) ){
    
    
            // 如果牌面朝下,就翻牌。 
            if (!isFaceUp) {
    
             
                setImage(faceUpImage);
                isFaceUp = true;
            }
        }
    }

    //获取这张牌的点数
    public int getValue(){
    
    
        return value;
    }

    //获取这张牌是否已翻面
    public boolean getFaceup(){
    
    
        return isFaceUp;
    }

    //将牌翻成背部朝上
    public void turnFaceDown(){
    
    
        isFaceUp = false;
        setImage(faceDownImage);

    }

}

So far, the flop function has been realized.

Guess you like

Origin blog.csdn.net/weixin_44892517/article/details/112757103