曾经风靡一时的消消乐,至今坐在地铁上都可以看到很多人依然在玩,想当年我也是大军中的一员,那家伙,吃饭都在玩,进入到高级的那种胜利感还是很爽的,连续消,无限消,哈哈,现在想想也是很带劲的。今天就来实现一款简易版的,后续会陆续带上高阶版的,先来尝试一把。
首先我们需要准备消消乐的素材,会放在项目的resource文件夹下面,具体我准备了7种,稍后会展示给大家看的。
开心消消乐
先初始化消消乐的游戏界面参数,就是具体显示多大,每行显示多少个方块,每个方块大小,这里我们按照这个大小来设置,数量和大小看着都还挺舒服的:
'''初始化消消乐的参数'''
WIDTH = 600
HEIGHT = 640
NUMGRID = 12
GRIDSIZE = 48
XMARGIN = (WIDTH - GRIDSIZE * NUMGRID) // 2
YMARGIN = (HEIGHT - GRIDSIZE * NUMGRID) // 2
ROOTDIR = os.getcwd()
FPS = 45
每行显示12个,每个方块48个,看看效果
接下来,我们开始实现消除的逻辑:
初始化消消乐的方格:
class elimination_block(pygame.sprite.Sprite):
def __init__(self, img_path, size, position, downlen, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(img_path)
self.image = pygame.transform.smoothscale(self.image, size)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.down_len = downlen
self.target_x = position[0]
self.target_y = position[1] + downlen
self.type = img_path.split('/')[-1].split('.')[0]
self.fixed = False
self.speed_x = 10
self.speed_y = 10
self.direction = 'down'
移动方块:
'''拼图块移动'''
def move(self):
if self.direction == 'down':
self.rect.top = min(self.target_y, self.rect.top + self.speed_y)
if self.target_y == self.rect.top:
self.fixed = True
elif self.direction == 'up':
self.rect.top = max(self.target_y, self.rect.top - self.speed_y)
if self.target_y == self.rect.top:
self.fixed = True
elif self.direction == 'left':
self.rect.left = max(self.target_x, self.rect.left - self.speed_x)
if self.target_x == self.rect.left:
self.fixed = True
elif self.direction == 'right':
self.rect.left = min(self.target_x, self.rect.left + self.speed_x)
if self.target_x == self.rect.left:
self.fixed = True
获取方块的坐标:
'''获取坐标'''
de