您的位置:首页 > 编程语言

【更新】不到90行代码,pygame从无到有教会你制作有趣的大小图全景效果,不看是你的损失!

2021-05-25 17:57 771 查看

pygame从无到有教会你制作有趣的大小图效果,不看是你的损失,
好的,马上开始。

文章目录

  • (三)截取部分图
  • 二、实现右下角半透明化小图
  • 三、增加小图的选择矩形框
  • 四、实现鼠标单击响应操作
  • 五、实现鼠标移动监听事件
  • 六、完整实现效果
  • 一、实现基本的功能

    (一)先搭个架子

    import pygame,sys
    
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption('大小框展示')
    fcclock = pygame.time.Clock()
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口

    黑黑的框,不截图了。大家都懂。

    (二)直接贴个图

    1、代码如下:

    import pygame,sys
    
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption('大小框展示')
    img = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg').convert_alpha()
    
    fcclock = pygame.time.Clock()
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    screen.blit(img,(0,0))
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口

    2、运行效果

    (三)截取部分图

    1、简单截取

    screen.blit(img,(0,0),(1000,1000,500,500))

    2、让部分截图动起来

    # aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg
    
    import pygame,sys
    
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption('大小框展示')
    fcclock = pygame.time.Clock()
    img = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg').convert_alpha()
    i = 0
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    screen.blit(img,(0,0),(i,1000,500,500))
    i += 1
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口

    3、显示个全图出来吧

    import pygame,sys
    
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption('大小框展示')
    fcclock = pygame.time.Clock()
    img = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg').convert_alpha()
    img = pygame.transform.scale(img, (500, 500))
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    screen.blit(img,(0,0))
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口

    二、实现右下角半透明化小图

    (一)初始化小图surface

    img_small = pygame.transform.scale(img_big, (int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    bgSurface = pygame.Surface((int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    pygame.Surface.convert_alpha(bgSurface)

    (二)循环过程中画小图

    bgSurface.blit(img_small,(0,0))
    screen.blit(bgSurface,(screen.get_rect().width - bgSurface.get_rect().width,screen.get_rect().height - bgSurface.get_rect().height))

    (三)完整代码

    import pygame,sys
    flag = 20
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption('大小框展示')
    fcclock = pygame.time.Clock()
    img_big = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg').convert_alpha()
    print(img_big.get_rect().width,img_big.get_rect().height)
    img_small = pygame.transform.scale(img_big, (int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    bgSurface = pygame.Surface((int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    pygame.Surface.convert_alpha(bgSurface)i = 0
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    screen.blit(img_big,(0,0),(i,1000,500,500))
    bgSurface.set_alpha(220)
    i += 1
    bgSurface.blit(img_small,(0,0))
    screen.blit(bgSurface,(screen.get_rect().width - bgSurface.get_rect().width,screen.get_rect().height - bgSurface.get_rect().height))fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口
    
    

    (四)运行效果

    三、增加小图的选择矩形框

    (一)核心代码

    bgx = screen.get_rect().width - bgSurface.get_rect().width
    bgy = screen.get_rect().height - bgSurface.get_rect().height
    posx = i / img_big.get_rect().width * bgSurface.get_rect().width+bgx
    posy = j / img_big.get_rect().height * bgSurface.get_rect().height+bgy
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    print(rect_rect)
    pygame.draw.rect(screen,(255,0,0),rect_rect,2)

    (二)完整代码

    import pygame,sys
    flag = 20
    pygame.init()
    WIDTH = 500
    HEIGHT = 500
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption('大小框展示')
    fcclock = pygame.time.Clock()
    img_big = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg')
    print(img_big.get_rect().width,img_big.get_rect().height)
    img_small = pygame.transform.scale(img_big, (int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    bgSurface = pygame.Surface((int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    pygame.Surface.convert_alpha(bgSurface)i = 0
    j = 1000
    bgx = screen.get_rect().width - bgSurface.get_rect().width
    bgy = screen.get_rect().height - bgSurface.get_rect().height
    # posx = i/img_big.get_rect().width*bgSurface.get_rect().width
    # posy = j/img_big.get_rect().height*bgSurface.get_rect().height
    # rect_rect = (posx,posy,WIDTH/flag,HEIGHT/flag)
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    screen.blit(img_big,(0,0),(i,j,500,500))
    bgSurface.set_alpha(230)
    bgSurface.blit(img_small,(0,0))
    screen.blit(bgSurface,(bgx,bgy))
    posx = i / img_big.get_rect().width * bgSurface.get_rect().width+bgx
    posy = j / img_big.get_rect().height * bgSurface.get_rect().height+bgy
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    print(rect_rect)
    pygame.draw.rect(screen,(255,0,0),rect_rect,2)
    i += 1
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口
    
    

    (三)运行效果


    上面的都是自动运动的,现在需要实现鼠标响应操作。

    四、实现鼠标单击响应操作

    (一)实现鼠标响应事件

    if event.type == pygame.MOUSEBUTTONDOWN:
    choose_rect = pygame.Rect(bgx,bgy,int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag))
    pos = x, y = pygame.mouse.get_pos()  # 获取鼠标位置,鼠标就是需要打击的目标
    if choose_rect.collidepoint(pos):
    print('in')
    else:
    print('not in')

    (二)实现矩形框点击响应函数

    if event.type == pygame.MOUSEBUTTONDOWN:
    choose_rect = pygame.Rect(bgx,bgy,int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag))
    pos = x, y = pygame.mouse.get_pos()  # 获取鼠标位置,鼠标就是需要打击的目标
    if choose_rect.collidepoint(pos):
    select = True
    print('in')
    if x-12.5>0:
    posx = x-12.5
    else:
    posx = 0
    if y-12.5>0:
    posy = y-12.5
    else:
    posy = 0
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    i = (posx-bgx)*img_big.get_rect().width/bgSurface.get_rect().width
    j = (posy-bgy)*img_big.get_rect().height/bgSurface.get_rect().height
    pygame.draw.rect(screen, (255, 0, 0), rect_rect, 2)
    else:
    print('not in')

    (三)实现效果

    五、实现鼠标移动监听事件

    初始化变量

    select=False

    (一)鼠标按下

    if event.type == pygame.MOUSEBUTTONDOWN:
    choose_rect = pygame.Rect(bgx,bgy,int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag))
    pos = x, y = pygame.mouse.get_pos()  # 获取鼠标位置,鼠标就是需要打击的目标
    if choose_rect.collidepoint(pos):
    select = True
    print('in')
    if x-12.5>0:
    posx = x-12.5
    else:
    posx = 0
    if y-12.5>0:
    posy = y-12.5
    else:
    posy = 0
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    i = (posx-bgx)*img_big.get_rect().width/bgSurface.get_rect().width
    j = (posy-bgy)*img_big.get_rect().height/bgSurface.get_rect().height
    pygame.draw.rect(screen, (255, 0, 0), rect_rect, 2)
    else:
    print('not in')

    (二)鼠标移动

    if event.type == pygame.MOUSEMOTION:
    if select:
    choose_rect = pygame.Rect(bgx,bgy,int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag))
    pos = x, y = pygame.mouse.get_pos()  # 获取鼠标位置,鼠标就是需要打击的目标
    if choose_rect.collidepoint(pos):
    print('in')
    if x-12.5>0:
    posx = x-12.5
    else:
    posx = 0
    if y-12.5>0:
    posy = y-12.5
    else:
    posy = 0
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    i = (posx-bgx)*img_big.get_rect().width/bgSurface.get_rect().width
    j = (posy-bgy)*img_big.get_rect().height/bgSurface.get_rect().height
    pygame.draw.rect(screen, (255, 0, 0), rect_rect, 2)
    else:
    print('not in')

    (三)鼠标松开

    if event.type == pygame.MOUSEBUTTONUP:
    select = False

    (四)完整代码

    import pygame,sys
    flag = 20
    pygame.init()
    WIDTH = 500
    HEIGHT = 500
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption('大小框展示')
    fcclock = pygame.time.Clock()
    img_big = pygame.image.load('./image/aerial-alpine-ceresole-reale-desktop-backgrounds-1562.jpg')
    print(img_big.get_rect().width,img_big.get_rect().height)
    img_small = pygame.transform.scale(img_big, (int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    bgSurface = pygame.Surface((int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag)))
    pygame.Surface.convert_alpha(bgSurface)i = 0
    j = 1000
    bgx = screen.get_rect().width - bgSurface.get_rect().width
    bgy = screen.get_rect().height - bgSurface.get_rect().height
    posx = i/img_big.get_rect().width*bgSurface.get_rect().width+bgx
    posy = j/img_big.get_rect().height*bgSurface.get_rect().height+bgy
    select = False
    # rect_rect = (posx,posy,WIDTH/flag,HEIGHT/flag)
    while True:
    for event in pygame.event.get():
    if event.type == pygame.QUIT or event.type == pygame.K_F1:
    pygame.quit()
    sys.exit()
    if event.type == pygame.MOUSEBUTTONDOWN:
    choose_rect = pygame.Rect(bgx,bgy,int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag))
    pos = x, y = pygame.mouse.get_pos()  # 获取鼠标位置,鼠标就是需要打击的目标
    if choose_rect.collidepoint(pos):
    select = True
    print('in')
    if x-12.5>0:
    posx = x-12.5
    else:
    posx = 0
    if y-12.5>0:
    posy = y-12.5
    else:
    posy = 0
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    i = (posx-bgx)*img_big.get_rect().width/bgSurface.get_rect().width
    j = (posy-bgy)*img_big.get_rect().height/bgSurface.get_rect().height
    pygame.draw.rect(screen, (255, 0, 0), rect_rect, 2)
    else:
    print('not in')
    if event.type == pygame.MOUSEBUTTONUP:
    select = False
    if event.type == pygame.MOUSEMOTION:
    if select:
    choose_rect = pygame.Rect(bgx,bgy,int(img_big.get_rect().width/flag), int(img_big.get_rect().height/flag))
    pos = x, y = pygame.mouse.get_pos()  # 获取鼠标位置,鼠标就是需要打击的目标
    if choose_rect.collidepoint(pos):
    print('in')
    if x-12.5>0:
    posx = x-12.5
    else:
    posx = 0
    if y-12.5>0:
    posy = y-12.5
    else:
    posy = 0
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    i = (posx-bgx)*img_big.get_rect().width/bgSurface.get_rect().width
    j = (posy-bgy)*img_big.get_rect().height/bgSurface.get_rect().height
    pygame.draw.rect(screen, (255, 0, 0), rect_rect, 2)
    else:
    print('not in')
    
    screen.blit(img_big,(0,0),(i,j,500,500))
    bgSurface.set_alpha(230)
    bgSurface.blit(img_small,(0,0))
    screen.blit(bgSurface,(bgx,bgy))
    
    rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    i = (posx - bgx) * img_big.get_rect().width / bgSurface.get_rect().width
    j = (posy - bgy) * img_big.get_rect().height / bgSurface.get_rect().height
    pygame.draw.rect(screen, (255, 0, 0), rect_rect, 2)
    # posx = i / img_big.get_rect().width * bgSurface.get_rect().width+bgx
    # posy = j / img_big.get_rect().height * bgSurface.get_rect().height+bgy
    # rect_rect = (int(posx), int(posy), WIDTH / flag, HEIGHT / flag)
    # # print(rect_rect)
    # pygame.draw.rect(screen,(255,0,0),rect_rect,2)
    # i += 1
    fcclock.tick(60)
    pygame.display.flip()  # 刷新窗口
    

    六、完整实现效果

    可点击、可拖动查看大小图,任君选择,喜欢就好!
    比心,感谢!

    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: 
    相关文章推荐