怎么利用Pygame实现通过鼠标移动和缩放图片
Admin 2022-06-18 群英技术资讯 1082 次浏览
这篇文章给大家介绍了“怎么利用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__':并把对应的值传进对应的位置去
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了pytorch中的nn.Sequential(*net[3: 5])是啥意思,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
大家好,本篇文章主要讲的是python查策网字体反爬实例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
这篇文章主要为大家详细介绍了python tkinter实现定时关机,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
今天带大家来学习selenium库的使用方法及相关知识总结,文中非常详细的介绍了selenium库,对正在学习python的小伙伴很有帮助,需要的朋友可以参考下
pytorch作为深度学习的计算框架正得到越来越多的应用.我们除了在模型训练阶段应用外,最近也把pytorch应用在了部署上.
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008