Pygame鼠标进行图片的移动与缩放怎么实现(pygame,开发技术)

时间:2024-05-06 00:23:14 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :
#-*-coding:UTF-8-*-#!/usr/bin/envpython3#@Time:2021.12#@Author:高二水令#@Software:图层拖拽缩放importosimportsysimportpygamefrompygame.localsimport*classBackground(pygame.sprite.Sprite):def__init__(self,image_file,location):pygame.sprite.Sprite.__init__(self)#callSpriteinitializerself.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)defis_in_rect(pos,rect):x,y=posrx,ry,rw,rh=rectif(rx<=x<=rx+rw)and(ry<=y<=ry+rh):returnTruereturnFalsedefmove_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):foreventinpygame.event.get():ifevent.type==pygame.QUIT:exit()#鼠标按下、让状态变成可以移动ifevent.type==pygame.MOUSEBUTTONDOWN:w,h=image.get_size()ifis_in_rect(event.pos,(image_x,image_y,w,h)):is_move=True#鼠标弹起、让状态变成不可以移动ifevent.type==pygame.MOUSEBUTTONUP:is_move=False#鼠标移动对应的事件ifevent.type==pygame.MOUSEMOTION:ifis_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() #鼠标按钮响应、是点击图片的位置范围进行跳转ifevent.type==pygame.MOUSEBUTTONDOWNandmyimage_x<=event.pos[0]<=myimage_x+90and\myimage_y<=event.pos[1]<=myimage_y+40:#判断鼠标位置以及是否摁了下去 #这里可以写按钮响应的功能pygame.quit()#关闭原来窗口#os.system('ui.py')run_flag=False#跳出循环(不然会报错)#sys.exit()#滚轮缩放ifevent.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()

预览图大概是这样:

Pygame鼠标进行图片的移动与缩放怎么实现

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

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Pygame鼠标进行图片的移动与缩放怎么实现的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何基于vue.js实现的分页下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18