【问题标题】:why does this code only register certain collisions and not all?为什么这段代码只记录某些冲突而不是全部?
【发布时间】:2019-04-22 22:09:51
【问题描述】:

您好,我有一个游戏代码,其中有多个水果从天而降,底部的青蛙必须尝试抓住它们。当他接住一个时,分数就会上升。这只发生在青蛙与一些水果而不是全部碰撞时。并且在某一点之后,分数随机开始无缘无故地增加。这是大部分代码,因为我不确定错误在哪里:

import pygame, sys, time, random
from pygame.locals import *
from math import fabs
######### constants ##########
jumpvel=20
fallingspeed=1
running= True
blue= [129,183 ,253]
pink=[255,174,201]
textcolour= [255,255,255]
x=700//2
y=1000//2
score=0

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 (12):
    new_thing_image=pygame.image.load(thingylist[(random.randrange(0,12))])
    new_thing_image.set_colorkey(pink)
    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,all_things,score):
    collides_with=None
    for thing_image, thing_rect in all_things:
        if frog_rect.colliderect(thing_rect):
            collides_with=True
    if collides_with == True:
        score= score+100
    return collides_with,score

######## initialising screen#########        
pygame.init()
gamedisplay=pygame.display.set_mode((1000,600)) #making the screen
pygame.display.set_caption('frog')
clock=pygame.time.Clock()# frames per second
bg=pygame.image.load('actual clouds.bmp').convert()


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

##########drawing things#############
def drawThings (all_things):
    for item in all_things:
        new_thing_image, new_thing_rect= item
        gamedisplay.blit(new_thing_image, (new_thing_rect.x, new_thing_rect.y))

#########update display function###########
def update(x,y,all_things,score):
    gamedisplay.blit(bg,[0,0])
    gamedisplay.blit(frog,(x,y))
    for thing in range (len(all_things)):
        new_thing_rect=all_things[i][1]
        #thing_rect.y=thing_rect.y+fallingspeed
        new_thing_rect.y+= fallingspeed
    drawThings(all_things)
    label=font.render("score "+ str(score) ,1,textcolour)
    gamedisplay.blit(label,(750,10))
    gamedisplay.blit(heart1,(750,50))
    gamedisplay.blit(heart2,(850,50))
    gamedisplay.blit(heart2,(800,50))
    pygame.display.update()
    pygame.time.delay(50)

while running == True:
    gamedisplay.blit(bg,[0,0])
    gamedisplay.blit(frog,(x,y))
    drawThings(all_things)
    label=font.render("score "+ str(score) ,1,textcolour)
    gamedisplay.blit(label,(750,10))
    pygame.display.flip()
    pygame.event.pump()
    key=pygame.key.get_pressed()
    for item in all_things:
        new_thing_image, new_thing_rect= item
        new_thing_rect.y+= fallingspeed
        if new_thing_rect.y >450:
            new_thing_rect.x=random.randrange(0,950)
            new_thing_rect.y=-random.randrange(50,500)
    ############collision detection##########
    detect,score =checkCollision (frog_rect, all_things,score)

    update(x,y,all_things,score)

每次它与任何掉落的水果碰撞时,分数都应该增加,而不仅仅是某些水果,而不是开始随机不断地增加。任何帮助将不胜感激谢谢!

【问题讨论】:

  • frog_rect 在哪里更新?青蛙会动吗?碰撞功能看起来不错(除非您只为可能的多次碰撞得分一次)。
  • @Kingsley the frog 正在更新功能中进行更新:gamedisplay.blit(frog,(x,y)) 但我不确定 frog_rect。
  • 查看这个可爱的debug 博客寻求帮助。
  • gamedisplay.blit(frog,(x,y)) 只是将青蛙位图绘制到屏幕(x, y)frog_rect 需要根据青蛙坐标的任何变化进行更新,以使碰撞起作用。这发生了吗?我本来希望青蛙被画成这样:gamedisplay.blit( frog, ( frog_rect.centerx, frog_rect.centery ) ),因为(中心)坐标在frog_rect的初始化中是这样设置的。
  • 不,它不能那样工作。您需要在xy 更改时更新frog_rect,或者仅使用frog_rect 保持精灵的位置。问题的核心是您使用一组坐标作为精灵在屏幕上的位置,另一组坐标用于碰撞矩形(除了最初之外,它永远不会更新)。

标签: python pygame collision-detection


【解决方案1】:

根据代码sn-p-不包括青蛙位置更新代码,我猜问题是:

  • 分数随机增加
  • 碰撞并不总是有效

是由于为青蛙的起始位置定义了一次碰撞矩形,但随后从未随着青蛙位置的变化而更新。

############ initialising sprites##############
frog= pygame.image.load('actual frog.bmp')
frog.set_colorkey(blue)
frog_rect=frog.get_rect()
frog_rect.centerx=(x)       # <-- rect only ever updated here!
frog_rect.centery=(y)

这将导致所描述的症状,因为从碰撞矩形中落下的得分对象(水果?)会增加得分(看似随机),并且当青蛙仍在其原始位置上方时,碰撞将完美运行。如果青蛙部分在矩形上方,它会有点工作,一旦青蛙移开那个起始位置矩形,它就根本不起作用。

解决问题的方法是每当青蛙位置的x & y 更新时,更新矩形frog_rect 的坐标。这可以通过在update() 函数中设置frog_rect.centerxfrog_rect.centery 来实现:

def update(x, y, all_things, score ):
    frog_rect.x = x
    frog_rect.y = y

青蛙的矩形被初始化为.centerx.centery 坐标,但是青蛙被绘制在x,y,所以碰撞矩形也开始有点偏离中心。因此最好也更新初始化函数:

frog_rect=frog.get_rect()
frog_rect.x = x
frog_rect.y = y

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-05
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多