怎样改才能让鼠标控制图片移动?
import pygame as pg #导入pygame模块,通常为了简便而命名为pg
import sys
import time
pg.init() #初始化
screen = pg.display.set_mode((400, 400)) #建立一个400x400的窗口
pg.display.set_caption("Pygame窗口")
image= pg.image.load("u=4269708609,317730862&fm=253&fmt=auto&app=138&f=JPEG.png")
image = pg.transform.scale(image,(400,400))
clock = pg.time.Clock() #Clock对象可以控制FPS
background_color = (255, 255, 255)
while True:
screen.fill((background_color))
screen.blit(image, (0, 0)) #绘制图片,使图片左上角位于(0, 0)的位置
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit(0)
elif event.type == pg.MOUSEBUTTONDOWN: #按下鼠标:
if event.button == 1:
image = pg.transform.scale(image,(25, 25))
x, y = pg.mouse.get_pos()
#获得鼠标位置
x-= image.get_width() / 2
y-= image.get_height() / 2
#计算光标的左上角位置
screen.blit(image, (x, y))
clock.tick(65) #设置FPS
pg.display.flip()