为什么我的碰撞检测或下降图像不起作用?

问题描述 投票:0回答:1

我最近写了这个代码,其中水果和其他东西从屏幕顶部落下,底部的青蛙必须尝试抓住它们。碰撞检测不起作用,也不是落下的图像,因为它们似乎只是掉落并卡在屏幕的顶部。这是我的大部分代码,因为我似乎无法确定实际错误所在的区域:

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

score=0

#### fruits and naughty ######
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])


################collision###############
def checkCollision (frog_rect,all_things,score):
    collides_with=None
    for i in range (len(all_things)):
        thing_rect=all_things[i][1]
        if (frog_rect.colliderect(thing_rect)):
            score=score+100
    return collides_with



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


####### score###########
pygame.font.init()
font= pygame.font.SysFont ('Dubai MS', 48)

##########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))
    pygame.display.update()
    pygame.time.delay(50)



#########main game loop ############
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()

    ########### escape ###########
    if key [pygame.K_ESCAPE]:
        sys.exit()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    ########### controls ##############        
    if key[pygame.K_LEFT]:
        x -=2

    elif key[pygame.K_RIGHT]:
        x +=2

    elif key[pygame.K_SPACE]or key[pygame.K_UP]:
        for i in range (5):
            y -= jumpvel
            update(x,y,all_things,score)
        for i in range (5):
            y += jumpvel
            update(x,y,all_things,score)
    ######## limits ####################
    if x < 10:
        x = 10
    elif (x > (900 - 2)):
        x= 900-2

    ######### falling###########

    for item in all_things:
        new_thing_image, new_thing_rect= item
        #new_thing_rect=all_things[i][1]
        #thing_rect.y=thing_rect.y+fallingspeed
        new_thing_rect.y+= fallingspeed

    ############collision detection##########
    detect=checkCollision (frog_rect, all_things,score)
    if (detect !=None):
        score=score+100

update(x,y,all_things,score)

“东西”意味着落到屏幕的底部让青蛙抓住,但它们似乎都被卡在了顶端。当我测试碰撞检测代码时,即使两个图像相撞也不会影响分数 - 这意味着某些东西不起作用。

python pygame collision-detection rect
1个回答
0
投票

由于这个原因,下降的图像停止了

fallingspeed = 0.5

qazxsw poi使用整数值来保持位置,因此它会将qazxsw poi舍入为整数 - rect

当你有0.5并且你添加int(0.5) == 0 - 所以你可以期待y = 0 - 它将它圆形到0.5。在下一个循环中,您将再次添加y = 0.5,它将再次将其四舍五入到y = 0。这样它就停在0.5

当你有qazxsw poi并且你添加qazxsw poi然后你会期待y = 0但它绕到y = 0(不是y = -5)所以它正在移动。

使用

0.5

它不会停留在-4.5


-4

有时在更复杂的代码中,将位置保持在浮点值是很重要的,然后你必须使用-5变量而不是fallingspeed = 1 来保持位置并仅在你必须绘制或检查碰撞时将其复制到y = 0


碰撞

old_y = 0 new_y = int(old_y+0.5) print(old_y, new_y) # 0 0 old_y = -5 new_y = int(old_y+0.5) print(old_y, new_y) # -5 -4 里面你设置了float,但是你永远不会把它变成'True

rect

你可以写得更短

rect

现在它应该改变checkCollision

collides_with = None

但是如果你想在 def checkCollision (frog_rect,all_things, score): collides_with = None for i in range (len(all_things)): thing_rect = all_things[i][1] if (frog_rect.colliderect(thing_rect)): score=score+100 collides_with = True return collides_with 中更改它,那么你必须返回值 def checkCollision(frog_rect,all_things, score): collides_with = None for thing_image, thing_rect in all_things: if frog_rect.colliderect(thing_rect): score = score+100 collides_with = True return collides_with score中的变量detect = checkCollision(frog_rect, all_things, score) if detect: score = score+100 是局部变量,它不会改变全局变量checkCollision中的值

score

然后你必须分配给全球qazxsw poi

score
© www.soinside.com 2019 - 2024. All rights reserved.