Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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_Collision Detection_Pygame - Fatal编程技术网

Python Pygame中使用精灵的碰撞检测

Python Pygame中使用精灵的碰撞检测,python,collision-detection,pygame,Python,Collision Detection,Pygame,我正在尝试使用碰撞检测来检测鼠标何时碰到我导入的图像。我得到错误“tuple没有属性rect” 这与pygame或python无关,但可能会有所帮助 懒惰的FO有许多很棒的SDL教程,但是它们都是C++的。不过,他们的评论非常好。链接如下:这与pygame或python无关,但可能会有所帮助 懒惰的FO有许多很棒的SDL教程,但是它们都是C++的。不过,他们的评论非常好。它们的链接在这里:问题是pygame.sprite.collide\u rect()需要两个sprite对象。您将向其传递两个

我正在尝试使用碰撞检测来检测鼠标何时碰到我导入的图像。我得到错误“tuple没有属性rect”


这与pygame或python无关,但可能会有所帮助


懒惰的FO有许多很棒的SDL教程,但是它们都是C++的。不过,他们的评论非常好。链接如下:

这与pygame或python无关,但可能会有所帮助


懒惰的FO有许多很棒的SDL教程,但是它们都是C++的。不过,他们的评论非常好。它们的链接在这里:

问题是pygame.sprite.collide\u rect()需要两个sprite对象。您将向其传递两个元组-光标和图像都不是精灵,因此缺少rect属性

您可以使用image_one制作一个精灵,但将光标转换为精灵将更加棘手。我认为手动测试光标是否在图像中会更容易

#Set up the pygame window
screen = pygame.display.set_mode((200,200))
screen.fill((255, 255, 255))

#Set up image properties (would be better to make an object)
image_one = pygame.image.load("image_one.png").convert()
image_x = 225
image_y = 400
image_width = image_one.get_width()
image_height = image_one.get_height()

# Mouse properties
mouse_width = 10
mouse_height = 10

screen.blit(image_one, (image_x, image_y))
pygame.display.flip()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEMOTION:
            mouse_x, mouse_y = pygame.mouse.get_pos()

            # Test for 'collision'
            if image_x - mouse_width < mouse_x < image_x + image_width and image_y - mouse_height < mouse_y < image_y + image_height:
                print 'Hi!'
#设置pygame窗口
screen=pygame.display.set_模式((200200))
屏幕填充((255、255、255))
#设置图像属性(最好创建一个对象)
image\u one=pygame.image.load(“image\u one.png”).convert()
图像_x=225
图像_y=400
image\u width=image\u one.get\u width()
image\u height=image\u one.get\u height()
#鼠标属性
鼠标宽度=10
鼠标高度=10
blit(图像一,(图像x,图像y))
pygame.display.flip()
而1:
对于pygame.event.get()中的事件:
如果event.type==pygame.MOUSEMOTION:
鼠标x,鼠标y=pygame.mouse.get\u pos()
#“碰撞”测试
如果图像x-鼠标宽度<鼠标x<图像x+图像宽度和图像y-鼠标高度<鼠标y<图像y+图像高度:
打印“嗨!”

请注意,在测试鼠标是否在图像中之前,我先测试鼠标是否移动,以避免不必要地重复计算。

问题在于pygame.sprite.collide_rect()接受两个sprite对象。您将向其传递两个元组-光标和图像都不是精灵,因此缺少rect属性

您可以使用image_one制作一个精灵,但将光标转换为精灵将更加棘手。我认为手动测试光标是否在图像中会更容易

#Set up the pygame window
screen = pygame.display.set_mode((200,200))
screen.fill((255, 255, 255))

#Set up image properties (would be better to make an object)
image_one = pygame.image.load("image_one.png").convert()
image_x = 225
image_y = 400
image_width = image_one.get_width()
image_height = image_one.get_height()

# Mouse properties
mouse_width = 10
mouse_height = 10

screen.blit(image_one, (image_x, image_y))
pygame.display.flip()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEMOTION:
            mouse_x, mouse_y = pygame.mouse.get_pos()

            # Test for 'collision'
            if image_x - mouse_width < mouse_x < image_x + image_width and image_y - mouse_height < mouse_y < image_y + image_height:
                print 'Hi!'
#设置pygame窗口
screen=pygame.display.set_模式((200200))
屏幕填充((255、255、255))
#设置图像属性(最好创建一个对象)
image\u one=pygame.image.load(“image\u one.png”).convert()
图像_x=225
图像_y=400
image\u width=image\u one.get\u width()
image\u height=image\u one.get\u height()
#鼠标属性
鼠标宽度=10
鼠标高度=10
blit(图像一,(图像x,图像y))
pygame.display.flip()
而1:
对于pygame.event.get()中的事件:
如果event.type==pygame.MOUSEMOTION:
鼠标x,鼠标y=pygame.mouse.get\u pos()
#“碰撞”测试
如果图像x-鼠标宽度<鼠标x<图像x+图像宽度和图像y-鼠标高度<鼠标y<图像y+图像高度:
打印“嗨!”

请注意,在测试鼠标是否在图像中之前,我先测试鼠标是否移动,以避免不必要地重复计算。

Peter建议,您也发现,在处理碰撞检测时,使用矩形比使用位置更容易

我会更进一步:始终与精灵一起工作

使用sprite,您可以访问
pygame.sprite
中所有方便的碰撞检测功能。如果你决定移动图像,更新位置和动画就容易多了。它还包含图像曲面,全部位于单个对象中。更不用说精灵团体了

精灵还有一个
.rect
属性,因此如果您想使用
mysprite.rect

也就是说,以下是如何从图像中提取精灵:

image_one = pygame.sprite.Sprite()
image_one.image = pygame.image.load("image_one.png").convert()
image_one.rect = pygame.Rect((image_x, image_y), image_one.image.get_size())
image\u two
\u three
等创建更多精灵。或者创建一个函数(或者更好的是,子类
Sprite
),接收位置和文件名作为参数,因此您可以在单行中创建精灵,如下所示:

现在,您可以选择将它们分组:

my_images = pygame.sprite.Group(image_one, image_two, image_three)
绘图非常简单,如下所示:

my_images.draw(screen)
这将同时显示所有图像,每个图像位于各自的位置!酷吧

让我们为鼠标光标创建一个“假”精灵:

mouse = pygame.sprite.Sprite()
mouse.rect = pygame.Rect(pygame.mouse.get_pos(), (1, 1))
我制作了一个1x1精灵,所以它只在鼠标热点上碰撞。请注意,它没有
.image
属性(因此是“假”精灵),但pygame并不在意,因为我们无论如何都不会绘制它

现在最精彩的部分是:

imagehit = pygame.sprite.spritecollideany(mouse, my_images)
print imagehit
在一行中,您对所有图像进行了碰撞测试,您不仅知道鼠标是否与任何图像发生碰撞,而且还知道它与哪个图像发生了碰撞


使用精灵真的很划算;)

Peter建议,而且你也发现,在处理碰撞检测时,使用矩形比使用位置更容易

我会更进一步:始终与精灵一起工作

使用sprite,您可以访问
pygame.sprite
中所有方便的碰撞检测功能。如果你决定移动图像,更新位置和动画就容易多了。它还包含图像曲面,全部位于单个对象中。更不用说精灵团体了

精灵还有一个
.rect
属性,因此如果您想使用
mysprite.rect

也就是说,以下是如何从图像中提取精灵:

image_one = pygame.sprite.Sprite()
image_one.image = pygame.image.load("image_one.png").convert()
image_one.rect = pygame.Rect((image_x, image_y), image_one.image.get_size())
image\u two
\u two
等创建更多精灵。或者创建一个函数(或者更好的是,子类
Sprite
),接收位置和文件名作为参数,这样您就可以在单个l中创建精灵