python - 在不使用 Sprite 的情况下,如何在屏幕底部的移动图像和下落物体之间进行碰撞检测?

标签 python pygame collision-detection

我编写了这段代码,其中有各种物体从屏幕顶部掉落,底部的 Frog 必须尝试捕获它们。我似乎无法按照任何教程对 Frog 和下落物体进行碰撞检测,因为我没有使用 Sprite ,并且由于某种原因,矩形函数也不起作用。我不知道如何继续下去,因为没有教程起作用。如有任何帮助,我们将不胜感激!

import pygame, sys, time, random
from pygame.locals import *

fx1=random.randrange(0,300)
fx2=random.randrange(350,650)
fx3=random.randrange(700,950)
fy1=-50
fy2=-100
fy3=--200
fy1a=-300
fy2a=-400
fy3a=-500
#### fruits######
thingylist= ['fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','naughty1.bmp','naughty2.bmp','naughty3.bmp',]
thing1=pygame.image.load(thingylist[(random.randrange(0,12))])
thing1.set_colorkey(pink)
thing1_rect=thing1.get_rect()
thing1_rect.centerx=(fx1)
thing1_rect.centery=(fy1)
thing2=pygame.image.load(thingylist[(random.randrange(0,12))])
thing2.set_colorkey(pink)
thing2_rect=thing2.get_rect()
thing2_rect.centerx=(fx2)
thing2_rect.centery=(fy2)
thing3=pygame.image.load(thingylist[(random.randrange(0,12))])
thing3.set_colorkey(pink)
thing3_rect=thing3.get_rect()
thing3_rect.centerx=(fx3)
thing3_rect.centery=(fy3)

fx1a=random.randrange(0,300)
fx2a=random.randrange(350,650)
fx3a=random.randrange(700,950)
thing4=pygame.image.load(thingylist[(random.randrange(0,12))])
thing4.set_colorkey(pink)
thing4_rect=thing4.get_rect()
thing4_rect.centerx=(fx1a)
thing4_rect.centery=(fy1a)
thing5=pygame.image.load(thingylist[(random.randrange(0,12))])
thing5.set_colorkey(pink)
thing5_rect=thing5.get_rect()
thing5_rect.centerx=(fx2a)
thing5_rect.centery=(fy2a)
thing6=pygame.image.load(thingylist[(random.randrange(0,12))])
thing6.set_colorkey(pink)
thing6_rect=thing6.get_rect()
thing6_rect.centerx=(fx3a)
thing6_rect.centery=(fy3a)

##thingylist=[thing1,thing2,thing3,thing4,thing5,thing6]
################collision###############






############ initialising sprites##############
frog= pygame.image.load('actual frog.bmp')
frog.set_colorkey(blue)
frog_rect=frog.get_rect()
frog_rect.centerx=(x)
frog_rect.centery=(y)



#########update display function###########
def update(x,y,fx1,fx2,fx3,fx1a,fx2a,fx3a,fy1,fy2,fy3,fy1a,fy2a,fy3a):
    gamedisplay.blit(bg,[0,0])
    gamedisplay.blit(frog,(x,y))
    gamedisplay.blit(thing1,(fx1,fy1))
    gamedisplay.blit(thing2,(fx2,fy2))
    gamedisplay.blit(thing3,(fx3,fy3))
    gamedisplay.blit(thing4,(fx1a,fy1a))
    gamedisplay.blit(thing5,(fx2a,fy2a))
    gamedisplay.blit(thing6,(fx3a,fy3a))
    label=font.render("score "+ str(score) ,1,textcolour)
    gamedisplay.blit(label,(750,10))
    pygame.display.update()
    pygame.time.delay(50)



#########main game loop ############
while running == True:
    gamedisplay.blit(bg,[0,0])
    gamedisplay.blit(frog,(x,y))
    gamedisplay.blit(thing1,(fx1,fy1))
    gamedisplay.blit(thing2,(fx2,fy2))
    gamedisplay.blit(thing3,(fx3,fy3))
    gamedisplay.blit(thing4,(fx1a,fy1a))
    gamedisplay.blit(thing5,(fx2a,fy2a))
    gamedisplay.blit(thing6,(fx3a,fy3a))
    label=font.render("score "+ str(score) ,1,textcolour)
    gamedisplay.blit(label,(750,10))
    pygame.display.flip()
    pygame.event.pump()
    key=pygame.key.get_pressed()


    ############collision detection##########
##    for t in thingylist:
##        if frog.rect.colliderect(thing_rect):
##            score=score+100
update(x,y,fx1,fx2,fx3,fx1a,fx2a,fx3a,fy1,fy2,fy3,fy1a,fy2a,fy3a)

最佳答案

您需要获取每个“事物”的rect,并将其与frog_rect进行比较。 PyGame 函数 pygame.rect.colliderect() 可以轻松检查这一点。对于您当前的代码来说,造成这种困难的原因是您不知道每个事物的 rect,只知道 (x,y)

由于您不想使用 Sprites,我谦虚地建议您重新编写代码,将每个“事物”存储在一个适度的数据结构中,例如事物列表,其中每个事物都是一对 [图像,矩形]。这允许代码简单地循环“事物”列表并对它们执行操作。

# Create some things
thingylist = ['fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','naughty1.bmp','naughty2.bmp','naughty3.bmp',]

all_things = []
for i in range( 10 ):
    new_thing_image  = thingylist[(random.randrange(0,12))]
    new_thing_rect   = new_thing_image.get_rect()
    new_thing_rect.x = random.randrange(0,950)
    new_thing_rect.y = -random.randrange(50,500)
    all_things.append( [ new_thing_image, new_thing_rect ] )

这个事物列表允许代码说,循环遍历列表检查冲突:

def checkCollision( frog_rect, things ):
    """ Find the first thing that collides with frog.
        Return the thing (removing it from the list), or None """
    collides_with = None
    for i in range( len( things) ):
        thing_rect = things[i][1]
        if ( frog_rect.colliderect( thing_rect ) ):
            collides_with = things.pop( i )
    return collides_with

允许代码说:

thing_hit = checkCollision( frog_rect, all_things )
if ( thing_hit != None ):
    # We hit something!
    pass # TODO

也适用于绘制所有东西之类的操作:

def drawThings( things ):
    for item in things:
        thing_image, thing_rect = item
        gamedisplay.blit( thing_image, ( thing_rect.x, thing_rect.y ) )

这比跟踪所有这些单独的坐标要简单得多。

关于python - 在不使用 Sprite 的情况下,如何在屏幕底部的移动图像和下落物体之间进行碰撞检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681941/

相关文章:

python - 如何使用 Selenium WebDriver 按名称定位文本输入?

python - Pygame:按住键时玩家不会移动

python - Pygame 碰撞仅作用于一个对象

tkinter - 如何使用tkinter和pygame使单个Button在Python中播放音乐,暂停音乐和取消暂停音乐?

python - 键未知时访问字典值的简洁方法

python - 使用 python 生成字符串中包含的数值的索引

python - 将 QCheckBox 定位在 QListView 项目的左上角

c++ - OpenGL C++ 圆与圆碰撞

c# - Unity 2d 游戏中奇怪的碰撞错误

c# - 为什么在 Unity 5 中,我的 2D 触发器对撞机只感知与某些对象的碰撞而不感知其他对象的碰撞?