Python3 - 如何在 pygame 中检测鼠标是否点击了图像

Python3 - How to detect mouse clicked on the image or not in pygame

我正在制作游戏,我想获得我放置在中心的图像位置我试过 image.get_rect() 但它返回图像矩形大小

我的代码如下:

import pygame
import random # for random line color
import pygame.mouse

# Random color script
a = random.randrange(1,255,1)
b = random.randrange(1,255,1)
c = random.randrange(1,255,1)
linecolor = a, b, c
#a bunch of variables
bgcolor = 0, 0, 0
x = y = 0
LEFT = 1
running = 1
s = 0

h, w = 640, 400

screen = pygame.display.set_mode((h, w))
# defining image
image = pygame.image.load( "DK.bmp" )
imagePosition = image.get_rect()
imagePosition.bottom = 200
imagePosition.left = 300
screen.blit( image, imagePosition )

pygame.mixer.init()
shot = pygame.mixer.Sound("shot.wav")
         

while running:
     event = pygame.event.poll()
     pygame.display.init()
     # font stuff
     pygame.font.init()
     fontDefault = pygame.font.Font( None, 48 )
     fontScoot = pygame.font.Font("scootchover-sans.ttf",24)
     
     pygame.event.pump()
     pygame.mouse.set_visible(0)
     
     if event.type == pygame.QUIT:
          running = 0
     elif event.type == pygame.MOUSEMOTION:
          x, y = event.pos
     elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
          s = s + 1 # number of shots fired
          shot.play()
          x, y = pygame.mouse.get_pos()

          if image.get_rect().collidepoint(x, y):
               print('clicked on image')

     
     # score board color variables and "if" statements:
     s1 = 66
     s2 = 66
     s3 = 66
     if s > 99:
          s1 = 0
          s2 = 120
          s3 = 0
     if s > 199:
          s1 = 255
          s2 = 120
          s3 = 0
     if s  > 299:
          s1 = 187
          s2 = 0
          s3 = 0
     if s > 399:
          s1 = 255
          s2 = 255
          s3 = 0
     if s > 499:
          s1 = 0
          s2 = 0
          s3 = 255
     if s > 599:
          s1 = 0
          s2 = 255
          s3 = 0
     if s > 699:
          s1 = 128
          s2 = 255
          s3 = 255
     if s > 799:
          s1 = 64
          s2 = 128
          s3 = 128
     if s > 899:
          s1 = 255
          s2 = 255
          s3 = 255
     if s > 999:
          # score board
          s1 = random.randrange(1,255,1)
          s2 = random.randrange(1,255,1)
          s3 = random.randrange(1,255,1)
          # aimer
          a = random.randrange(1,255,1)
          b = random.randrange(1,255,1)
          c = random.randrange(1,255,1)
          linecolor = a, b, c
          ya = random.randrange(1,6,1)
          if ya == 1:
               image = pygame.image.load( "ya.bmp" )
          if ya == 2:
               image = pygame.image.load( "ya2.bmp" )
          if ya == 3:
               image = pygame.image.load( "ya3.bmp" )
          if ya == 4:
               image = pygame.image.load( "ya4.bmp" )
          if ya == 5:
               image = pygame.image.load( "ya5.bmp" )
          if ya == 6:
               image = pygame.image.load( "ya6.bmp" )
     score = fontScoot.render( "Score: " + str(s),1, (s1,s2,s3))
     if s > 999:
          score = fontScoot.render( "OMG!! YOU SCORE IS " + str(s),1, (s1,s2,s3))
     if s > 1999:
          score = fontScoot.render( "HOLY F*CKING SH*T!! YOU SCORE IS " + str(s),1, (s1,s2,s3))

     pygame.display.set_caption("Shooter")

     screen.blit( score, (500,100) )
     screen.fill(bgcolor)
     screen.blit( image, imagePosition )
     pygame.draw.line(screen, linecolor, (x, 0), (x, 399))
     pygame.draw.line(screen, linecolor, (0, y), (639, y))
     screen.blit( score, (100,100) )
     pygame.display.flip()

我的代码包含一些图片,所以我上传了整张图片和所有图片 link 我正在使用 python 3.8 & 我在 linux

在这里试试这个

def on_click(event=None):
    # `command=` calls function without argument
    # `bind` calls function with one argument
    print("image clicked")

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The position of the rectangle can be specified by a keyword argument. For example, the center of the rectangle can be specified with the keyword argument center. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect 关键字参数的完整列表)。

您已经将图像矩形设置为 imagePosition。使用 imagePosition 而不是 image.get_rect():

while running:
    # [...]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = 0
        elif event.type == pygame.MOUSEMOTION:
            x, y = event.pos
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
            s = s + 1 # number of shots fired
            shot.play()
            if imagePosition.collidepoint(event.pos):
                print('clicked on image')