【问题标题】:How do you run a function when you click on an image in pygame?在pygame中单击图像时如何运行函数?
【发布时间】:2020-08-13 18:29:27
【问题描述】:

当您在 pygame 中单击图像时,您将如何运行函数?在我的程序中,我想在您单击某个图像时运行特定功能。问题是,我不知道该怎么做。甚至有可能做到这一点吗?下面是我的代码...

import pygame


black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
pygame.init()
size = (500, 400)

screen = pygame.display.set_mode(size)
pygame.draw.rect(screen, red,(150,450,100,50))
button1 = pygame.Rect(100,100,50,50)
button2 = pygame.Rect(200,200,50,50)
button3 = pygame.Rect(130,250,50,50)
pygame.display.set_caption("Yami no Game")

txt = pygame.image.load('txt.png')
Stxt = pygame.transform.scale(txt,(48,48))

exe = pygame.image.load('exe.jpg')
Sexe = pygame.transform.scale(exe,(48,48))

done = False
clock = pygame.time.Clock()

background_image=pygame.image.load('windows_background.jpg').convert()
 
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            100, 100 = event.pos
            if Sexe.get_rect().collidepoint(100,100):
                print('Runnig thing')
    
    screen.blit(background_image, [0,0])
    screen.blit(Stxt,[100,100])
    screen.blit(Sexe,[250,250])

 

    pygame.display.update()
 

    clock.tick(60)
 

pygame.quit()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    检测鼠标点击,然后在点击发生时检查鼠标的位置,并使用collidepoint函数查看它是否在图像内:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousePos = pygame.mouse.get_pos()
            if Sexe.get_rect().collidepoint(mousePos):
                runFunction()
    

    【讨论】:

      【解决方案2】:

      一般来说,您说的是使用按钮来执行某事。为此,我们需要知道玩家用鼠标点击的位置,测试它是否在描绘图像的区域(我们的“按钮”)内,如果是,则执行一个函数。这是一个小例子:

          # get the mouse clicks
          mouse = pygame.mouse.get_pos()   # position
          click = pygame.mouse.get_pressed()  # left/right click
      
          if img.x + img.width > mouse[0] > img.x and img.y + img.height > mouse[1] > img.y:  # Mouse coordinates checking.
               
               if click[0] == 1: # Left click
                   my_function_on_click()
      

      它要求您的图像对象具有x 和y 坐标以及定义的height 和width。如果您的图像对象具有与您可以在该矩形上调用的大小相同的矩形,或者正如其他答案指出的那样,使用collidepoint 函数会容易得多。

      使用您复制到 cmets 中的代码的最小示例:

      width = 48
      height = 48
      x = 100
      y = 100
      
      exe = pygame.image.load('exe.jpg')
      Sexe = pygame.transform.scale(exe,(width,height))
      
       while not done: 
          screen.blit(Sexe,[x,y]) # blit image
      
          for event in pygame.event.get():        
              if event.type == pygame.QUIT: 
                  done = True 
      
       
          mouse = pygame.mouse.get_pos() 
          position click = pygame.mouse.get_pressed() # left/right click 
      
          # Mouse coordinates checking:
          sexe_rect = Sexe.get_rect()
          if sexe_rect.x + sexe_rect.width > mouse[0] > sexe_rect.x and sexe_rect.y + sexe_rect.height > mouse[1] > sexe_rect.y: 
          # if Sexe.get_rect().collidepoint(mousePos): # Alternative, A LOT shorter and more understandable
           
              if click[0] == 1: # Left click 
                  print("I GOT CLICKED!")
      

      【讨论】:

      • 对不起,我对这个有点陌生,是不是应该看起来像这样?因为存在无效语法错误
      • ``` while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True mouse = pygame.mouse.get_pos() # position click = pygame.mouse.get_pressed() # 左/右点击 if Sexe.[100] + Sexe.(48) > 鼠标[0] > Sexe.[100]和性别。[100] + Sexe.(48) > mouse[1] > Sexe.[100]: # 鼠标坐标检查。 if click[0] == 1: # 左键点击runFunction()#ignore this
      • 我编辑了我的帖子并包含了两个选项 - 手动检查宽度/高度,或使用 collidepoint 进行检查,如其他答案所建议的那样。
      • 感谢您的帮助,但是当我点击图片时没有任何反应。我还使用了collidepoint 函数。
      • 我目前手头没有python,但我建议打印鼠标坐标并手动检查矩形坐标、宽度和长度以查看错误所在。
      猜你喜欢
      • 2014-07-14
      • 2012-09-24
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2014-12-31
      • 2015-12-25
      相关资源
      最近更新 更多