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 initializerself.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 = posrx, ry, rw, rh = rectif (rx <= x <= rx+rw) and (ry <= y <= ry+rh):return Truereturn 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 = 600myimage_y = 480scale_ = pygame.image.load('.\\Avel_scale.tif')scale_ = pygame.transform.scale(scale_, (70, 520))scale_x = 632scale_y = 0screen.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 = 100image_y = 100screen.blit(image,(image_x, image_y))pygame.display.flip()is_move = Falserun_flag = Truewhile (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.posimage_w, image_h = image.get_size()# 保证鼠标在图片的中心image_y = y-image_h/2image_x = x-image_w/2screen.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()
move_image(pic_bottom,pic_upper,ssn)#这里传值调用

预览图大概是这样:

如有需要关于叠图、拼图、透明度代码在这里:python PIL Image 图片叠图、拼接,图片透明度调整
如果对你有帮助的话就点个赞吧

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

  1. JavaFX鼠标拖拽移动图片

    一.鼠标拖拽移动图片 package cn.util;import java.io.File; import javafx.application.Application; import javafx ...

  2. Win7/windows8/win 10系统下Photoshop不能直接拖拽打开图片的解决办法

    我们知道Adobe Photoshop打开图片的方式有很多种,其中有一种是我们经常用到的,那就是直接从资源管理器或者其他地方中直接把图片通过拖拽的方式拖到PS窗口中打开,在Win10系统中,使用PS的 ...

  3. 如何模拟鼠标的拖拽行为.

    如何模拟鼠标的拖拽行为. Delphi / Windows SDK/API http://www.delphi2007.net/DelphiAPI/html/delphi_20061112235944 ...

  4. 通过拖拽改变图片大小

    <!doctype html> <html lang="en"><head><meta charset="utf-8" ...

  5. html5图片拖拽删除,基于jquery插件实现拖拽删除图片功能

    本文实例为大家分享了jquery插件实现拖拽删除图片功能的具体代码,供大家参考,具体内容如下 实现以下效果 完全拖出这个层,图片会消失,否则图片会回到原来的位置 #mydiv{ width:900px ...

  6. 鼠标事件 ——拖拽效果

    目录 关于鼠标事件 拖拽效果 关于鼠标事件 鼠标事件类型:     onclick 单击     ondbclick 双击     oncontextmenu 右侧菜单     onmouseover ...

  7. H5实现类似微信可拖拽左右吸边浮动按钮

    H5实现类似微信可拖拽左右吸边浮动按钮 背景 之前项目需求产品经理要求实现一个可以实时拖拽的按钮,刚好我们移动端H5用的调试工具vconsole也有类似功能,于是研究了一下vconsole里面具体实现 ...

  8. js实现图片拖拽,定点缩放,旋转 (二)

    这篇我们继续来完成图片的定点缩放功能,同样兼容性目前只考虑谷歌,其他类型和版本的自行拓展 1.定点缩放 html部分 <div id="app"><img id= ...

  9. 数码相框项目之显示一张可放大、缩小、拖拽的图片

    之前我做过一个电子相框的项目,涉及到的重难点主要为:在LCD上放大.缩小.移动图片. 首先我们得明白的一点是:无论是放大或缩小,实际上都是对原图进行等比例的缩小,然后在LCD上面显示,只不过缩小的程度 ...

最新文章

  1. Cisco路由器的基本配置
  2. Android各种各样的Drawable-更新中
  3. SQL语句求解同一人物不同日期,某一属性的差值
  4. hdu 3577Fast Arrangement
  5. 转 php include
  6. 宝塔php开启zip组建,宝塔面板如何开启php扩展
  7. Java即时类| hashCode()方法与示例
  8. UITextView 控件-IOS开发
  9. 优化LibreOffice如此简单
  10. ios - 高效,准确的网络检测
  11. 最牛逼android上的图表库MpChart(二) 折线图
  12. SPSS AMOS常用统计软件及科研神器安装包资源【SPSS 006期】
  13. android toast防重_安卓Toast自定义及防止重复显示
  14. 淘宝API接口 item_search - 按关键字搜索淘宝商品
  15. 几个大学生7天涨粉百万,月入超过10万,5000字长文拆解
  16. java自己写母版_Java 创建并用应用幻灯片母版
  17. word中的方括号怎么删_Word如何批量删除习题括号内答案
  18. 基于Android的医院预约挂号系统
  19. mac bootcamp 安装 win7
  20. ubuntu安装java_如何在Ubuntu系统上安装Java

热门文章

  1. 基础数据类型补充 set集合 深浅拷贝
  2. 电脑怎么用c语言写丘比特,C语言丘比特#includebr/#includebr/ 爱问知识人
  3. PRJ: LGA Design
  4. 登康口腔冲刺上市:销售费用远高于研发,旗下品牌包括冷酸灵等
  5. 模拟器连接本地服务器
  6. 深圳python培训学习班
  7. 电脑常用技巧:常用DOS命令大全
  8. 快速清除字符串边上的空格
  9. 2019小程序发展趋势
  10. vue 创建一个 表格,横向纵向都可以增加(减少)行和列,并且内容内容可以输入,标题可以修改