Pygame鼠标进行图片的移动与缩放案例详解

 更新时间:2021年12月24日 14:24:02   作者:高二水令  
pygame是Python的第三方库,里面提供了使用Python开发游戏的基础包。本文将介绍如何通过Pygame实现鼠标进行图片的移动与缩放,感兴趣的可以关注一下

pygame鼠标进行拖拽移动图片、缩放、以及按钮响应 案例

# -*- coding: UTF-8 -*-
#!/usr/bin/env python3
# @Time    : 2021.12
# @Author  : 高二水令
# @Software: 图层拖拽缩放
import os
import sys
import pygame
from pygame.locals import *


class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
# 写一个函数,判断一个点是否在某个范围内
# 点(x,y)
# 范围 rect(x,y,w,h)
def is_in_rect(pos, rect):
    x, y = pos
    rx, ry, rw, rh = rect
    if (rx <= x <= rx+rw) and (ry <= y <= ry+rh):
        return True
    return False
def move_image(pic_bottom,pic_upper,ssn):
#pic_bottom,pic_upper分别是背景图和上层拖拽图层,ssn是我自己设置的路径信息、不需要可以删去、需要直接运行可以改成main()
    pygame.init()
    screen = pygame.display.set_mode((710, 520))
    BackGround = Background(pic_bottom, [0, 0])
    screen.fill((255, 255, 255))
    myimage = pygame.image.load('.\\next.png')
    myimage = pygame.transform.scale(myimage, (90, 40))
    myimage_x = 600
    myimage_y = 480
    scale_ = pygame.image.load('.\\Avel_scale.tif')
    scale_ = pygame.transform.scale(scale_, (70, 520))
    scale_x = 632
    scale_y = 0
    screen.blit(BackGround.image, BackGround.rect)
    screen.blit(scale_, (scale_x, scale_y))
    screen.blit(myimage, (myimage_x, myimage_y))
    pygame.display.set_caption('图像定标')
    size = []
    location = [0, 0]

    image = pygame.image.load(pic_upper)
    image_x = 100
    image_y = 100
    screen.blit(image,(image_x, image_y))
    pygame.display.flip()

    is_move = False
    run_flag = True
    while (run_flag==True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            # 鼠标按下、让状态变成可以移动
            if event.type == pygame.MOUSEBUTTONDOWN:
                w,h = image.get_size()
                if is_in_rect(event.pos, (image_x, image_y, w, h)):
                    is_move = True

            # 鼠标弹起、让状态变成不可以移动
            if event.type == pygame.MOUSEBUTTONUP:
                is_move = False


            # 鼠标移动对应的事件
            if event.type == pygame.MOUSEMOTION:
                if is_move:
                    screen.fill((255, 255, 255))
                    screen.blit(BackGround.image, BackGround.rect)
                    x, y = event.pos
                    image_w, image_h = image.get_size()
                    # 保证鼠标在图片的中心
                    image_y = y-image_h/2
                    image_x = x-image_w/2
                    screen.blit(scale_, (scale_x, scale_y))
                    screen.blit(myimage, (myimage_x, myimage_y))
                    screen.blit(image, (image_x, image_y))
                    #print(image.get_rect())
                    location[0]=event.pos[0]
                    location[1] = event.pos[1]
                    print(event.pos)
                    pygame.display.update()
			#鼠标按钮响应、是点击图片的位置范围进行跳转
            if event.type == pygame.MOUSEBUTTONDOWN and myimage_x <= event.pos[0] <= myimage_x + 90 and \
                    myimage_y <= event.pos[1] <= myimage_y + 40:  # 判断鼠标位置以及是否摁了下去
					#这里可以写按钮响应的功能
					
                    pygame.quit()#关闭原来窗口
                    #os.system('ui.py')
                    run_flag = False#跳出循环(不然会报错)
                    #sys.exit()
             #滚轮缩放
            if event.type == MOUSEWHEEL:
                screen.fill((255, 255, 255))
                screen.blit(BackGround.image, BackGround.rect)
                image_width = image.get_width()
                image_heigt = image.get_height()
                image = pygame.transform.scale(image, (
                    image_width + event.y * image_width / image_heigt * 10, image_heigt + event.y * 10))
                screen.blit(scale_, (scale_x, scale_y))
                screen.blit(myimage, (myimage_x, myimage_y))
                screen.blit(image, (image_x, image_y))
                #print(event)
                print(image_width, image_heigt)
                #print(event.flipped)
                pygame.display.update()


预览图大概是这样:

如需直接运行就直接把def move_image(pic_bottom,pic_upper,ssn)这句改成if __name__ == '__main__':并把对应的值传进对应的位置去 

到此这篇关于Pygame鼠标进行图片的移动与缩放案例详解的文章就介绍到这了,更多相关Pygame图片的移动与缩放内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python中django框架通过正则搜索页面上email地址的方法

    python中django框架通过正则搜索页面上email地址的方法

    这篇文章主要介绍了python中django框架通过正则搜索页面上email地址的方法,涉及django框架及正则表达式的使用技巧,需要的朋友可以参考下
    2015-03-03
  • python RC4加密操作示例【测试可用】

    python RC4加密操作示例【测试可用】

    这篇文章主要介绍了python RC4加密操作,结合实例形式分析了python实现RC4加密功能的具体操作步骤与相关问题解决方法,需要的朋友可以参考下
    2019-09-09
  • Python中实现优雅的switch操作的方法小结

    Python中实现优雅的switch操作的方法小结

    这篇文章主要为大家详细介绍了如何在Python中优雅地实现 switch 操作,并提供丰富的示例代码,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • python使用arcpy.mapping模块批量出图

    python使用arcpy.mapping模块批量出图

    出图是项目里常见的任务,有的项目甚至会要上百张图片,所以批量出土工具很有必要,这篇文章主要介绍了python使用arcpy.mapping模块批量出图,有兴趣的可以了解一下。
    2017-03-03
  • TensorFlow人工智能学习数据类型信息及转换

    TensorFlow人工智能学习数据类型信息及转换

    这篇文章主要为大家介绍了TensorFlow人工智能学习数据类型信息及转换,
    2021-11-11
  • python中prettytable库的使用方法

    python中prettytable库的使用方法

    prettytable是Python的一个第三方工具库,用于创建漂亮的ASCII表格,本文主要介绍了python中prettytable库的使用方法,使用prettytable可以轻松地将数据可视化为表格,感兴趣的可以了解一下
    2023-08-08
  • Django csrf 验证问题的实现

    Django csrf 验证问题的实现

    csrf是通过伪装来自受信任用户的请求来利用受信任的网站。这篇文章主要介绍了Django csrf 验证问题的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • Python海龟绘图(Turtle)应用指南

    Python海龟绘图(Turtle)应用指南

    python2.6版本中后引入的一个简单的绘图工具,叫做海龟绘图(Turtle Graphics)。海龟绘图(turtle库)是python的内部模块,使用前导入即可。本文将展示三个通过海龟绘图实现的小程序,快来跟随小编一起学习吧
    2022-03-03
  • django xadmin中form_layout添加字段显示方式

    django xadmin中form_layout添加字段显示方式

    这篇文章主要介绍了django xadmin中form_layout添加字段显示方式,具有很好的 参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 浅谈Python中的继承

    浅谈Python中的继承

    这篇文章主要介绍了Python中继承的的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06

最新评论