""" 单图拼图游戏,本游戏平均切分一张图片,打乱顺序后显示在屏幕上,用鼠标依次单击能交换图片的坐标顺序,直到拼好和原图一样,则成功结束。核心模块 block.py 扫码可见。 """ __author__ = "lixingqiu" from block import * rows,cols = 2,2 # 行列数 amounts = rows*cols # 初切分的图像总数 imagefile = "nezha.jpg" # 待切分的图像 screen = Screen() # 新建屏幕对象 "成功切分,那么sliced_images_list就不是空的,if语句就成立" if sliced_images_list: random_index_list = list(range(amounts)) # random_index_list列表存储的是索引,如果是2x2切分,那么初始值为[0,1,2,3] shuffle(random_index_list) # 洗牌随机索引表,打扰顺序后的值可能是 2,3,0,1 height_step = image_height//rows # 每个被切分的图像的高度 width_step = image_width //cols # 每个被切分的图像的宽度 left = -image_width//2 # 图像左上角x坐标 top = image_height//2 # 图像左上角y标 coordinates = [] # 存储切分的每张图的中心点坐标,从左上角第一个数起 "以下代码形成每块图片的中心点坐标,存储在coordinates列表中" for y in range(rows): # 2x2切分的话,y的值是0,1 for x in range(cols): # 2x2切分的话,x的值是0,1 startx = left + x * width_step # 每张切分的图像左上角x坐标 starty = top - y * height_step # 每张切分的图像左上角y坐标 centerx = startx + width_step//2 # 每张切分的图像的中心点x坐标 centery = starty - height_step//2 # 每张切分的图像的中心点y坐标 coordinates.append((centerx,centery)) # 添加到坐标列表 [screen.addshape(image) for image in sliced_images_list] # 注册每张图到形状列表 blocks_list = [] for i in range(amounts): "角色己的初始坐标" index = random_index_list[i] # 取出的索引号是按顺序取出的。 """Block类,有它新建对象后,它能把分块图进行封装,让图能记住自己原始坐标及即将定位的坐标及接受鼠标单击事件,以便和其它图交换位置。" coordinates[index]是它的初始坐标,coordinates[i]是它即将定位的坐标,把图形,coordinates和index与i传进去即可。 """ s = Block(sliced_images_list[index],coordinates,index,i) # 由于index已被打乱顺序,所以形成的图片就是随机的 blocks_list.append(s) screen.mainloop()
下载完整源代码与素材,包含各个子模块,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)