英文的购物网站,高端网站建设公司哪家服务好,新像素ui设计官网,新安人才网曾经风靡一时的消消乐#xff0c;至今坐在地铁上都可以看到很多人依然在玩#xff0c;想当年我也是大军中的一员#xff0c;那家伙#xff0c;吃饭都在玩#xff0c;进入到高级的那种胜利感还是很爽的#xff0c;连续消#xff0c;无限消#xff0c;哈哈#xff0c;现…
曾经风靡一时的消消乐至今坐在地铁上都可以看到很多人依然在玩想当年我也是大军中的一员那家伙吃饭都在玩进入到高级的那种胜利感还是很爽的连续消无限消哈哈现在想想也是很带劲的。今天就来实现一款简易版的后续会陆续带上高阶版的先来尝试一把。 首先我们需要准备消消乐的素材会放在项目的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 positionself.down_len 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获取方块的坐标 获取坐标def get_position(self):return self.rect.left, self.rect.top设置坐标def set_position(self, position):self.rect.left, self.rect.top position绘制得分加分还有倒计时 显示剩余时间def show_remained_time(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 (WIDTH - 201, 6)self.screen.blit(remaining_time_render, rect)显示得分def show_score(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 add_score(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 generate_new_blocks(self, res_match):if res_match[0] 1:start res_match[2]while start -2:for each in [res_match[1], res_match[1] 1, res_match[1] 2]:block self.get_block_by_position(*[each, start])if start res_match[2]:self.blocks_group.remove(block)self.all_blocks[each][start] Noneelif start 0:block.target_y GRIDSIZEblock.fixed Falseblock.direction downself.all_blocks[each][start 1] blockelse:block elimination_block(img_pathrandom.choice(self.block_images), size(GRIDSIZE, GRIDSIZE),position[XMARGIN each * GRIDSIZE, YMARGIN - GRIDSIZE],downlenGRIDSIZE)self.blocks_group.add(block)self.all_blocks[each][start 1] blockstart - 1elif res_match[0] 2:start res_match[2]while start -4:if start res_match[2]:for each in range(0, 3):block self.get_block_by_position(*[res_match[1], start each])self.blocks_group.remove(block)self.all_blocks[res_match[1]][start each] Noneelif start 0:block self.get_block_by_position(*[res_match[1], start])block.target_y GRIDSIZE * 3block.fixed Falseblock.direction downself.all_blocks[res_match[1]][start 3] blockelse:block elimination_block(img_pathrandom.choice(self.block_images), size(GRIDSIZE, GRIDSIZE),position[XMARGIN res_match[1] * GRIDSIZE, YMARGIN start * GRIDSIZE],downlenGRIDSIZE * 3)self.blocks_group.add(block)self.all_blocks[res_match[1]][start 3] blockstart - 1绘制拼图移除匹配下滑的效果等 移除匹配的blockdef clear_matched_block(self, res_match):if res_match[0] 0:self.generate_new_blocks(res_match)self.score self.rewardreturn self.rewardreturn 0游戏界面的网格绘制def draw_grids(self):for x in range(NUMGRID):for y in range(NUMGRID):rect pygame.Rect((XMARGIN x * GRIDSIZE, YMARGIN y * GRIDSIZE, GRIDSIZE, GRIDSIZE))self.draw_block(rect, color(0, 0, 255), size1)画矩形block框def draw_block(self, block, color(255, 0, 255), size4):pygame.draw.rect(self.screen, color, block, size)下落特效def draw_drop_animation(self, x, y):if not self.get_block_by_position(x, y).fixed:self.get_block_by_position(x, y).move()if x NUMGRID - 1:x 1return self.draw_drop_animation(x, y)elif y NUMGRID - 1:x 0y 1return self.draw_drop_animation(x, y)else:return self.is_all_full()是否每个位置都有拼图块了def is_all_full(self):for x in range(NUMGRID):for y in range(NUMGRID):if not self.get_block_by_position(x, y).fixed:return Falsereturn True绘制匹配规则绘制拼图交换效果 检查有无拼图块被选中def check_block_selected(self, position):for x in range(NUMGRID):for y in range(NUMGRID):if self.get_block_by_position(x, y).rect.collidepoint(*position):return [x, y]return None是否有连续一样的三个块(无--返回0/水平--返回1/竖直--返回2)def is_block_matched(self):for x in range(NUMGRID):for y in range(NUMGRID):if x 2 NUMGRID:if self.get_block_by_position(x, y).type self.get_block_by_position(x 1, y).type self.get_block_by_position(x 2,y).type:return [1, x, y]if y 2 NUMGRID:if self.get_block_by_position(x, y).type self.get_block_by_position(x, y 1).type self.get_block_by_position(x,y 2).type:return [2, x, y]return [0, x, y]根据坐标获取对应位置的拼图对象def get_block_by_position(self, x, y):return self.all_blocks[x][y]交换拼图def swap_block(self, block1_pos, block2_pos):margin block1_pos[0] - block2_pos[0] block1_pos[1] - block2_pos[1]if abs(margin) ! 1:return Falseblock1 self.get_block_by_position(*block1_pos)block2 self.get_block_by_position(*block2_pos)if block1_pos[0] - block2_pos[0] 1:block1.direction leftblock2.direction rightelif block1_pos[0] - block2_pos[0] -1:block2.direction leftblock1.direction rightelif block1_pos[1] - block2_pos[1] 1:block1.direction upblock2.direction downelif block1_pos[1] - block2_pos[1] -1:block2.direction upblock1.direction downblock1.target_x block2.rect.leftblock1.target_y block2.rect.topblock1.fixed Falseblock2.target_x block1.rect.leftblock2.target_y block1.rect.topblock2.fixed Falseself.all_blocks[block2_pos[0]][block2_pos[1]] block1self.all_blocks[block1_pos[0]][block1_pos[1]] block2return True准备工作差不多了开始主程序吧
def main():pygame.init()screen pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption(开心消消乐)# 加载背景音乐pygame.mixer.init()pygame.mixer.music.load(os.path.join(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(ROOTDIR, resources/audios/badswap.wav))sounds[match] []for i in range(6):sounds[match].append(pygame.mixer.Sound(os.path.join(ROOTDIR, resources/audios/match%s.wav % i)))# 加载字体font pygame.font.Font(os.path.join(ROOTDIR, resources/font.TTF), 25)# 图片加载block_images []for i in range(1, 8):block_images.append(os.path.join(ROOTDIR, resources/images/gem%s.png % i))# 主循环game InitGame(screen, sounds, font, block_images)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 R to restart the game.text2 Press Esc 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()test
if __name__ __main__:main()运行main.py就可以开始游戏了看看效果吧 玩了一下还可以可能是电脑原因初始化的时候存在卡顿的情况后续会优化一下初始化8个64大小的方块是最佳情况大家可以尝试改下配置文件就好了。后续会退出各个小游戏的进阶版欢迎大家关注及时获取最新内容。
需要游戏素材和完整代码点**开心消消乐**获取。
今天的分享就到这里希望感兴趣的同学关注我每天都有新内容不限题材不限内容你有想要分享的内容也可以私聊我