Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3-如何在pygame中检测鼠标是否点击图像_Python_Python 3.x_Pygame_Mouse - Fatal编程技术网

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

Python3-如何在pygame中检测鼠标是否点击图像,python,python-3.x,pygame,mouse,Python,Python 3.x,Pygame,Mouse,我正在制作一个游戏,我想得到我放在中间的图像位置。我尝试了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)

我正在制作一个游戏,我想得到我放在中间的图像位置。我尝试了
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()
我的代码包括一些图像和所有,所以我上传了整个图像和所有这些 我使用的是python 3.8&我使用的是linux

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

def on_click(event=None):
    # `command=` calls function without argument
    # `bind` calls function with one argument
    print("image clicked")
返回一个具有曲面对象大小的矩形,该矩形始终从(0,0)开始,因为曲面对象没有位置。矩形的位置可以通过关键字参数指定。例如,可以使用关键字参数
center
指定矩形的中心。这些关键字参数在返回之前应用于的属性(有关关键字参数的完整列表,请参阅)

您已经将图像矩形设置为
imagePosition
。使用
imagePosition
而不是
image.get\u rect()

运行时:
# [...]
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=0
elif event.type==pygame.MOUSEMOTION:
x、 y=event.pos
elif event.type==pygame.MOUSEBUTTONDOWN和event.button==LEFT:
s=s+1#射击次数
shot.play()
如果imagePosition.collidepoint(事件位置):
打印('单击图像')
返回一个带有曲面对象大小的矩形,该矩形始终从(0,0)开始,因为曲面对象没有位置。矩形的位置可以通过关键字参数指定。例如,可以使用关键字参数
center
指定矩形的中心。这些关键字参数在返回之前应用于的属性(有关关键字参数的完整列表,请参阅)

您已经将图像矩形设置为
imagePosition
。使用
imagePosition
而不是
image.get\u rect()

运行时:
# [...]
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=0
elif event.type==pygame.MOUSEMOTION:
x、 y=event.pos
elif event.type==pygame.MOUSEBUTTONDOWN和event.button==LEFT:
s=s+1#射击次数
shot.play()
如果imagePosition.collidepoint(事件位置):
打印('单击图像')

PyGame在单击事件时没有
。不要与PyGame混淆单击事件时没有
。不要把它和