python+pygame实现小游戏连连看之四

上次python+pygame实现小游戏连连看之三已经实现连连看的核心逻辑和讲解,下面来完成后续代码。

七、消除小图标及后续操作

有3个动作:

1、消除小图标

2、消除红框

3、参数重置

修改mouseClick的代码:

def main():
    ...
    def mouseClick(posX,posY):
        ...

        if isStart:
            curCell = getPoint(posX,posY)
            # print (curCell)
            if isUseful(curCell[0],curCell[1]) and not isEmptyCell(curCell[0],curCell[1]):
                if isFirst :
                    drawRedRect(curCell[0],curCell[1])
                    isFirst = False
                    compareCell = curCell
                else:
                    if compareCell[0] == curCell[0]  and compareCell[1] == curCell[1]: 
                        isFirst = True
                        delRedRectangle(curCell[0],curCell[1])
                    else:
                        linkType = getTwoIconsLinkType(compareCell, curCell)
                        if linkType['type'] != NONE_LINK:    
                            ClearMatchedIcons(compareCell, curCell)   #新增
                            delRedRectangle(compareCell[0],compareCell[1])  #新增
                            isFirst = True                       #新增
                            if  isGameSuccess():      #新增通关判断逻辑
                                '''
                                通关后重置
                                '''
                                isStart = False        #新增
                                isFirst = True         #新增
                        else:
                            delRedRectangle(compareCell[0],compareCell[1])
                            compareCell = curCell
                            drawRedRect(curCell[0],curCell[1])

  消除的函数:

def main():
    ...
    '''
    消除
    '''
    def ClearMatchedIcons(Cell0,Cell8):    #新增
        cur_game[Cell0[0]][Cell0[1]] = EMPTYCELL
        cur_game[Cell8[0]][Cell8[1]] = EMPTYCELL
        drawGame()

判断游戏是否结束:循环读取cur_game二维数组,所有的都是EMPTYCELL即为游戏结束

def main():
    ...
    # 判断游戏是否结束
    def isGameSuccess():
        for y in range(0, game_rows):
            for x in range(0, game_cols):
                if cur_game[y][x] != EMPTYCELL:
                    return False
        return True
     

到此,实现了连连看的所有逻辑。

运行效果:

20240624_165208

全部消除后,需要显示一个界面,让玩家选择。

八、游戏结束后的操作

 1、修改消息循环的操作,增加AfterGameSuccess

def main():
    ...
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            
            # 检查鼠标事件
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and isStart:
                # print("鼠标点击:", event.pos[0])
                mouseClick(event.pos[0],event.pos[1])
        
        if not isStart:       #新增
            AfterGameSuccess()    #新增

2、增加AfterGameSuccess函数

def main():
    ...
    '''
    通关后操作
    '''
    # 绘制结束画面
    def AfterGameSuccess():
        again_image = pygame.image.load("imgs/again.png") #再来一局
        again_rect = again_image.get_rect()
        gameover_image = pygame.image.load("imgs/gameover.png") #gameover
        gameover_rect = gameover_image.get_rect()

        again_rect.left, again_rect.top = (width - again_rect.width) // 2, 18
        game_screen.blit(again_image, again_rect)

        gameover_rect.left, gameover_rect.top = (width - again_rect.width) // 2,height - 68
        game_screen.blit(gameover_image, gameover_rect)
       
        pygame.display.flip()
        
        # 检测用户的鼠标操作
        # 如果用户按下鼠标左键
        if pygame.mouse.get_pressed()[0]:
            # 获取鼠标坐标
            choicepos = pygame.mouse.get_pos()
            # print(choicepos)
           
            # 如果用户点击“重新开始”
            if again_rect.left  < choicepos[0] < again_rect.right  and \
                 again_rect.top < choicepos[1] < again_rect.bottom:
                # 调用main函数,重新开始游戏
                main()
            # 如果用户点击“结束游戏”            
            elif gameover_rect.left  < choicepos[0] < gameover_rect.right and \
                 gameover_rect.top < choicepos[1] < gameover_rect.bottom:
                # 退出游戏
                pygame.quit()
                sys.exit()    

小提示:上述判断矩形边界的代码,可以通过pygame的collidepoint实现,代码如下:

def main():
    ...    
    '''
    通关后操作
    '''
    # 绘制结束画面
    def AfterGameSuccess():
        ...
        
        # 检测用户的鼠标操作
        # 如果用户按下鼠标左键
        if pygame.mouse.get_pressed()[0]:
            # 获取鼠标坐标
            choicepos = pygame.mouse.get_pos()
            # print(choicepos)
           
            # 如果用户点击“重新开始”
            if again_rect.collidepoint(choicepos):   #修改
                # 调用main函数,重新开始游戏
                main()
            # 如果用户点击“结束游戏”            
            elif gameover_rect.collidepoint(choicepos):  #修改
                # 退出游戏
                pygame.quit()
                sys.exit()

再次执行程序:

20240624_171243

剩下的问题:1、血槽的添加,增加趣味性

2、计分的逻辑设计,增加趣味性

3、通关后难度的增加

4、背景音乐、消除时动画、小图标主题

5、程序层面的整个程序的封装

......

留给有兴趣的同仁自行搞定。收工~

完整代码及图片资源:python+pygame连连看小游戏源码

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值