A
A
Ashjin13372021-08-17 15:41:52
Python
Ashjin1337, 2021-08-17 15:41:52

Who knows how to remove the delay when making sound in python (pygame)?

import pygame
from Variables import *

def inter (x1, y1, x2, y2, db1, db2):
    if x1 > x2 - db1 and x1 < x2 + db2 and y1 > y2 - db1 and y1 < y2 + db2:
        #ПРОИГРЫВАНИЕ ЭТОГО ЗВУКА
        pygame.mixer.Sound.play(boom_sound)
        return 1
    else:
        return 0

pygame.init()

clock = pygame.time.Clock()
window = pygame.display.set_mode((600, 500))
screen = pygame.Surface((600, 500))
player = pygame.Surface((40, 60))
zet = pygame.Surface((52, 52))

zet1 = pygame.Surface((52, 52))

zet2 = pygame.Surface((52, 52))

arrow = pygame.Surface((20, 40))
myfont = pygame.font.SysFont('Impact', 20)
img_p = pygame.image.load('Game/player.png')
img_a = pygame.image.load('Game/arrow.png')
img_z = pygame.image.load('Game/zet.png')
img_z1 = pygame.image.load('Game/zet.png')
img_z2 = pygame.image.load('Game/zet.png')
img_bg = pygame.image.load('Game/bg.jpg')
#ВАМ НУЖЕН ЭТОТ ЗВУК
boom_sound = pygame.mixer.Sound('Game/boom.wav')

player.set_colorkey((0, 0, 0))
arrow.set_colorkey((0, 0, 0))
zet.set_colorkey((0, 0, 0))

while done == False:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            done = True
        #движение персонажа
        if e.type == pygame.KEYDOWN and e.key == pygame.K_a:
            x_p -= speed
        if e.type == pygame.KEYDOWN and e.key == pygame.K_d:
            x_p += speed

        #стрела
        if e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE:
            if strike == False:
                strike = True
                a_x = x_p
                a_y = y_p - 40

    if strike:
        a_y -= 1.5
        if a_y < 0:
            strike = False
            a_y = 1000

    #счет
    if inter(a_x, a_y, z_x, z_y, 20, 40):
        count += 3
        strike = False
        a_y = 1000

    if inter(a_x, a_y, z1_x, z1_y, 20, 40):
        count += 2
        strike = False
        a_y = 1000

    if inter(a_x, a_y, z2_x, z2_y, 20, 40):
        count += 1
        strike = False
        a_y = 1000

    #движение  высокой цели
    if right:
        z_x += 3
        if z_x > 548:
            z_x -= 1
            right = False
    else:
        z_x -= 3
        if z_x <= 0:
            z_x += 1
            right = True

    #движение средней цели
    if left:
        z1_x -= 2
        if z1_x < 0:
            z1_x += 1
            left = False
    else:
        z1_x += 2
        if z1_x > 548:
            z1_x -= 1
            left = True
    #движение низкой цели
    if right1:
        z2_x += 1
        if z2_x > 548:
            z2_x -= 1
            right1 = False
    else:
        z2_x -= 1
        if z2_x < 0:
            z2_x += 1
            right1 = True
#######
    clock.tick(FPS)
    string = myfont.render('Очков: ' + str(count), 0, (156, 51, 51))


    screen.blit(img_bg, (0, 0))
    player.blit(img_p, (0, 0))
    arrow.blit(img_a, (0, 0))
    zet.blit(img_z, (0, 0))
    screen.blit(string, (3, 60))
    screen.blit(arrow, (a_x, a_y))
    screen.blit(zet, (z_x, z_y))
    screen.blit(img_z, (z1_x, z1_y))
    screen.blit(img_z, (z2_x, z2_y))
    screen.blit(player, (x_p, y_p))
    window.blit(screen, (0, 0))
    pygame.display.update()

pygame.quit()


Just in case, I will also insert the second code file ... Suddenly, who needs to test the game later)

done = False
right = True
right1 = True
strike = False
left = True

FPS = 500
speed = 10
count = 0

x_p = 0
y_p = 440

z_x = 0
z_y = 0

a_x = 1000
a_y = 1000

z1_x = 348
z1_y = 100

z2_x = 0
z2_y = 200

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question