或者是感觉安装速度太慢可以添加镜像源文件的,这边我通常用的是豆瓣的镜像源:
pip install -i https://pypi.douban.com/simple/ pygame
2)准备好素材
这边准备了2组素材大家可以自由选择:
首先是第一组是打游戏的表情包——
然后第二组是王者里面的人物图像——
3)附完整的源码
import os
import sys
import random
import pygame
‘’‘判断游戏是否结束’‘’
def isGameOver(board, size):
assert isinstance(size, int)
num_cells = size * size
for i in range(num_cells-1):
if board[i] != i: return False
return True
‘’‘将空白Cell左边的Cell右移到空白Cell位置’‘’
def moveR(board, blank_cell_idx, num_cols):
if blank_cell_idx % num_cols == 0: return blank_cell_idx
board[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_idx
board[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_idx
board[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_idx
board[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 - 1
board[blank_cell_idx] = -1
for i in range(cfg.NUMRANDOM):
0: left, 1: right, 2: up, 3: down
direction = 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) > 0
return 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 3
elif event.key == ord(‘m’): return 4
elif event.key == ord(‘h’): return 5
pygame.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, size
num_cells = size * size
计算Cell大小
cell_width = game_img_used_rect.width // num_cols
cell_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 = True
while 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_width
y_pos = y // cell_height
idx = x_pos + y_pos * num_cols
if idx == blank_cell_idx-1:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)
.(img-KnfZexOb-1713800991075)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)