• 小学生Python编程——拼图


    ​​​​​​​

    from pgzrun import *

    #{
    empty = [
             [186, 352],
             [266, 352],
             [345, 352],
             [425, 352],
             [186, 432],
             [266, 432],
             [346, 432],
             [425, 432],
             [186, 512],
             [266, 512],
             [346, 512],
             [425, 512],
             [186, 592],
             [266, 592],
             [345, 592],
             [425, 592]
            ]
    #} #empty坐标列表在这里,用于吸附
    #{     
    from random import *
    from PIL import Image
    import os
    def open(images):
        images = images.lstrip()
        images = images.rstrip()
        im = Image.open("images/" + images)
        im1 = im.resize((320,320))
        im1.save("images/" + images, "png")
        im3 = im.resize((240,240))
        im3.save("images/" + images, "png")
        list = []
        for y in range(4): 
            for x in range(4):
                loc = (x * 80,y * 80,(x+1)*80,(y+1)*80)
                list.append(loc)
        shuffle(list)
        num = 0
        for n in list:
            im2 = im1.crop(box = n)
            im2.save("images/h"+ str(num)+ ".png")
            num += 1
        return images
    #}  

    music.play("nice.mp3")

    WIDTH = 960
    HEIGHT = 720

    #每个碎片角色的坐标列表out
    #{
    out = [
          [164, 129],
          [253, 129],
          [344, 129],
          [434, 129],
          [527, 129],
          [617, 129],
          [708, 129],
          [797, 129],
          [163, 218],
          [253, 218],
          [343, 218],
          [433, 218],
          [527, 218],
          [617, 218],
          [707, 218],
          [797, 218]
        ]
    #}

    me = open("flower1.png") #open函数可以把任意图片切分为16块
    a = Actor(me, [700, 495])
    bg = Actor("bg.png")

    #使用for循环创建角色
    chips = []
    for i in range(16):
       
        c = Actor("h" +str(i) + ".png", out[i])
        chips.append(c)

    def draw():
        bg.draw()
        a.draw()

        for g in chips:
            g.draw()
    pic = None
    def on_mouse_down(pos):
        global pic
        #遍历角色列表,判断是否被点击
        for p in chips:
            if p.collidepoint(pos):
                pic = p
        
    def on_mouse_move(pos):
        if pic != None:
            pic.pos = pos
            
    def on_mouse_up(pos):
        global pic
        
        #114~118行是吸附的逻辑
        if pic != None:
            for e in empty:
                if pic.collidepoint(e):
                    pic.pos = e
                    break
                
        pic = None
     
    go()

  • 相关阅读:
    使用 MAUI 在 Windows 和 Linux 上绘制 PPT 的图表
    php实现选择排序法
    FPGA_IIC代码-正点原子 野火 小梅哥 特权同学对比写法(2)
    以太坊:“攀登巅峰超越比特币!引领加密货币世界的新时代“
    从零开始学安卓笔记:Android基础知识点
    搜维尔科技:远程操作,遥操作机器人进行 TCP点胶演示
    文本标注工具doccano无法进入后台管理系统admin
    docker使用笔记
    Python VScode 配置
    STM32的HAL库的定时器使用
  • 原文地址:https://blog.csdn.net/fqfq123456/article/details/126150258