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

The first two articles
remember the flop game (1) the
memory flop game (2)


continue. For the flop effect in the previous article, although the mouse clicks the flop, it can be found that the poker cards have been opened. , Is still placed on the card table and has not been removed.
This article comes to achieve the removal effect.

Let's take a look at the final result first.

Insert picture description here
In the previous article, we can already click the flop with the mouse. To achieve the removal effect, we need to check the matching of the playing cards.
The basic idea is as follows:

  • Traverse each playing card in the set of playing cards. If the card is revealed first, record the number of cards at that time, and if the second card is revealed, also record the number of cards;
  • Compare the points of the first card with the points of the second card. If the points are the same, remove the two cards; if they are different, restore the two cards to their original state (that is, the back is facing up).

Add the following pairing check code in the act() method of the Table class

  • First define the required variables
Card card1 = null, card2 = null;  //用来保存两张牌的对象
int count = 0;                    //表示牌桌上被翻开的是第几张牌
int card1Value = 0,card2Vlaue = 0;//记录两张牌的点数
  • Next, loop through the set of playing cards, the
    overall frame
for (int i=0; i< cards.size(); i++) {
    
    	//用for循环遍历集合cards中的所有牌
            if (cards.get(i).getFaceup() == true) {
    
    	//如果遍历到的这张牌是翻开的
                count++;		//用count将牌桌上翻开的牌数累加
                if (count == 1) {
    
    			//如果是第一张翻开的牌
                
                }
                if (count == 2) {
    
    			//如果是第二张翻开的牌
                
               }    
            }
        }

Use the value of count to determine which card is currently turned over. If it is 1, it means the first card, and you only need to save its points.

 if (count == 1) {
    
    			//如果是第一张翻开的牌
 	card1 = cards.get(i);	
    card1Value = card1.getValue();	//变量card1Value记录第一张翻开牌的点数
                }

After saving the points, continue to traverse the set. If there are still playing cards that have been turned over, the count value is increased to 2, and the points of the two turned over on the table are compared to see if the points are the same.

 if (count == 2) {
    
    			//如果是第二张翻开的牌
 	card2 = cards.get(i);
    card2Value = card2.getValue();	//变量card2Value记录第二张翻开牌的点数
    if (card1Value == card2Value) {
    
       // 如果翻开的两张牌的点数是一样的 
    	Greenfoot.playSound("WaterDrop.wav");
        Greenfoot.delay(10);	//延迟10毫秒,游戏效果更好
        
        //移除翻开的两张同样的牌
        removeObject(card1);
        removeObject(card2);
        cards.remove(card1);
        cards.remove(card2);
    } else {
    
    	
    	//如果翻开的两张牌不同
        reenfoot.delay(15);
        
        // 将两张牌面朝下背朝上
        card1.turnFaceDown();
        card2.turnFaceDown();
    }
    break;	//剩下的牌不再遍历,结束for循环
}        

Determine whether the two cards are the same by comparing the card1Value and card2Value values. If they are the same, call the removeObject() method to remove them from the table. At the same time, you need to call the remove() method to remove them from the entire playing card set. .

The complete code is as follows:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
import java.util.Collections;

/**
 * 牌桌类,提供游戏的运行场景
 */
public class Table extends World{
    
    
    ArrayList<Card> cards = new ArrayList<Card>();  //声明一个保存扑克牌对象的集合cards 
    //Table类的构造方法
    public Table() {
    
       
        super(600, 400, 1);
        for (int i=1; i<=5; i++) {
    
          //向集合cards中添加两组共10张5点以下的牌
            cards.add(new Card(i));
            cards.add(new Card(i));
        }
        Collections.shuffle(cards); //集合类Collections的混排算法,用于打乱集合cards中牌的顺序
        int x=100, y=100;   //牌桌上摆放牌的起点坐标
        for (int i=0; i<5; i++) {
    
       //用for循环依次在牌桌上摆放每排5张,共两排的扑克牌
            addObject(cards.get(i) , x, y);
            addObject(cards.get(i+5) , x, y + cards.get(i).getImage().getHeight() + 20);
            x += cards.get(i).getImage().getWidth()+20;
        }
    }
    
    //act()方法是游戏单步执行的动作
    public void act() {
    
         
        int count = 0, card1Value=0, card2Value=0;	//count表示牌桌上被翻开的第几张牌
        Card card1=null, card2=null;
        for (int i=0; i< cards.size(); i++) {
    
    	//用for循环遍历集合cards中的所有牌
            if (cards.get(i).getFaceup() == true) {
    
    	//如果遍历到的这张牌是翻开的
                count++;		//用count将牌桌上翻开的牌数累加
                if (count == 1) {
    
    			//如果是第一张翻开的牌
                    card1 = cards.get(i);	
                    card1Value = card1.getValue();	//变量card1Value记录第一张翻开牌的点数
                }
                if (count == 2) {
    
    			//如果是第二张翻开的牌
                    card2 = cards.get(i);
                    card2Value = card2.getValue();	//变量card2Value记录第二张翻开牌的点数
                    if (card1Value == card2Value) {
    
       // 如果翻开的两张牌的点数是一样的 
                        Greenfoot.playSound("WaterDrop.wav");
                        Greenfoot.delay(10);	//延迟10毫秒,游戏效果更好
                        //移除翻开的两张同样的牌
                        removeObject(card1);
                        removeObject(card2);
                        cards.remove(card1);
                        cards.remove(card2);
                    }
                    else {
    
    	//如果翻开的两张牌不同
                        Greenfoot.delay(15);
                        // 将两张牌面朝下背朝上
                        card1.turnFaceDown();
                        card2.turnFaceDown();
                    }
                    break;	//两张牌如相同,则移除,如不同,则翻回来。剩下的牌不再遍历,结束for循环
                }
            }
        }
    }
}

At this point, the mouse clicks on the flop, and the effect of removing cards of the same rank from the table has been achieved.

Guess you like

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