pygame-web / pygbag

python and pygame wasm for everyone ( packager + test server + simulator )
https://github.com/pygame-web
MIT License
338 stars 39 forks source link

trouble with loading small duration sound and small duration images #158

Closed Subisuresh321 closed 8 months ago

Subisuresh321 commented 8 months ago

in my code..there is image that shows correct when a match is found along with a sound which usually stays for 1 sec...but in browser it is going fast like a blink and i cant hear the sound's whole portion...only some beeps

import pygame
import asyncio
from random import shuffle
pygame.mixer.init()
pygame.init()

screen = pygame.display.set_mode((800, 600))
#BG
background=pygame.image.load('bgimgs/bg.jpg')
background=pygame.transform.scale(background,(800,600))
# title and icon
pygame.display.set_caption("Matching Letters")
icon = pygame.image.load('bgimgs/playtime.png')
pygame.display.set_icon(icon)

# Load all lowercase and uppercase letters
all_lowercase_letters = [pygame.transform.scale(pygame.image.load(f'letters/letter-{chr(i+97)}.png'),(64,64)) for i in range(26)]
all_uppercase_letters = [pygame.transform.scale(pygame.image.load(f'letters/letter-{chr(i+97)} (1).png'),(64,64)) for i in range(26)]

# Initialize the current letters
lowercase_letters = all_lowercase_letters[:5]
uppercase_letters = all_uppercase_letters[:5]

#correct
correct_image=pygame.image.load('bgimgs/verified.png')
correct_image=pygame.transform.scale(correct_image,(100,100))
#wrong
wrong_image=pygame.image.load('bgimgs/wrong.png')
wrong_image=pygame.transform.scale(wrong_image,(100,100))
#won
won_image=pygame.image.load('bgimgs/won.png')

# Store the positions of the letters
x_positions = [i * 180 for i in range(5)]  # 160 is the distance between the letters
shuffle(x_positions)  # Shuffle the positions to randomize the order of the letters
lowercase_positions = [(x, 500) for x in x_positions]  # 500 is the y-coordinate
shuffle(x_positions)
uppercase_positions = [(x, 100) for x in x_positions]  # 100 is the y-coordinate

# Store which letter & its position is being moved
moving_letter = None
drag_start_position=None

#bgsound
pygame.mixer.music.load('sounds/bgsound.ogg')
pygame.mixer.music.play(-1)
correct_sound=pygame.mixer.Sound('sounds/correct.ogg')
wrong_sound=pygame.mixer.Sound('sounds/wrong.ogg')
won_sound=pygame.mixer.Sound('sounds/gameover.ogg')
async def main_loop():
    # Game loop
    running = True
    while running:

        # rgb
        screen.fill((0, 0, 0))
        #bg
        screen.blit(background,(0,0))
        global lowercase_letters,uppercase_letters,all_uppercase_letters,all_lowercase_letters,lowercase_positions,uppercase_positions,x_positions,moving_letter,drag_start_position

        # Display letters
        for i in range(min(len(lowercase_letters),len(uppercase_letters))):
            if lowercase_letters[i] is not None:
                screen.blit(lowercase_letters[i], lowercase_positions[i])
            if uppercase_letters[i] is not None:
                screen.blit(uppercase_letters[i], uppercase_positions[i])
        pygame.display.flip()

        # gameover
        if all(i is None for i in all_lowercase_letters):
            screen.blit(won_image, (272, 172))
            pygame.display.flip()
            pygame.mixer.music.stop()
            won_sound.play()
            pygame.time.wait(4000)
            running = False

        #events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # Check if a lowercase letter was clicked
                for i in range(min(len(lowercase_letters),len(uppercase_letters))):
                    if lowercase_letters[i] is not None:
                        if lowercase_letters[i].get_rect(topleft=lowercase_positions[i]).collidepoint(event.pos):
                            moving_letter = i
                            drag_start_position=lowercase_positions[i]
            elif event.type == pygame.MOUSEBUTTONUP:
                if moving_letter is not None:
                    match_made=False
                    for i in range(5):
                        if lowercase_letters[i] is not None and uppercase_letters[i] is not None:
                            if lowercase_letters[i].get_rect(topleft=lowercase_positions[i]).colliderect(uppercase_letters[i].get_rect(topleft=uppercase_positions[i])):
                                    match_made=True
                                    break
                    if not match_made:
                        lowercase_positions[moving_letter] = drag_start_position
                        moving_letter=None
                        drag_start_position=None

            elif event.type == pygame.MOUSEMOTION:
                # Move the letter with the mouse
                if moving_letter is not None:
                    lowercase_positions[moving_letter] = event.pos

        # Check for matches
        for i in range(min(len(lowercase_letters),len(uppercase_letters))):
            if lowercase_letters[i] is not None:
                    for j in range(min(len(lowercase_letters),len(uppercase_letters))):
                        if i != j and uppercase_letters[j] is not None and lowercase_letters[i].get_rect(topleft=lowercase_positions[i]).colliderect(uppercase_letters[j].get_rect(topleft=uppercase_positions[j])):
                            # The letters do not match

                            wrong_sound.play()
                            screen.blit(wrong_image, (350, 250))  # Adjust the position as needed
                            pygame.display.flip()  # Update the display to show the wrong image
                            pygame.time.wait(3000)  # Wait for 1 seconds
                    if uppercase_letters is not None and lowercase_letters[i].get_rect(topleft=lowercase_positions[i]).colliderect(uppercase_letters[i].get_rect(topleft=uppercase_positions[i])):
                        # Display the correct image & sound

                        correct_sound.play()
                        screen.blit(correct_image, (350, 250))  # Adjust the position as needed
                        pygame.display.flip()  # Update the display to show the correct image
                        pygame.time.wait(3000)  # Wait for 1 seconds

                        # Remove the letters
                        lowercase_letters[i] = uppercase_letters[i] = None
                        pygame.display.flip()

        if all(t is None for t in lowercase_letters):
            # Remove the matched letters from the all_letters list
            all_lowercase_letters = all_lowercase_letters[5:]
            all_uppercase_letters = all_uppercase_letters[5:]

            # Get the next set of letters
            lowercase_letters = all_lowercase_letters[:5]
            uppercase_letters = all_uppercase_letters[:5]
            # Reset the positions
            x_positions = [i * 180 for i in range(5)]
            shuffle(x_positions)
            lowercase_positions = [(x, 500) for x in x_positions]
            shuffle(x_positions)
            uppercase_positions = [(x, 100) for x in x_positions]
            moving_letter=None
            drag_start_position=None

        await asyncio.sleep(0)
asyncio.run(main_loop())
Subisuresh321 commented 8 months ago

specific part of code where the trouble is:

# Check for matches
    for i in range(min(len(lowercase_letters),len(uppercase_letters))):
        if lowercase_letters[i] is not None:
                for j in range(min(len(lowercase_letters),len(uppercase_letters))):
                    if i != j and uppercase_letters[j] is not None and lowercase_letters[i].get_rect(topleft=lowercase_positions[i]).colliderect(uppercase_letters[j].get_rect(topleft=uppercase_positions[j])):
                        # The letters do not match

                        wrong_sound.play()
                        screen.blit(wrong_image, (350, 250))  # Adjust the position as needed
                        pygame.display.flip()  # Update the display to show the wrong image
                        pygame.time.wait(3000)  # Wait for 1 seconds
                if uppercase_letters is not None and lowercase_letters[i].get_rect(topleft=lowercase_positions[i]).colliderect(uppercase_letters[i].get_rect(topleft=uppercase_positions[i])):
                    # Display the correct image & sound

                    correct_sound.play()
                    screen.blit(correct_image, (350, 250))  # Adjust the position as needed
                    pygame.display.flip()  # Update the display to show the correct image
                    pygame.time.wait(3000)  # Wait for 1 seconds

                    # Remove the letters
                    lowercase_letters[i] = uppercase_letters[i] = None
                    pygame.display.flip()
pmp-p commented 8 months ago

try replacing pygame.time.wait(milliseconds) by await asyncio.sleep(float(milliseconds)/1000)

the browser does not allow to pause wasm programs, so you must idle looping instead

Subisuresh321 commented 8 months ago

thanks bro you are a life saver