Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当物体碰撞时播放声音_Python_Python 3.x_Pygame_Virtualbox_Ubuntu 18.04 - Fatal编程技术网

Python 当物体碰撞时播放声音

Python 当物体碰撞时播放声音,python,python-3.x,pygame,virtualbox,ubuntu-18.04,Python,Python 3.x,Pygame,Virtualbox,Ubuntu 18.04,当对象球与愤怒球碰撞时,我试图播放哎哟声音,但声音并不总是播放。当两个对象位于相同的x或y坐标上时,它会在第一次碰撞时开始,但之后在将来的碰撞中无法正确播放。 也许是我的错,我怎么处理他们的坐标来检查碰撞 import pygame import os import random size = width, height = 750, 422 screen = pygame.display.set_mode(size) img_path = os.path.join(os.getcwd())

当对象
愤怒球
碰撞时,我试图播放
哎哟
声音,但声音并不总是播放。当两个对象位于相同的x或y坐标上时,它会在第一次碰撞时开始,但之后在将来的碰撞中无法正确播放。 也许是我的错,我怎么处理他们的坐标来检查碰撞

import pygame
import os
import random

size = width, height = 750, 422
screen = pygame.display.set_mode(size)

img_path = os.path.join(os.getcwd())
background_image = pygame.image.load('background.jpg').convert()
bg_image_rect = background_image.get_rect()
pygame.mixer.pre_init(44100, 16, 2, 4096)

pygame.display.set_caption("BallGame")

class Ball(object):
    def __init__(self):
        self.image = pygame.image.load("ball.png")
        self.image_rect = self.image.get_rect()
        self.image_rect.x
        self.image_rect.y
        self.facing = 'LEFT'

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN] and self.image_rect.y < 321:
            self.facing = 'DOWN'
            self.image_rect.y += dist
        elif key[pygame.K_UP] and self.image_rect.y > 0:
            self.facing = 'UP'
            self.image_rect.y -= dist
        if key[pygame.K_RIGHT] and self.image_rect.x < 649:
            self.facing = 'RIGHT'
            self.image_rect.x += dist
        elif key[pygame.K_LEFT] and self.image_rect.x > 0:
            self.facing = 'LEFT'
            self.image_rect.x -= dist

    def draw(self, surface):
        if self.facing == "RIGHT":
            surface.blit(pygame.transform.flip(self.image, True, False),(self.image_rect.x,self.image_rect.y))
        elif self.facing == "DOWN":
            surface.blit(pygame.image.load("ball_down.png"),(self.image_rect.x,self.image_rect.y))
        if self.facing == "UP":
            surface.blit(pygame.image.load("ball_up.png"),(self.image_rect.x,self.image_rect.y))
        elif self.facing == "LEFT":
            surface.blit(self.image,(self.image_rect.x,self.image_rect.y))


mob_images = [pygame.image.load("image1.png").convert_alpha(),pygame.image.load("image2.png").convert_alpha(),pygame.image.load("image3.png").convert_alpha(),pygame.image.load("image4.png").convert_alpha(),pygame.image.load("image5.png").convert_alpha()]

class Angryball(pygame.sprite.Sprite):
    def __init__(self, mob_images, pos_x, pos_y):
        super(Angryball, self).__init__()
        self.mob_images = mob_images
        self.image = random.choice(self.mob_images)
        self.rect = self.image.get_rect(x=pos_x, y=pos_y)
        self.facing = 'LEFT'

    def update(self, screen):
        if self.rect.x <= 0:
            self.rect.right = screen.get_rect().width
            self.rect.top = random.randint(0, screen.get_rect().height)
            self.image = random.choice(self.mob_images)
        else:
            self.rect.move_ip(-5, 0)

pygame.init()
screen = pygame.display.set_mode((750, 422))

ball = Ball()
angryball = Angryball(mob_images , 700, random.randrange(400))
sprites = pygame.sprite.Group()
sprites.add(angryball)

clock = pygame.time.Clock()

pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play(-1, 0.0)
ouch = pygame.mixer.Sound("border_sound.wav")

running = True
while running:
    esc_key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if esc_key[pygame.K_ESCAPE]:
            pygame.display.quit()
            pygame.quit()
            running = False

    if ball.image_rect.x == angryball.rect.x:
        ouch.play()
    if ball.image_rect.y == angryball.rect.y:
        ouch.play()


    ball.handle_keys()

    screen.blit(background_image, bg_image_rect)
    screen.blit(background_image, bg_image_rect.move(bg_image_rect.width, 0))
    bg_image_rect.move_ip(-2, 0)
    if bg_image_rect.right <= 0:
        bg_image_rect.x = 0

    sprites.update(screen)
    sprites.draw(screen)

    ball.draw(screen)
    pygame.display.update()

    clock.tick(60)
导入pygame
导入操作系统
随机输入
尺寸=宽度,高度=750422
screen=pygame.display.set_模式(大小)
img_path=os.path.join(os.getcwd())
background\u image=pygame.image.load('background.jpg').convert()
bg_image_rect=background_image.get_rect()
pygame.mixer.pre_init(44100,16,24096)
pygame.display.set_标题(“球赛”)
类球(对象):
定义初始化(自):
self.image=pygame.image.load(“ball.png”)
self.image\u rect=self.image.get\u rect()
self.image_rect.x
自映像
self.faceting='LEFT'
def手柄_键(自):
key=pygame.key.get_pressed()
距离=5
如果键[pygame.K_DOWN]和self.image_rect.y<321:
self.faceting='DOWN'
自映像_rect.y+=dist
elif键[pygame.K_UP]和self.image_rect.y>0:
self.faceting='UP'
自映像\u rect.y-=距离
如果键[pygame.K_RIGHT]和self.image_rect.x<649:
self.faceting='RIGHT'
self.image_rect.x+=dist
elif键[pygame.K_LEFT]和self.image_rect.x>0:
self.faceting='LEFT'
self.image_rect.x-=距离
def绘图(自、表面):
如果self.faceting==“RIGHT”:
blit(pygame.transform.flip(self.image,True,False),(self.image\rect.x,self.image\rect.y))
elif self.facing==“向下”:
blit(pygame.image.load(“ball\u down.png”),(self.image\rect.x,self.image\u rect.y))
如果self.faceting==“向上”:
blit(pygame.image.load(“ball\u up.png”),(self.image\rect.x,self.image\u rect.y))
elif self.faceting==“LEFT”:
blit(self.image,(self.image\rect.x,self.image\rect.y))
mob_images=[pygame.image.load(“image1.png”).convert_alpha(),pygame.image.load(“image2.png”).convert_alpha(),pygame.image.load(“image3.png”).convert_alpha(),pygame.image.load(“image5.png”).convert_alpha()]
类愤怒球(pygame.sprite.sprite):
定义初始(自我、移动图像、位置x、位置y):
超级(愤怒的、自我的)。\uuuuu init\uuuuuuu()
self.mob\u images=mob\u images
self.image=random.choice(self.mob_图像)
self.rect=self.image.get_rect(x=pos_x,y=pos_y)
self.faceting='LEFT'
def更新(自我,屏幕):

如果self.rect.x如建议的那样,我将碰撞检测替换为

如果ball.image_rect.collide rect(angryball.rect):


它起作用了

您还可以检查特定点的碰撞,例如:

ball.rect.collidepoint(angryball.rect.x, angryball.rect.y)

它所做的是测试一个点是否在精灵的矩形内。我发现它工作得很好,很简单,使用和理解都很清楚。

您需要替换这些行

if ball.image_rect.x == angryball.rect.x:
    ouch.play()
if ball.image_rect.y == angryball.rect.y:
    ouch.play()
使用此选项(检查两个矩形是否碰撞):

现在的问题是,声音将在两个物体碰撞的每一帧播放,因此声音会变得相当大(最多可同时在8个通道中播放)。此外,如果所有通道都已被占用,则如果几个对象在短时间内连续碰撞,则可能会跳过声音播放

为了防止出现这种情况,您可以为
Angryball
赋予
collized
属性(布尔值),并在第一次碰撞后将其设置为
True
。然后在一段时间间隔后或在重置位置的同时将其重置为
False

if ball.image_rect.colliderect(angryball.rect) and not angryball.collided:
    angryball.collided = True
    ouch.play()

我没有时间查看完整的atm程序,但尝试用
if ball.image\u rect.collide rect(angryball.rect):
替换碰撞检测。检查两个对象的矩形是否碰撞。看起来还有更多的问题,但这可能是朝着正确方向迈出的一步。谢谢你,它成功了
if ball.image_rect.colliderect(angryball.rect) and not angryball.collided:
    angryball.collided = True
    ouch.play()