Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
Python 如何在pygame中检查鼠标与精灵的碰撞?_Python_Pygame - Fatal编程技术网

Python 如何在pygame中检查鼠标与精灵的碰撞?

Python 如何在pygame中检查鼠标与精灵的碰撞?,python,pygame,Python,Pygame,我用十字线替换了鼠标,屏幕上有一些目标。当你射击目标时,它应该从屏幕上消失。如何检查我的十字线(我的实际鼠标)的中心是否与其中一个精灵发生碰撞? 下面的代码没有精确地检查我的十字线的中心是否在一个目标上,所以即使你用十字线中的空白区域射击,它也会计数,我不希望这样 import pygame from random import randrange class Crosshair(pygame.sprite.Sprite): def __init__(self, image):

我用十字线替换了鼠标,屏幕上有一些目标。当你射击目标时,它应该从屏幕上消失。如何检查我的十字线(我的实际鼠标)的中心是否与其中一个精灵发生碰撞? 下面的代码没有精确地检查我的十字线的中心是否在一个目标上,所以即使你用十字线中的空白区域射击,它也会计数,我不希望这样

import pygame
from random import randrange

class Crosshair(pygame.sprite.Sprite):
    def __init__(self, image):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect()
    def update(self):
        self.rect.center = pygame.mouse.get_pos()
    def shoot(self):
        pygame.sprite.spritecollide(self, target_group, True)

class Target(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        super().__init__()
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)

def set_background(background):
    (b_width, b_height) = background.get_size()
    for x in range(0, screen_width, b_width):
        for y in range(0, screen_height, b_height):
            screen.blit(background, (x, y))


#General setup
pygame.init()
fps = 60
clock = pygame.time.Clock()

#Background picture
background = pygame.image.load('Art/bg.png')

#Hide mouse
pygame.mouse.set_visible(False)

#Game Screen
screen_width = 1024
screen_height = 576
screen = pygame.display.set_mode((screen_width, screen_height))


#Crosshair
crosshair_image = pygame.image.load('Art/crosshair.png')
crosshair = Crosshair(crosshair_image)
crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)

#Targets
targets = 10
target_image = pygame.image.load('Art/target.png')
new_target_image = pygame.transform.rotozoom(target_image, 0, 0.5)
target_group = pygame.sprite.Group()
while len(target_group)<10:
    target = Target(new_target_image,
        randrange(0, screen_width),
        randrange(0, screen_height))
    if pygame.sprite.spritecollide(target, target_group, False):
        continue
    target_group.add(target)


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()

    set_background(background)

    target_group.draw(screen)

    crosshair_group.update()
    crosshair_group.draw(screen)
    
    pygame.display.flip()
    clock.tick(fps)

pygame.quit()
导入pygame
从随机输入范围
类十字线(pygame.sprite.sprite):
定义初始化(自我,图像):
super()。\uuuu init\uuuuu()
self.image=image
self.rect=self.image.get_rect()
def更新(自我):
self.rect.center=pygame.mouse.get_pos()
def喷射(自):
pygame.sprite.spritecollide(自我,目标组,真)
类目标(pygame.sprite.sprite):
定义初始化(自、图像、x、y):
super()。\uuuu init\uuuuu()
self.image=image
self.rect=self.image.get_rect()
self.rect.center=(x,y)
def设置_背景(背景):
(b_宽度,b_高度)=背景。获取_大小()
对于范围内的x(0,屏幕宽度,b宽度):
对于范围内的y(0,屏幕高度,b高度):
屏幕光点(背景,(x,y))
#一般设置
pygame.init()
fps=60
clock=pygame.time.clock()
#背景图片
background=pygame.image.load('Art/bg.png')
#隐藏鼠标
pygame.mouse.set_可见(False)
#游戏屏幕
屏幕宽度=1024
屏幕高度=576
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
#十字线
crosshair\u image=pygame.image.load('Art/crosshair.png'))
十字线=十字线(十字线图像)
crosshair_group=pygame.sprite.group()
交叉线组。添加(交叉线)
#目标
目标=10
target\u image=pygame.image.load('Art/target.png')
new_target_image=pygame.transform.rotozoom(target_image,0,0.5)
target_group=pygame.sprite.group()

len(target_group)由于目标是圆形的,因此可以通过计算鼠标指针到目标中心的距离来检测碰撞

计算从鼠标光标到目标中心(
dx*dx+dy*dy
)的平方。测试距离的平方是否小于半径的平方:

将鼠标位置传递到
十字准线。拍摄
。检测到碰撞时,通过目标和目标:

类十字线(pygame.sprite.sprite):
# [...]
def发射(自身、鼠标位置):
对于target_组中的target:
dx=target.rect.centerx-鼠标位置[0]
dy=target.rect.centery-鼠标位置[1]
dist_sq=dx*dx+dy*dy
半径=target.image.get_width()/2
如果距离平方<半径*半径:
target.kill()
running=True
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果event.type==pygame.MOUSEBUTTONDOWN:

十字准星射击(event.pos)#非常感谢@谢谢。不客气。