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

连连看是一个小游戏,主要是打发时间的,闲来无事,记录一下编写的过程。

设想步骤:1、在图片中

截取几个小的图片作为连连看的小图标

2、随机实现小图片的排列(注意一定是成对出现,不然咋连),为方便测试,先弄6*6的

3、连连看逻辑实现

4、通关后的界面

以下是过程:

一、界面初始化等基本动作

没有啥好讲的,直接上代码:

import pygame
import sys
from pygame.locals import *
import time
import traceback
import os,random

def main():
    pygame.init()
    game_size = width,height = 700,500
    bg_color = (255,255,255)  #白色底
    game_cols = 6
    game_rows = 6
    imgs_repeat =  game_cols * game_rows / 4
    cell_size = 40
    
    pygame.display.set_caption('Hi,连连看!')


    '''    
    让游戏界面居中显示
    '''
    os.environ['SDL_VIDEO_CENTERED'] = '1' 
    game_screen = pygame.display.set_mode(game_size)  
    # 获取屏幕的宽度和高度
    game_screen_width = game_screen.get_width()
    game_screen_height = game_screen.get_height()
    
    # 计算图片应该被放置的位置,使其居中
    x_position = (game_screen_width // 2) - (game_cols * cell_size // 2)
    y_position = (game_screen_height // 2) - (game_rows * cell_size // 2)


    game_screen.fill(bg_color)   #1、填充背景颜色,这样也把上一次的图像刷掉了。
    
    pygame.display.flip() #3、双缓冲机制,一次性将缓冲好的内容显示在屏幕上

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

if __name__ == "__main__":
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()

简单起见,目前只搞6*6,每个小图标是40*40,imgs_repeat表示的是一个图片可以出现4次。上述代码将其设置为居中:游戏居中,未来的图片显示在游戏中也是居中。

框架已有了,下面就慢慢添砖加瓦。

二、切出小图片

利用pygame中对象Surface的subsurface方法即可实现,它返回一个子 Surface 对象,它将与父对象共享所有的像素。有4个参数,依次为左上角坐标(x,y)及截取的宽度,高度。依赖左上角坐标x的值可以循环截取了。

实现的方法就是这样的:

def main():
    ...
    cell_size = 40
    small_imgs = []    #新增
    ...
    '''
    分割小图标
    '''
    def splitImg():
        llk_base = pygame.image.load("imgs\\llk.png")  #图片加载成功后,pygame将图片转换为一个Surface对象
        for i in range(0, int(imgs_repeat)):
            i_left = i * cell_size
            small_imgs.append(llk_base.subsurface(i_left,0,cell_size,cell_size))

三、随机将小图片放在屏幕上

选图片有三步

1、将需要的图放在一个列表tmp_game中

2、随机选中放到列表mid_game,并从tmp_game删除

3、将mid_game转为一个二维的,这样对应了屏幕的2D空间

def main():
    ...

    cur_game = []    #游戏        ,新增
    tmp_game = []    #临时游戏内容   ,新增
    mid_game = []    #中间游戏    ,新增

    '''
    初始化游戏
    '''
    def iniGame():
        for i in range(0, int(imgs_repeat)):
            for j in range(0, 4):
                tmp_game.append(i)

        total = game_cols * game_rows
        for x in range(0, total):
            index = random.randint(0, total - x - 1)
            mid_game.append(tmp_game[index])
            del tmp_game[index]

        # 一维转为二维,y为高
        for y in range(0, game_rows):
            for x in range(0, game_cols):
                if x == 0:
                    cur_game.append([])
                cur_game[y].append(mid_game[x + y * game_rows])

最后的cur_game就是对应的屏幕上的二维数组了。数组的内容就是小图标small_imgs的下标。

选完后将小图片按规定的二维顺序显示到页面上即可。显示时还是利用循环按行列顺序blit组成一个大的Surface,然后再flip刷到屏幕上就可以了。注意,在后面消掉后也要调用该方法,所以设置一个EMPTYCELL常量,对应-1,这样就表示当前的格子上的小图标已经消掉了。

def main():
    ...
    EMPTYCELL = -1   #新增

    '''
    显示   game_screen.blit(small_imgs[0],(x_position,y_position))
    '''
    def drawGame():
        game_screen.fill(bg_color)
        for y in range(0, game_rows):
            for x in range(0, game_cols):
                if cur_game[y][x] != EMPTYCELL:
                    game_screen.blit(small_imgs[cur_game[y][x]],(x_position + x * cell_size,y_position + y * cell_size))
        pygame.display.flip()

修改main代码,把程序完善一下。

def main():
    ...
    # 计算图片应该被放置的位置,使其居中
    x_position = (game_screen_width // 2) - (game_cols * cell_size // 2)
    y_position = (game_screen_height // 2) - (game_rows * cell_size // 2)

    splitImg()    #新增

    game_screen.fill(bg_color)   #1、填充背景颜色,这样也把上一次的图像刷掉了。
    '''
    初始化,并显示    
    '''
    iniGame()     #新增
    drawGame()    #新增

    pygame.display.flip() #3、双缓冲机制,一次性将缓冲好的内容显示在屏幕上

调试程序:

对应的例如cur_game[0][5]的值是2,对应的图片就是

待续。。。

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个用 Python 实现连连看小游戏: ```python import pygame import random # 初始化 pygame.init() # 设置游戏界面大小 screen = pygame.display.set_mode((800, 600)) # 设置游戏标题 pygame.display.set_caption("连连看") # 加载图片资源 bg_image = pygame.image.load("background.png").convert() icons_image = pygame.image.load("icons.png").convert_alpha() # 定义游戏元素大小 icon_size = 64 # 定义游戏元素的行列数 rows = 8 cols = 10 # 定义游戏元素的图像区域 icons_rects = [ pygame.Rect((i * icon_size, j * icon_size), (icon_size, icon_size)) for i in range(cols) for j in range(rows) ] # 定义游戏元素的类型 icons_types = [i // 2 for i in range(rows * cols)] # 随机打乱游戏元素类型的顺序 random.shuffle(icons_types) # 定义游戏元素的位置和类型 icons = [ (icons_rects[i], icons_types[i]) for i in range(rows * cols) ] # 定义选中游戏元素的变量 selected = [] # 定义游戏的主循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: # 退出游戏 pygame.quit() exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 处理鼠标按下事件 x, y = event.pos for i, (rect, t) in enumerate(icons): if rect.collidepoint(x, y) and i not in selected: selected.append(i) if len(selected) == 2: # 如果已经选中了两个游戏元素,判断它们是否能够相连 i1, i2 = selected if icons_types[i1] == icons_types[i2]: icons_types[i1] = icons_types[i2] = -1 selected = [] else: selected = [i2] break # 绘制游戏界面 screen.blit(bg_image, (0, 0)) for i, (rect, t) in enumerate(icons): if icons_types[i] >= 0: screen.blit(icons_image, rect, pygame.Rect((t * icon_size, 0), (icon_size, icon_size))) pygame.display.update() ``` 在运行代码之前,需要确保程序所在的目录下有以下两张图片文件: - `background.png`:游戏界面的背景图片。 - `icons.png`:游戏元素的图像资源,由多个游戏元素的图像拼接而成。 运行代码后,将会看到一个简单的连连看小游戏界面。你可以使用鼠标来选中两个相同类型的游戏元素,当它们之间可以画一条线连接时,这两个游戏元素将会被消除。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值