本文主要是介绍独一无二的《斗罗大陆》小游戏火爆上线,玩家闯关等你来~(等级有点儿难),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
大家好!我是梨子同学!
希望大家多多支持我!哈哈
为了感谢每一个关注我的小可爱:💓每篇文章的项目源码都是无偿分享滴💓👇👇👇👇
点这里蓝色这行字体自取,需要什么源码记得说标题名字哈!私信我也可!
很多csdn的功能还在研究中,还有小编的文笔不好勿怪,会慢慢进步跟大家一起学习的
小编也一直在学习编程,如果代码小程序出现错误欢迎大家评论区留言哈!
最后——如果文章有帮助到你,记得“关注”、“点赞”、“评论”三连哦~
正文
对于《斗罗大陆》动画粉丝而言,春节即将到来,除了蕴藏对新年的憧憬外,还承载着更多期盼。
小编最希望的就是导演能每个星期多更新一集,这部动画作品带给粉丝无数难忘的回忆。(当然也
包括我啦~)嗯哼~今天的话也给大家带来一款斗罗小拼图游戏给大家。希望大家喜欢~💖💖
一、素材环境
1)准备好相应的素材图片,这里是随机到网上寻找的斗罗的背景图片,记得设置好小程序的屏幕
大小跟素材大小大概一致,能一致更好!不然图片显示不全的哈!
2)环境安装准备好Python版本基本上都可以、小编用的Python3.7、Pycharm2021的,然后写游
戏的话基本上用的都是Pygame专属游戏模块写的,然后一些自带的不用管 直接导入即可。
安装模块也就是第三方模块的小编经常用的方法是:pip install +模块名或者提速需要用到镜像源,
百度下或者csdn搜下就会出来很多安装模块的镜像源这里就不一一介绍了!
二、代码演示
完整的源码展示,主要分为2个py小源码。
主程序game.py
import os
import sys
import cfg
import random
import pygame'''判断游戏是否结束'''
def isGameOver(board, size):assert isinstance(size, int)num_cells = size * sizefor i in range(num_cells-1):if board[i] != i: return Falsereturn True'''将空白Cell左边的Cell右移到空白Cell位置'''
def moveR(board, blank_cell_idx, num_cols):if blank_cell_idx % num_cols == 0: return blank_cell_idxboard[blank_cell_idx-1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-1]return blank_cell_idx - 1'''将空白Cell右边的Cell左移到空白Cell位置'''
def moveL(board, blank_cell_idx, num_cols):if (blank_cell_idx+1) % num_cols == 0: return blank_cell_idxboard[blank_cell_idx+1], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+1]return blank_cell_idx + 1'''将空白Cell上边的Cell下移到空白Cell位置'''
def moveD(board, blank_cell_idx, num_cols):if blank_cell_idx < num_cols: return blank_cell_idxboard[blank_cell_idx-num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx-num_cols]return blank_cell_idx - num_cols'''将空白Cell下边的Cell上移到空白Cell位置'''
def moveU(board, blank_cell_idx, num_rows, num_cols):if blank_cell_idx >= (num_rows-1) * num_cols: return blank_cell_idxboard[blank_cell_idx+num_cols], board[blank_cell_idx] = board[blank_cell_idx], board[blank_cell_idx+num_cols]return blank_cell_idx + num_cols'''获得打乱的拼图'''
def CreateBoard(num_rows, num_cols, num_cells):board = []for i in range(num_cells): board.append(i)# 去掉右下角那块blank_cell_idx = num_cells - 1board[blank_cell_idx] = -1for i in range(cfg.NUMRANDOM):# 0: left, 1: right, 2: up, 3: downdirection = random.randint(0, 3)if direction == 0: blank_cell_idx = moveL(board, blank_cell_idx, num_cols)elif direction == 1: blank_cell_idx = moveR(board, blank_cell_idx, num_cols)elif direction == 2: blank_cell_idx = moveU(board, blank_cell_idx, num_rows, num_cols)elif direction == 3: blank_cell_idx = moveD(board, blank_cell_idx, num_cols)return board, blank_cell_idx'''随机选取一张图片'''
def GetImagePath(rootdir):imagenames = os.listdir(rootdir)assert len(imagenames) > 0return os.path.join(rootdir, random.choice(imagenames))'''显示游戏结束界面'''
def ShowEndInterface(screen, width, height):screen.fill(cfg.BACKGROUNDCOLOR)font = pygame.font.Font(cfg.FONTPATH, width//15)title = font.render('恭喜! 你成功完成了拼图!', True, (233, 150, 122))rect = title.get_rect()rect.midtop = (width/2, height/2.5)screen.blit(title, rect)pygame.display.update()while True:for event in pygame.event.get():if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):pygame.quit()sys.exit()pygame.display.update()'''显示游戏开始界面'''
def ShowStartInterface(screen, width, height):screen.fill(cfg.BACKGROUNDCOLOR)tfont = pygame.font.Font(cfg.FONTPATH, width//4)cfont = pygame.font.Font(cfg.FONTPATH, width//20)title = tfont.render('斗罗大陆', True, cfg.RED)content1 = cfont.render('按H、M、L开始游戏', True, cfg.BLUE)content2 = cfont.render('H为难度模式, M为中级模式, L简易模式', True, cfg.BLUE)trect = title.get_rect()trect.midtop = (width/2, height/10)crect1 = content1.get_rect()crect1.midtop = (width/2, height/2.2)crect2 = content2.get_rect()crect2.midtop = (width/2, height/1.8)screen.blit(title, trect)screen.blit(content1, crect1)screen.blit(content2, crect2)while True:for event in pygame.event.get():if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):pygame.quit()sys.exit()elif event.type == pygame.KEYDOWN:if event.key == ord('l'): return 3elif event.key == ord('m'): return 4elif event.key == ord('h'): return 5pygame.display.update()'''主函数'''
def main():# 初始化pygame.init()clock = pygame.time.Clock()# 加载图片game_img_used = pygame.image.load(GetImagePath(cfg.PICTURE_ROOT_DIR))game_img_used = pygame.transform.scale(game_img_used, cfg.SCREENSIZE)game_img_used_rect = game_img_used.get_rect()# 设置窗口screen = pygame.display.set_mode(cfg.SCREENSIZE)pygame.display.set_caption('王者拼图小游戏')# 游戏开始界面size = ShowStartInterface(screen, game_img_used_rect.width, game_img_used_rect.height)assert isinstance(size, int)num_rows, num_cols = size, sizenum_cells = size * size# 计算Cell大小cell_width = game_img_used_rect.width // num_colscell_height = game_img_used_rect.height // num_rows# 避免初始化为原图while True:game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells)if not isGameOver(game_board, size):break# 游戏主循环is_running = Truewhile is_running:# --事件捕获for event in pygame.event.get():# ----退出游戏if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):pygame.quit()sys.exit()# ----键盘操作elif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT or event.key == ord('a'):blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)elif event.key == pygame.K_RIGHT or event.key == ord('d'):blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)elif event.key == pygame.K_UP or event.key == ord('w'):blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)elif event.key == pygame.K_DOWN or event.key == ord('s'):blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)# ----鼠标操作elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:x, y = pygame.mouse.get_pos()x_pos = x // cell_widthy_pos = y // cell_heightidx = x_pos + y_pos * num_colsif idx == blank_cell_idx-1:blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols)elif idx == blank_cell_idx+1:blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols)elif idx == blank_cell_idx+num_cols:blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols)elif idx == blank_cell_idx-num_cols:blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols)# --判断游戏是否结束if isGameOver(game_board, size):game_board[blank_cell_idx] = num_cells - 1is_running = False# --更新屏幕screen.fill(cfg.BACKGROUNDCOLOR)for i in range(num_cells):if game_board[i] == -1:continuex_pos = i // num_colsy_pos = i % num_colsrect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height)img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height)screen.blit(game_img_used, rect, img_area)for i in range(num_cols+1):pygame.draw.line(screen, cfg.BLACK, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height))for i in range(num_rows+1):pygame.draw.line(screen, cfg.BLACK, (0, i*cell_height), (game_img_used_rect.width, i*cell_height))pygame.display.update()clock.tick(cfg.FPS)# 游戏结束界面ShowEndInterface(screen, game_img_used_rect.width, game_img_used_rect.height)
配置文件定义一些屏幕大小等。
'''配置文件'''
import os'''屏幕大小'''
SCREENSIZE = (1080, 700)
'''图片素材根目录'''
PICTURE_ROOT_DIR = os.path.join(os.getcwd(), 'resources/pictures')
'''字体路径'''
FONTPATH = os.path.join(os.getcwd(), 'resources/font/FZSTK.TTF')
'''定义一些颜色'''
BACKGROUNDCOLOR = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
'''FPS'''
FPS = 40
'''随机打乱拼图次数'''
NUMRANDOM = 100
三、效果展示
1)第一组随机展示唐三原图VS效果图
2)第二组随机展示唐三原图VS效果
3)前面都是简单L的3*3模式,这里开始中级模式
o_o ....好了不多说了,我先攻克中级模式去了......最难的等你们慢慢发掘了~
总结
嘿,最后来欣赏下小舞的美貌~百看不厌,当然斗罗建模人物都挺精致的。棒棒~
关注小编获取更多精彩内容!
制作不易,记得一键三连哦!! 如需打包好的源码+素材免费分享滴!!传送门
这篇关于独一无二的《斗罗大陆》小游戏火爆上线,玩家闯关等你来~(等级有点儿难)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!