Pygame鼠标进行图片的移动与缩放怎么实现
发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,本篇内容主要讲解"Pygame鼠标进行图片的移动与缩放怎么实现",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Pygame鼠标进行图片的移动与缩放怎么实现"
千家信息网最后更新 2025年11月16日Pygame鼠标进行图片的移动与缩放怎么实现
本篇内容主要讲解"Pygame鼠标进行图片的移动与缩放怎么实现",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Pygame鼠标进行图片的移动与缩放怎么实现"吧!
# -*- coding: UTF-8 -*-#!/usr/bin/env python3# @Time : 2021.12# @Author : 高二水令# @Software: 图层拖拽缩放import osimport sysimport pygamefrom 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 Falsedef 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鼠标进行图片的移动与缩放怎么实现"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
鼠标
移动
图片
位置
范围
内容
按钮
状态
学习
运行
实用
更深
上层
事件
信息
兴趣
函数
功能
图像
实用性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
违反网络安全法的处罚方式
信承诺互联网科技有限公司
群晖网络安全
剑三人口大数据库
怎么匹配多行数据库
临沂谷道网络技术
丽珠集团软件开发
厦门警方组建网络安全志愿者
控制系统软件开发多少钱
军运会志愿者网络安全
网安维护网络安全
金华市欧瑞网络技术有限公司
战地服务器是什么
2021西安网络安全宣传视频
我的世界服务器管理代码
战地5欧服有社区服务器吗
深圳管理软件开发机构
极限网络技术股份有限
互联网科技中国排名
互联网保险新规天利科技
雷蛇的云服务器在哪
软件开发私人合同模板
第二版计算机网络技术
网络安全防护小技巧简图
电脑无法确定服务器dns怎么办
文本框显示数据库
view 安全服务器布置
网络安全工作计划会议记录
云服务器价格恒创
镇江通用软件开发咨询报价