如何确保两个对象之间的碰撞只注册一次,并且发生碰撞时所做的任何更改在碰撞结束后仍然存在? [重复]

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

我正在开发一款游戏,它依赖于两个物体的碰撞来运行游戏。 但是,我注意到碰撞不是只发生一次,而是在碰撞发生时发生无限次,因为一个物体穿过另一个物体。

有没有办法保证碰撞只被注册一次?

除此之外,每次碰撞都需要导致我游戏中的生命丢失,因此需要更新图像,但是,一旦碰撞结束,对剩余生命数所做的更改就会返回到原来三生。 问题是有可能发生碰撞的三个单独的行,并且用户可以以任何随机顺序与每一行碰撞,所以我不确定这是否会在创建代码时产生差异。

这可能真的很简单,但是我怎样才能确保即使在碰撞发生后这些变化仍然存在?

我尝试将 pygame.display.update() 更改为 pygame.display.flip() 以防万一这导致了问题,但我不相信它是。

这是我目前的代码:

global n
n = 3
                        
if p_rect.colliderect(n_rect): #collision between player and the net in top row
    collision()
    n = n - 1
    pygame.display.flip() #updated only a small area of the screen
                            
                                
if p_rect.colliderect(n_rect2): #collision between player and the net in middle row
   collision()
   n = n - 1
   pygame.display.flip() #updated only a small area of the screen
                            
                            
if p_rect.colliderect(n_rect3): #collision between player and the net in bottom row
   collision()
   n = n - 1
   pygame.display.flip() #updated only a small area of the screen

碰撞功能也可以在下面看到:

def collision():
    global n
    if n == 3:
        heart2 = pygame.image.load("heart2.png")
        heart2 = pygame.transform.rotozoom(heart2,0,0.2)
        heart2_x = 0
        screen.blit(heart2,(heart2_x,0))
        pygame.display.flip() #updated only a small area of the screen
    if n == 2:
        heart1 = pygame.image.load("heart1.png")
        heart1 = pygame.transform.rotozoom(heart1,0,0.2)
        heart1_x = 0
        screen.blit(heart1,(heart1_x,0))
        pygame.display.flip() #updated only a small area of the screen
    if n == 1:
        image = pygame.image.load("Game_Over.png") #inputs an image for the pre-game preparation
        image = pygame.transform.scale(image, (640,480)) #scales to image to fit the screen
        pygame.display.flip()#updated only a small area of the screen
        while True:
            screen.blit(image,(0,0)) #sets the image coordinates at (0,0)
            pygame.display.update() #updates display
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.display.quit()
                    exit() #exits the game
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.pos[0] in range (125, 250) and event.pos[1] in range (405, 442): #shows the positioning of the username button using x and y axis
                        game_level_1()
                    if event.pos[0] in range (262, 386) and event.pos[1] in range (405, 442): #shows the positioning of the username button using x and y axis
                        menu()
                    if event.pos[0] in range (399, 525) and event.pos[1] in range (405, 442): #shows the positioning of the username button using x and y axis
                        sign_in()
    n = n - 1

如果有人对如何解决此问题有任何想法,请告诉我,这是阻碍我玩游戏的唯一问题。

提前谢谢你!

python pygame collision-detection game-development side-scroller
© www.soinside.com 2019 - 2024. All rights reserved.