作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了这段代码,其中有各种物体从屏幕顶部掉落,底部的 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/
我是一名优秀的程序员,十分优秀!