用python实现一个让人戒不掉的百变款消消乐

导语 每日游戏更新系列——今天带大家解锁消消乐小游戏!
一天深夜,我像往常一样快乐地遨游在互联网的海洋里,猝不及防地接到了一个电话。
屏幕上一个触目惊心的大字:妈。
用python实现一个让人戒不掉的百变款消消乐
文章图片
?
我一边动作迟缓地按下通话键,一边迅速反省是不是刚才发朋友圈忘记屏蔽,被她发现我又在熬夜了。
电话那头果然传来了她火急火燎的声音,但我猜中了开头,却猜不到这结局。
【用python实现一个让人戒不掉的百变款消消乐】我妈说:“还没睡觉吧,给我解锁一下消消乐的新关卡。”
用python实现一个让人戒不掉的百变款消消乐
文章图片
?
我还在判断她的操作是有意试探还是虚晃一枪时,她已经开始着急了:“快点,就差最后一个人了,我在等着你啊!”
对于不想睡觉的人来说,每个辗转难眠的夜晚都是相似的,但同样清醒如鸡的我和我妈却有着各自不同的快乐源泉!
用python实现一个让人戒不掉的百变款消消乐
文章图片
?
正文 其实消消乐的话主题的素材可以随意一点儿, 可以是月饼中秋版本的,动漫版本的等等等.......万物皆可。
这边准备了2组消消乐的主题:1.表情包消消乐;2.十二生肖。
?用python实现一个让人戒不掉的百变款消消乐
文章图片

?用python实现一个让人戒不掉的百变款消消乐
文章图片

设置拼图游戏精灵类:

class gemSprite(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 = positionself.downlen = downlenself.target_x = position[0]self.target_y = position[1] + downlenself.type = img_path.split('/')[-1].split('.')[0]self.fixed = Falseself.speed_x = 10self.speed_y = 10self.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 = Trueelif 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 = Trueelif 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 = Trueelif 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

定义游戏类开始游戏主循环:用python实现一个让人戒不掉的百变款消消乐
文章图片

class gemGame():def __init__(self, screen, sounds, font, gem_imgs, cfg, **kwargs):self.info = 'Gemgem'self.screen = screenself.sounds = soundsself.font = fontself.gem_imgs = gem_imgsself.cfg = cfgself.reset()def start(self):clock = pygame.time.Clock()# 遍历整个游戏界面更新位置overall_moving = True# 指定某些对象个体更新位置individual_moving = False# 定义一些必要的变量gem_selected_xy = Nonegem_selected_xy2 = Noneswap_again = Falseadd_score = 0add_score_showtimes = 10time_pre = int(time.time())while True:for event in pygame.event.get():if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):pygame.quit()sys.exit()elif event.type == pygame.MOUSEBUTTONUP:if (not overall_moving) and (not individual_moving) and (not add_score):position = pygame.mouse.get_pos()if gem_selected_xy is None:gem_selected_xy = self.checkSelected(position)else:gem_selected_xy2 = self.checkSelected(position)if gem_selected_xy2:if self.swapGem(gem_selected_xy, gem_selected_xy2):individual_moving = Trueswap_again = Falseelse:gem_selected_xy = Noneif overall_moving:overall_moving = not self.dropGems(0, 0)# 移动一次可能可以拼出多个3连块if not overall_moving:res_match = self.isMatch()add_score = self.removeMatched(res_match)if add_score > 0:overall_moving = Trueif individual_moving:gem1 = self.getGemByPos(*gem_selected_xy)gem2 = self.getGemByPos(*gem_selected_xy2)gem1.move()gem2.move()if gem1.fixed and gem2.fixed:res_match = self.isMatch()if res_match[0] == 0 and not swap_again:swap_again = Trueself.swapGem(gem_selected_xy, gem_selected_xy2)self.sounds['mismatch'].play()else:add_score = self.removeMatched(res_match)overall_moving = Trueindividual_moving = Falsegem_selected_xy = Nonegem_selected_xy2 = Noneself.screen.fill((135, 206, 235))self.drawGrids()self.gems_group.draw(self.screen)if gem_selected_xy:self.drawBlock(self.getGemByPos(*gem_selected_xy).rect)if add_score:if add_score_showtimes == 10:random.choice(self.sounds['match']).play()self.drawAddScore(add_score)add_score_showtimes -= 1if add_score_showtimes < 1:add_score_showtimes = 10add_score = 0self.remaining_time -= (int(time.time()) - time_pre)time_pre = int(time.time())self.showRemainingTime()self.drawScore()if self.remaining_time <= 0:return self.scorepygame.display.update()clock.tick(self.cfg.FPS)

既然是游戏拿肯定是有加分设置的、玩游戏要设置一个时间限制更有趣。
def showRemainingTime(self):remaining_time_render = self.font.render('CountDown: %ss' % str(self.remaining_time), 1, (85, 65, 0))rect = remaining_time_render.get_rect()rect.left, rect.top = (self.cfg.SCREENSIZE[0]-201, 6)self.screen.blit(remaining_time_render, rect) def drawScore(self):score_render = self.font.render('SCORE:'+str(self.score), 1, (85, 65, 0))rect = score_render.get_rect()rect.left, rect.top = (10, 6)self.screen.blit(score_render, rect) def drawAddScore(self, add_score):score_render = self.font.render('+'+str(add_score), 1, (255, 100, 100))rect = score_render.get_rect()rect.left, rect.top = (250, 250)self.screen.blit(score_render, rect)

三个图片相同即可消除:
def removeMatched(self, res_match):if res_match[0] > 0:self.generateNewGems(res_match)self.score += self.rewardreturn self.rewardreturn 0

定义下来的图片特效:
def dropGems(self, x, y):if not self.getGemByPos(x, y).fixed:self.getGemByPos(x, y).move()if x < self.cfg.NUMGRID - 1:x += 1return self.dropGems(x, y)elif y < self.cfg.NUMGRID - 1:x = 0y += 1return self.dropGems(x, y)else:return self.isFull()

拼图的位置交换:
def swapGem(self, gem1_pos, gem2_pos):margin = gem1_pos[0] - gem2_pos[0] + gem1_pos[1] - gem2_pos[1]if abs(margin) != 1:return Falsegem1 = self.getGemByPos(*gem1_pos)gem2 = self.getGemByPos(*gem2_pos)if gem1_pos[0] - gem2_pos[0] == 1:gem1.direction = 'left'gem2.direction = 'right'elif gem1_pos[0] - gem2_pos[0] == -1:gem2.direction = 'left'gem1.direction = 'right'elif gem1_pos[1] - gem2_pos[1] == 1:gem1.direction = 'up'gem2.direction = 'down'elif gem1_pos[1] - gem2_pos[1] == -1:gem2.direction = 'up'gem1.direction = 'down'gem1.target_x = gem2.rect.leftgem1.target_y = gem2.rect.topgem1.fixed = Falsegem2.target_x = gem1.rect.leftgem2.target_y = gem1.rect.topgem2.fixed = Falseself.all_gems[gem2_pos[0]][gem2_pos[1]] = gem1self.all_gems[gem1_pos[0]][gem1_pos[1]] = gem2return True

???附游戏主程序界面:
import osimport sysimport cfgimport pygamefrom modules import * def main():pygame.init()screen = pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('消消乐小游戏')# 加载背景音乐pygame.mixer.init()pygame.mixer.music.load(os.path.join(cfg.ROOTDIR, "resources/audios/bg.mp3"))pygame.mixer.music.set_volume(0.6)pygame.mixer.music.play(-1)# 加载音效sounds = {}sounds['mismatch'] = pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/badswap.wav'))sounds['match'] = []for i in range(6):sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/match%s.wav' % i)))# 加载字体font = pygame.font.Font(os.path.join(cfg.ROOTDIR, 'resources/font/font.TTF'), 25)# 图片加载gem_imgs = []for i in range(1, 8):gem_imgs.append(os.path.join(cfg.ROOTDIR, 'resources/images/gem%s.png' % i))# 主循环game = gemGame(screen, sounds, font, gem_imgs, cfg)while True:score = game.start()flag = False# 一轮游戏结束后玩家选择重玩或者退出while True:for event in pygame.event.get():if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):pygame.quit()sys.exit()elif event.type == pygame.KEYUP and event.key == pygame.K_r:flag = Trueif flag:breakscreen.fill((135, 206, 235))text0 = 'Final score: %s' % scoretext1 = 'Press to restart the game.'text2 = 'Press to quit the game.'y = 150for idx, text in enumerate([text0, text1, text2]):text_render = font.render(text, 1, (85, 65, 0))rect = text_render.get_rect()if idx == 0:rect.left, rect.top = (212, y)elif idx == 1:rect.left, rect.top = (122.5, y)else:rect.left, rect.top = (126.5, y)y += 100screen.blit(text_render, rect)pygame.display.update()game.reset() '''run'''if __name__ == '__main__':main()

效果图:
用python实现一个让人戒不掉的百变款消消乐
文章图片

?说实话这个表情包版本的找的好哇!哈哈哈 很多相似的找都找不出来~
?用python实现一个让人戒不掉的百变款消消乐
文章图片

这个是真卡哇伊哈哈哈 适合家里的小朋友玩~
总结 好啦!每日游戏更新系列今天的结束了!
用python实现一个让人戒不掉的百变款消消乐
文章图片
????
到此这篇关于用python实现一个让人戒不掉的百变款消消乐的文章就介绍到这了,更多相关python 消消乐内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读