python pygame鼠标点击_pygame应用(鼠标事件和键盘事件)

鼠标事件的应用:

import pygame

#写一个函数判断一个点是否在指定范围内

def is_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

if __name__ == '__main__':

pygame.init()

screen = pygame.display.set_mode((600,600))

screen.fill((255,255,255))

pygame.display.set_caption('图片拖拽')

#显示一张图片

image = pygame.image.load('./luo.jpg')

image_x=100

image_y=100

screen.blit(image,(image_x,image_y))

pygame.display.flip()

# 用来存储图片是否可以移动

is_move =False

while 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_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))

x,y = event.pos

image_w,image_h=image.get_size()

#保证鼠标在图片的中心

image_x=x-image_h/2

image_y=y-image_w/2

screen.blit(image,(image_x,image_y))

pygame.display.up

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值