Pygame碰撞一次发生19次-Python3.x

Pygame碰撞一次发生19次-Python3.x,python,pygame,Python,Pygame,我今天刚开始学习pygame,遇到了一些碰撞问题。当飞船大部分时间撞击小行星时,它不会做任何事情,但会同时发生19次碰撞和许多声音效果 from pygame import * import random as rand from colors import * import os pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize pygame.init() #turn all of

我今天刚开始学习pygame,遇到了一些碰撞问题。当飞船大部分时间撞击小行星时,它不会做任何事情,但会同时发生19次碰撞和许多声音效果

from pygame import *
import random as rand
from colors import *
import os
pygame.mixer.pre_init(44100, 16, 2, 4096) #frequency, size, channels, buffersize
pygame.init() #turn all of pygame on.
fps = 60
window_size = window_width, window_height = 800, 600


pygame.mixer.music.load('music.wav')
pygame.mixer.music.play(-1)
explode = pygame.mixer.Sound("explode.wav")


class Player(pygame.sprite.Sprite):
    #sprite for player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("plane.png")
        self.rect = self.image.get_rect()
        self.setprop()
        self.xspeed = 0

    def setprop( self ):
        self.rect = self.image.get_rect()
        self.orgin_x = self.rect.centerx
        self.orgin_y = self.rect.centery

    def set_position(self, x, y):
        self.rect.x = x - self.orgin_x
        self.rect.y = y - self.orgin_y

    def update(self):
        self.xspeed = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.xspeed = -8
        if keystate[pygame.K_RIGHT]:
            self.xspeed = 8
        self.rect.x += self.xspeed

        if self.rect.right > window_width:
            self.rect.right = window_width

        if self.rect.left < 0:
            self.rect.left = 0

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image  = pygame.image.load("boulder.png")
        #self.image.fill(red)
        self.rect   = self.image.get_rect()
        self.rect.x = rand.randrange(window_width - self.rect.width)
        self.rect.y = rand.randrange( -100, -40 )
        self.speed  = rand.randrange(6, 8)
        self.sound = pygame.mixer.Sound("swoosh.wav")

    def play_sound(self):
        self.sound.play()

    def update(self):
        self.rect.y += self.speed
        if self.rect.top > window_height + 10:
            self.rect.x = rand.randrange(window_width - self.rect.width)
            self.rect.y = rand.randrange( -100, -40 )
            self.speed  = rand.randrange(6, 8)




sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
player = Player()
sprites.add(player)
for i in range(12):
    m = Mob()
    mobs.add(m)



player.set_position( window_width/2, window_height/2 +215 )       

pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( window_size, pygame.RESIZABLE )
pygame.display.set_caption('Dodge the Boulders!')
clock = pygame.time.Clock()
explode = pygame.mixer.Sound("explode.wav")


running = True

while ( running ):

    for event in pygame.event.get():
        if( event.type == pygame.QUIT ):
            running = False

    if ( pygame.sprite.collide_rect(m, player) ):
            explode.play()
            print('collide')





    clock.tick( fps )

    window.fill(black)
    sprites.update()
    sprites.draw(window)
    mobs.update()
    mobs.draw(window)
    pygame.display.update()


pygame.display.quit()
pygame.quit()

我在其他文件中使用过这种冲突方法,它似乎起到了作用,我不确定这里的问题是什么。我唯一能想到的是,我没有在某处更新它,但我找不到任何东西。除了播放声音(和打印碰撞)之外,我不希望发生任何其他事情。

您的游戏循环看到碰撞->播放声音。这个循环很快

理解这个问题吗

您可以尝试创建一个全局变量,并在发生冲突时将其设置为
True
,以避免出现多个声音

示例:

crash = False // at global scope

if (pygame.sprite.collide_rect(m, player) and not crash):
    explode.play()
    print('collide')
    crash = True

如果你的游戏在崩溃后仍在继续,请确保在需要时将其重置为
False

一点前就想好了,但决定在这里发布

那么这个

hits = pygame.sprite.spritecollide(player, mobs, False)
if hits:    
    running = False
先吃一个雪碧,然后是一组。(布尔值是dokill)。它基本上只能以这种方式进行测试

但是,这可以对以下组进行测试:

hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
if hits:
    pygame.mixer.Sound.play(zap)

您可以使用spritecollide杀死被碰撞的怪物。然后播放声音,为每个碰撞的怪物添加一个新的怪物

# A list of mobs that have collided with the player.
# Set dokill to True to remove the collided sprites.
collided_mobs = pygame.sprite.spritecollide(player, mobs, True)
# Do something for every collided mob.
for mob_sprite in collided_mobs:
    sound.play()
    mobs.add(Mob())  # Add a new mob to replace the collided.

您的游戏循环看到碰撞->播放声音。这个循环很快。理解这个问题吗?太多的代码…@sudo我该如何修复它?取决于你期望的行为和你的声音。。。只要检测到碰撞,声音应该播放一次还是重复?您需要一个类似布尔值的变量来处理这个似乎不起作用的变量。同样的问题也发生了。脚本打印碰撞的时间只有我与Mob对象碰撞的三分之一左右。
# A list of mobs that have collided with the player.
# Set dokill to True to remove the collided sprites.
collided_mobs = pygame.sprite.spritecollide(player, mobs, True)
# Do something for every collided mob.
for mob_sprite in collided_mobs:
    sound.play()
    mobs.add(Mob())  # Add a new mob to replace the collided.