一.介绍

 

    本篇文章将使用pygame开发一个拼图小游戏以使读者了解pygame游戏开发的大概流程.

游戏代码几乎没有任何封装,为的就是给读者呈现一个清晰的执行流程.

游戏截图:

 



二.游戏代码

    1.需要的图片,请与代码文件放在同一文件文件夹下,并命名为shili.jpg:

    2.游戏代码:
import random import pygame # 初始化 pygame.init() # 窗口标题
pygame.display.set_caption('拼图游戏') # 窗口大小 s = pygame.display.set_mode((1200,
600)) #绘图地图 imgMap = [ [0, 1, 2], [3, 4, 5], [6, 7, 8] ] #判断胜利的地图 winMap = [
[0, 1, 2], [3, 4, 5], [6, 7, 8] ] #游戏的单击事件 def click(x, y, map): if y - 1 >= 0
and map[y - 1][x] == 8: map[y][x], map[y - 1][x] = map[y - 1][x], map[y][x]
elif y + 1 <= 2 and map[y + 1][x] == 8: map[y][x], map[y + 1][x] = map[y +
1][x], map[y][x] elif x - 1 >= 0 and map[y][x - 1] == 8: map[y][x], map[y][x -
1] = map[y][x - 1], map[y][x] elif x + 1 <= 2 and map[y][x + 1] == 8:
map[y][x], map[y][x + 1] = map[y][x + 1], map[y][x] #打乱地图 def randMap(map): for
i in range(1000): x = random.randint(0, 2) y = random.randint(0, 2) click(x, y,
map) # 加载图片 img = pygame.image.load('./shili.jpg') #随机地图 randMap(imgMap) #游戏主循环
while True: #延时32毫秒,相当于FPS=30 pygame.time.delay(32) for event in
pygame.event.get(): # 窗口的关闭事件 if event.type == pygame.QUIT: exit() elif
event.type == pygame.MOUSEBUTTONDOWN: #鼠标单击事件 if pygame.mouse.get_pressed() ==
(1, 0, 0): #鼠标左键按下 mx, my = pygame.mouse.get_pos() #获得当前鼠标坐标 if mx<498 and my
<498: #判断鼠标是否在操作范围内 x=int(mx/166) #计算鼠标点到了哪个图块 y=int(my/166) click(x,y,imgMap)
#调用单击事件 if imgMap==winMap: #如果当前地图情况和胜利情况相同,就print胜利 print("胜利了!") #背景色填充成绿色
s.fill((0,255,0)) #绘图 for y in range(3): for x in range(3): i = imgMap[y][x] if
i == 8: #8号图块不用绘制 continue dx = (i % 3) * 166 #计算绘图偏移量 dy = (int(i / 3)) * 166
s.blit(img, (x * 166, y * 166), (dx, dy, 166, 166)) # 画参考图片 s.blit(img, (600,
0)) # 刷新界面 pygame.display.flip()
 

三.注意

 

    该游戏如果直接在地图的二维数组上进行随机打乱,是会很大几率出现无解的情况.

    所以,上面的代码是在拼好的基础上采用随机移动(按照游戏规则来移动)的思路进行地图打乱的.

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信