如果不使用精灵,如何在屏幕底部的运动图像和坠落物体之间进行碰撞检测?

2024-10-01 04:57:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了这个代码,其中有各种各样的物体从屏幕顶部掉落,底部的青蛙必须尝试抓住它们。我似乎不能做碰撞检测与青蛙和下降的物体后,任何教程,因为我没有使用精灵和出于某种原因rect函数也不工作。我不知道怎么去了,因为没有教程是工作了。感谢您的帮助

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)

Tags: rectrandompygamerandrangebmpblitfrogthing1
1条回答
网友
1楼 · 发布于 2024-10-01 04:57:37

您需要获得每个“事物”的rect,并将其与frog_rect进行比较。PyGame函数pygame.rect.colliderect()可以很容易地检查这一点。现在的代码很难做到这一点,因为您不知道每件事情的rect,只知道(x,y)

既然您不希望使用sprite,我谦虚地建议您重新编写代码,将每个“东西”存储在一个适度的数据结构中,比如一个事物列表,其中每个事物都是一对[ image, rect ]。这使得代码可以简单地在“事物”列表上循环并对它们执行操作

# 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 ) )

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

相关问题 更多 >