【问题标题】:Python3 - How to detect mouse clicked on the image or not in pygame [duplicate]Python3 - 如何在pygame中检测鼠标是否点击了图像[重复]
【发布时间】:2020-12-07 00:52:11
【问题描述】:

我正在制作一个游戏,我想获得我放在中心的图像位置,我试过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 上

【问题讨论】:

    标签: python python-3.x pygame mouse


    【解决方案1】:

    pygame.Surface.get_rect.get_rect() 返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始,因为 Surface 对象没有位置。矩形的位置可以由关键字参数指定。例如,可以使用关键字参数center 指定矩形的中心。这些关键字参数在返回之前应用于pygame.Rect 的属性(有关关键字参数的完整列表,请参见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')
    

    【讨论】:

      【解决方案2】:

      来试试这个

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      相关资源
      最近更新 更多