Pygame鼠标进行图片的移动与缩放案例详解
作者:高二水令 发布时间:2023-08-12 15:18:58
标签: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__':并把对应的值传进对应的位置去
来源:https://blog.csdn.net/qq_42619366/article/details/122099454
0
投稿
猜你喜欢
- 写这个文章绝对是偶然的偶然的机会,前年等一回的 元旦节,和 老婆上街 溜达,猛然想起买上一张福利彩票,结果屁都没有中上,开春第一
- django程序,需要写很多api,每个函数都需要几个装饰器,例如@csrf_exempt @require_POST 
- riginal_Data因为程序是为了实现对纯数值型Excel文档进行导入并生成矩阵,因此有必要对第五列文本值进行删除处理。Import_D
- 周末出去爬山,照了一大堆照片回来,照片同时存储为jpg和DNG格式,我用adobe bridge将dng格式的照片中要保留的筛选出来后,就不
- 一、实现创建文件夹和日志#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: nulig
- Python下有许多款不同的 Web 框架。Django是重量级选手中最有代表性的一位。许多成功的网站和APP都基于Django。Djang
- 1. 根据字符串名称 动态调用 python文件内的方法eval("function_name")(参数)2. 根据字符
- 在我们使用一些数据的过程中,我们想要打乱数组内数据的顺序但不改变数据本身,可以通过改变索引值来实现,也就是将索引值重新随机排列,然后生成新的
- 前言本文主要介绍的是关于python中open函数用法的相关资料,用法如下:name = open('errname.txt'
- 你是否曾为表单设计感到过沮丧或不知所措呢?接下来三篇文章,希望能彻底改变你的看法,真正爱上Web表单设计。首先感谢Luke Wroblews
- 一、读取Excel中的数据安装xlrd 只能读取Excel内容pip install xlrd==1.2.0xlrd库的open_workb
- PDOStatement::debugDumpParamsPDOStatement::debugDumpParams — 打印一条 SQL
- 1 栈的概念栈由一系列对象对象组织的一个集合,这些对象的增加和删除操作都遵循一个“后进先出”(Las
- asp生成html如果采用utf8编码方式,多数采用的是Adodb.Stream组件,因为fso并不能支持Utf-8最近在写一个生成HTML
- 本文实例讲述了python时间日期操作方法。分享给大家供大家参考,具体如下:#coding=utf-8import timeimport d
- system默认:managersys默认:change_on_install使用SQL Plus登录数据库时,system使用密码mana
- 在JavaScript中存在这样两种原始类型:Null与Undefined。这两种类型常常会使JavaScript的开发人员产生疑惑,在什么
- 前言:Python主要有三种数据类型:字典、列表、元组。其分别由花括号,中括号,小括号表示。如:字典:dic={'a':12
- 本文实例讲述了JavaScript常用的返回,自动跳转,刷新,关闭语句。分享给大家供大家参考。具体如下:1. Javascript 返回上一
- 代码如下: function astro(birth) astro="" if birth=""