基于OpenCV实现拼图版小游戏,供大家参考,具体内容如下

效果展示

OpenCV Python实现拼图小游戏

实现

思路

1.对图像进行分割,分割成m*n个子图
2.打乱子图的顺序
3.将子图重新组成一幅新的图片并显示
4.添加鼠标点击响应动作,交换鼠标依次点击的两张图的位置
5.每次交换后,判断是否与原图是否一致

python代码

import cv2 as cv
import numpy
import random
import math

src = cv.imread("D:\\CvPic\\1.jpg")
print(src.shape)
h = src.shape[0]
w = src.shape[1]
c = src.shape[2]

row = 3
col = 3

offset_h = h/row
offset_w = w/col

firstClick = False
clickIdx = [0,0]

tileList = []
def calPicIdx(x, y):
 print(str(y)+" "+str(h/col))
 i = y//(offset_h)
 print(str(y%offset_h)+" "+str(offset_w))
 j = math.ceil((x%w)/offset_w)
 idx = i*row+j
 print("i:"+str(i)+" j:"+str(j)+" idx:"+str(idx))
 return int(idx)

def onMouse(event, x, y, flag ,params):
 if event==cv.EVENT_LBUTTONDOWN:
  print("left button down:"+str(x)+" "+str(y))
  idx = calPicIdx(x, y)
  global firstClick
  firstClick = not firstClick
  print(firstClick)
  if firstClick:
   clickIdx[0] = idx
  else:
   clickIdx[1] = idx
   tileList[clickIdx[0]], tileList[clickIdx[1]] = tileList[clickIdx[1]], tileList[clickIdx[0]]
   for i in range(0, row):
    for j in range (0, col):
     dst[i*offset_h:(i+1)*offset_h-1, j*offset_w:(j+1)*offset_w-1] = tileList[i*row+j]
   cv.imshow("dst", dst)

   difference = cv.subtract(dst, src2)
   result = not numpy.any(difference) #if difference is all zeros it will return False
   print("result:"+str(result))
  print(clickIdx)

# --------------splite image into n*n tile--------------

tile = numpy.zeros((offset_h-1, offset_w-1, c),numpy.uint8)

for i in range(0, row):
 for j in range (0, col):
  tile = src[i*offset_h:(i+1)*offset_h-1, j*offset_w:(j+1)*offset_w-1]
  tileList.append(tile)
  # cv.imshow("tile", tile)

# --------------ramdom the tiles--------------------
print(len(tileList))
for i in range(len(tileList)-1,0,-1):
 randomIdx = random.randint(0,i-1)
 print("swap:"+str(random.randint(0,i-1))+" "+str(i))
 tileList[i], tileList[randomIdx] = tileList[randomIdx], tileList[i]

# debug show every tile
# for k,tile in enumerate(tileList):
# cv.imshow("tile"+str(k), tile)

dst = numpy.zeros((h, w, c), numpy.uint8)
for i in range(0, row):
 for j in range (0, col):
  dst[i*offset_h:(i+1)*offset_h-1, j*offset_w:(j+1)*offset_w-1] = tileList[i*row+j]
cv.namedWindow("dst")
cv.setMouseCallback("dst", onMouse)
cv.imshow("dst", dst)

# -------------match the origin image and now--------------
src2 = src.copy()
for i in range(1, row):
 src2[i*offset_h-1:i*offset_h]= numpy.zeros((1,w,3), numpy.uint8)
 for j in range(1, col):
  src2[0:h,j*offset_w-1:j*offset_w]= numpy.zeros((h,1,3), numpy.uint8)
# cv.imshow("src2", src2)

cv.waitKey(0)

参考

90年代经典“手游”—拼图板小游戏Opencv实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
opencv,python,拼图

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“OpenCV Python实现拼图小游戏”

暂无“OpenCV Python实现拼图小游戏”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。