• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pygame学习笔记——检测鼠标碰到、点击图片

武飞扬头像
IT技术学习
帮助1

一、碰到效果实现思路

1.坐标检测

时刻检测鼠标的坐标,有没有移到目标图片的坐标范围内,此方法无法准确描述不规则图像的坐标范围。

2.精灵碰撞检测

创建一个鼠标精灵类,创建鼠标对象,一直跟着鼠标移动,鼠标对象==鼠标指针

检测鼠标对象精灵,有没有和其他精灵碰撞即可。

二、碰到效果代码实现

1.案例说明

一个飞机精灵 (方向键可控制移动).

一个鼠标精灵,鼠标精灵跟随鼠标。

检测两个精灵对象有无碰撞,碰撞就退出。

2.代码

  1.  
    import pygame,sys
  2.  
     
  3.  
    pygame.init()#pygame库的初始化
  4.  
     
  5.  
    root_sf = pygame.display.set_mode((480,600))#创建窗口,设置大小
  6.  
     
  7.  
    '''
  8.  
    1.初始化 __init__
  9.  
    2.重写update
  10.  
    3.image、rect属性或变量
  11.  
    '''
  12.  
    #战斗机
  13.  
    class Plane(pygame.sprite.Sprite):
  14.  
    def __init__(self):
  15.  
    pygame.sprite.Sprite.__init__(self)
  16.  
    self.image = pygame.Surface((50,50))
  17.  
    self.image.fill('green')
  18.  
    self.rect = self.image.get_rect()#rect移动的方式更多
  19.  
    self.rect.center = (220,550)#初始化位置,updata方法会将突破移到这个位置
  20.  
     
  21.  
    def update(self):
  22.  
    keys = pygame.key.get_pressed()
  23.  
     
  24.  
    if keys[pygame.K_LEFT] and self.rect.left > 0:
  25.  
    self.rect = self.rect.move((-10,0))
  26.  
    elif keys[pygame.K_RIGHT] and self.rect.right < root_sf.get_width():
  27.  
    self.rect = self.rect.move((10,0))
  28.  
    elif keys[pygame.K_UP] and self.rect.top > 0:
  29.  
    self.rect = self.rect.move((0,-10))
  30.  
    elif keys[pygame.K_DOWN] and self.rect.bottom < root_sf.get_height():
  31.  
    self.rect = self.rect.move((0,10))
  32.  
     
  33.  
    root_sf.blit(self.image,self.rect)
  34.  
     
  35.  
     
  36.  
    #鼠标类
  37.  
    class Mouse(pygame.sprite.Sprite):
  38.  
    def __init__(self):
  39.  
    self.image = pygame.Surface((1,1))
  40.  
    self.image.fill('red')
  41.  
    self.rect = self.image.get_rect()
  42.  
    self.rect.center = pygame.mouse.get_pos()#初始位置到鼠标指针
  43.  
    def update(self):
  44.  
    self.rect.center = pygame.mouse.get_pos()#移到鼠标指针位置
  45.  
    root_sf.blit(self.image, self.rect)
  46.  
     
  47.  
    #创建鼠标精灵
  48.  
    mouse = Mouse()
  49.  
     
  50.  
    wj_plane = Plane()
  51.  
     
  52.  
    clock = pygame.time.Clock()
  53.  
    while True:#阻止窗口关闭
  54.  
    #事件判断
  55.  
    for event in pygame.event.get():
  56.  
    if event.type == pygame.QUIT:
  57.  
    sys.exit()
  58.  
     
  59.  
    #更新画面
  60.  
    root_sf.fill('black')
  61.  
    mouse.update()
  62.  
    wj_plane.update()
  63.  
    #检测玩家飞机是否碰到鼠标指针
  64.  
    if pygame.sprite.collide_mask(mouse, wj_plane):
  65.  
    sys.exit()#玩家飞机碰到鼠标指针,就退出
  66.  
     
  67.  
    #刷新屏幕
  68.  
    pygame.display.flip()
  69.  
    clock.tick(30)
学新通

三、点击效果

碰到鼠标 且 鼠标点击 == 图片被点击

1.代码实现

  1.  
    import pygame,sys
  2.  
     
  3.  
    pygame.init()#pygame库的初始化
  4.  
     
  5.  
    root_sf = pygame.display.set_mode((480,600))#创建窗口,设置大小
  6.  
     
  7.  
    '''
  8.  
    1.初始化 __init__
  9.  
    2.重写update
  10.  
    3.image、rect属性或变量
  11.  
    '''
  12.  
    #战斗机
  13.  
    class Plane(pygame.sprite.Sprite):
  14.  
    def __init__(self):
  15.  
    pygame.sprite.Sprite.__init__(self)
  16.  
    self.image = pygame.Surface((50,50))
  17.  
    self.image.fill('green')
  18.  
    self.rect = self.image.get_rect()#rect移动的方式更多
  19.  
    self.rect.center = (220,550)#初始化位置,updata方法会将突破移到这个位置
  20.  
     
  21.  
    def update(self):
  22.  
    keys = pygame.key.get_pressed()
  23.  
     
  24.  
    if keys[pygame.K_LEFT] and self.rect.left > 0:
  25.  
    self.rect = self.rect.move((-10,0))
  26.  
    elif keys[pygame.K_RIGHT] and self.rect.right < root_sf.get_width():
  27.  
    self.rect = self.rect.move((10,0))
  28.  
    elif keys[pygame.K_UP] and self.rect.top > 0:
  29.  
    self.rect = self.rect.move((0,-10))
  30.  
    elif keys[pygame.K_DOWN] and self.rect.bottom < root_sf.get_height():
  31.  
    self.rect = self.rect.move((0,10))
  32.  
     
  33.  
    root_sf.blit(self.image,self.rect)
  34.  
     
  35.  
     
  36.  
    #鼠标类
  37.  
    class Mouse(pygame.sprite.Sprite):
  38.  
    def __init__(self):
  39.  
    self.image = pygame.Surface((1,1))
  40.  
    self.image.fill('red')
  41.  
    self.rect = self.image.get_rect()
  42.  
    self.rect.center = pygame.mouse.get_pos()#初始位置到鼠标指针
  43.  
    def update(self):
  44.  
    self.rect.center = pygame.mouse.get_pos()#移到鼠标指针位置
  45.  
    root_sf.blit(self.image, self.rect)
  46.  
     
  47.  
    #创建鼠标精灵
  48.  
    mouse = Mouse()
  49.  
     
  50.  
    wj_plane = Plane()
  51.  
     
  52.  
    clock = pygame.time.Clock()
  53.  
    while True:#阻止窗口关闭
  54.  
    #事件判断
  55.  
    for event in pygame.event.get():
  56.  
    if event.type == pygame.QUIT:
  57.  
    sys.exit()
  58.  
     
  59.  
    #更新画面
  60.  
    root_sf.fill('black')
  61.  
    mouse.update()
  62.  
    wj_plane.update()
  63.  
    #检测玩家飞机是否碰到鼠标指针 且 鼠标点击
  64.  
    if pygame.sprite.collide_mask(mouse, wj_plane) and pygame.mouse.get_pressed()[0]:
  65.  
    sys.exit()#玩家飞机是否碰到鼠标指针 且 鼠标点击,就退出
  66.  
     
  67.  
    #刷新屏幕
  68.  
    pygame.display.flip()
  69.  
    clock.tick(30)
学新通

2.鼠标点击事件参考

pygame.mouse — pygame v2.1.1 documentation

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggcfka
系列文章
更多 icon
同类精品
更多 icon
继续加载