头图

Hello everyone, welcome to Crossin's programming classroom!
If you want to say the most popular game recently, it must be "The Sheep" has not run.

image.png

It has been on the hot search for several days in a row, and even CCTV has come to remind people to beware of scams in the name of the game.
But the other side of the game's explosion is also the complaints and dissatisfaction of countless players about the difficulty of the second level.

image.png

If you are like me, you are not happy because you can't pass the level, you might as well write a simple version in Python, think about a few levels and pass a few levels and even transfer an unsolved level to go to other people

image.png

My version is not only difficult, but the code is also very simple, less than 100 lines in total. The code has been open sourced:
python666.cn/c/8
It can be downloaded, modified, and commercially available, but attribution is required for public release. Source: Crossin's programming classroom made an equally simple video for everyone to enjoy:

https://www.bilibili.com/video/BV1Ze411K7WF/?aid=260990669&cid=842342999&page=1

A little explanation of the idea and code:

1.
The game uses the pygame-zero library (so it can be implemented with so little code), and the pgzero library needs to be installed before running:

 pip install pgzero

(or install via IDE)

2.
When running, you need to bring the images and music folders in the project (you can replace the pictures and music in it)

3.
Each card is an Actor object (equivalent to the wizard class in general game development), with attributes such as type (12 cards of 12 types), level, status (unpointable/pointable/collected).
At the beginning of the game, they are randomly scrambled and placed, and different pictures are displayed according to the type.
Except for the top card that can be tapped, the rest cannot be tapped.

 # 初始化牌组,12*12张牌随机打乱
ts = list(range(1, 13))*12
random.shuffle(ts)
n = 0
for k in range(7):    # 7层
    for i in range(7-k):    #每层减1行
        for j in range(7-k):
            t = ts[n]        #获取排种类
            n += 1
            tile = Actor(f'tile{t}')       #使用tileX图片创建Actor对象
            tile.pos = 120 + (k * 0.5 + j) * tile.width, 100 + (k * 0.5 + i) * tile.height * 0.9    #设定位置
            tile.tag = t            #记录种类
            tile.layer = k          #记录层级
            tile.status = 1 if k == 6 else 0        #除了最顶层,状态都设置为0(不可点)这里是个简化实现
            tiles.append(tile)

4.
Determine whether the mouse clicked position is in a certain card that can be clicked. If so, remove it from the list of all cards and add it to the list of cards below.
Then iterate over the next layer of cards that overlap with this card, and for each lower layer card, check in turn if there are other upper layer cards covering it, if not, make it clickable.

 for tile in reversed(tiles):    #逆序循环是为了先判断上方的牌,如果点击了就直接跳出,避免重复判定
    if tile.status == 1 and tile.collidepoint(pos):
        # 状态1可点,并且鼠标在范围内
        tile.status = 2
        tiles.remove(tile)
        diff = [t for t in docks if t.tag != tile.tag]    #获取牌堆内不相同的牌
        if len(docks) - len(diff) < 2:    #如果相同的牌数量<2,就加进牌堆
            docks.append(tile)
        else:             #否则用不相同的牌替换牌堆(即消除相同的牌)
            docks = diff
        for down in tiles:      #遍历所有的牌
            if down.layer == tile.layer - 1 and down.colliderect(tile):   #如果在此牌的下一层,并且有重叠
                for up in tiles:      #那就再反过来判断这种被覆盖的牌,是否还有其他牌覆盖它
                    if up.layer == down.layer + 1 and up.colliderect(down):     #如果有就跳出
                        break
                else:      #如果全都没有,说明它变成了可点状态
                    down.status = 1
        return

5.
If the lower deck reaches 7 cards, it is a failure, and if there are no remaining cards in the upper deck, it is a victory.

 # 达到7张,失败
if len(docks) >= 7:
    screen.blit('end', (0, 0))
# 没有剩牌,胜利
if len(tiles) == 0:
    screen.blit('win', (0, 0))

6.
This demo still has room for improvement from "The Sheep", and interested friends can try to expand, including but not limited to:

  • prop function
  • Increase the animation effect of closing cards (you can use the animate method provided by pgzero)
  • More level layouts
  • packaged into exe
  • Game leaderboard
  • Of course, there is also the most core part of the game: watching ads

The code has been open sourced:
python666.cn/c/8
It can be downloaded, modified, and commercially available, but attribution is required for public release. Source: Crossin's Programming Classroom If you like it, thank you for your help in liking and forwarding. Your support is the driving force for my continuous update!
I'm Crossin, see you in the next game~
By the way, oh, by the way, if you want to learn Python, remember to take a look at my "Everyone Can Learn Python" series of zero-based python introductory graphic tutorials: python666.cn


Crossin先生
945 声望193 粉丝