pygame-web / pygbag

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

random() not working as it should #161

Closed Subisuresh321 closed 8 months ago

Subisuresh321 commented 8 months ago

in my game random question and answer is selected to ask 3 times...in pycharm i am getting random question out of 30 questions..but after using pygbag..i am getting the 3 same questions in same order everytime i run (not same question three time)...

the code: import pygame, asyncio import random

Initialize Pygame

pygame.init()

Set up some constants

WIDTH, HEIGHT = 800, 600 FPS = 60

Create the screen

screen = pygame.display.set_mode((WIDTH, HEIGHT))

Load the images

bg = pygame.image.load('images/restoration_photo_bot.png') bg=pygame.transform.smoothscale(bg,(800,600)) player_img = pygame.image.load('images/dora.png') player_img=pygame.transform.smoothscale(player_img,(150,150)) question_card = pygame.image.load('images/card.jpg') question_card=pygame.transform.smoothscale(question_card,(800,600)) yes_img = pygame.image.load('images/yes.png') yes_img=pygame.transform.smoothscale(yes_img,(90,90)) no_img = pygame.image.load('images/no.png') no_img=pygame.transform.smoothscale(no_img,(90,90)) start_image=pygame.image.load('images/start.png') start_image=pygame.transform.smoothscale(start_image,(50,50)) textbox_image=pygame.image.load('images/textbox.png') textbox_image=pygame.transform.smoothscale(textbox_image,(250,250))

correct

correct_image=pygame.image.load('images/verified.png') correct_image=pygame.transform.smoothscale(correct_image,(80,80)) correct_image.convert()

wrong

wrong_image=pygame.image.load('images/wrong.png') wrong_image=pygame.transform.smoothscale(wrong_image,(80,80)) wrong_image.convert()

won

won_image=pygame.image.load('images/won.jpg') won_image=pygame.transform.smoothscale(won_image,(800,600))

Set up the player

player_rect = player_img.get_rect() player_rect.topleft = (0,450)

Set up the target positions

target_positions = [pygame.Vector2(180, 360), pygame.Vector2(320, 230), pygame.Vector2(530, 120)] current_target_index = 0 speed = 1

font

font = pygame.font.Font(None, 36)

sound

pygame.mixer.music.load('sounds/bgsound.ogg') pygame.mixer.music.play(-1) won_sound=pygame.mixer.Sound('sounds/jaichu.ogg')

questions and answers

questions_answers = { "Is the sky blue?": "yes", "Can a dog fly?": "no", "Is a banana yellow?": "yes", "Do we eat with our feet?": "no", "Is water wet?": "yes", "Can cars talk?": "no", "Do you wear shoes on your hands?": "no", "Is ice cream cold?": "yes", "Can fish live outside water?": "no", "Is a pineapple blue?": "no", "Do birds swim?": "no", "Is an ant small?": "yes", "Can elephants jump?": "no", "Do we drink food?": "no", "Is a car faster than a bicycle?": "yes", "Can a cat bark?": "no", "Is chocolate sweet?": "yes", "Can a mouse roar?": "no", "Is a tomato a fruit?": "no", "Can a rabbit climb trees?": "no", "Is a strawberry red?": "yes", "Is a circle round?": "yes", "Can a bird swim?": "no", "Is a square round?": "no", "Can a fish climb a tree?": "no", "Is a triangle round?": "no", "Can a car swim?": "no", "Is a flower shape like a square?": "no", "Can a bicycle fly?": "no", "Is a star shape like a circle?": "no", "Can a teddy bear talk?": "no" } current_question = None current_answer = None questions_answers_list = list(questions_answers.items()) random.shuffle(questions_answers_list)

Set up the game loop

async def main_loop(): global current_target_index,current_answer,current_question,questions_answers_list clickable=False game_started=False running = True while running:

Draw everything

        screen.blit(bg, (0, 0))
        if game_started:
            screen.blit(player_img, player_rect)
            if current_target_index<3:
                target_pos = target_positions[current_target_index]
                if pygame.Vector2(player_rect.topleft).distance_to(target_pos) > speed:
                    direction = (target_pos - pygame.Vector2(player_rect.topleft)).normalize()
                    player_rect.topleft += (direction * speed)
                else:
                    player_rect.topleft = target_pos
                    if current_question is None:
                        current_question, current_answer = questions_answers_list.pop()
                    screen.blit(question_card,(0,0))
                    question_card_rect=question_card.get_rect()
                    clickable=True
                    text = font.render(current_question, True,(0,0,0))
                    screen.blit(text, (300,100))
                    screen.blit(yes_img,(280,200))
                    screen.blit(no_img, (420, 200))
            else:
                pygame.mixer.music.pause()
                screen.blit(won_image,(0,0))
                pygame.display.flip()
                won_sound.play()
                await asyncio.sleep(float(10000) / 1000)
                running=False
        else:
            screen.blit(start_image, (380, 550))

        pygame.display.flip()
        # Event handling
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                #start click
                if start_image.get_rect(topleft=(380,550)).collidepoint(pygame.mouse.get_pos()):
                    #start_game
                    game_started=True
                if clickable:
                    # Check if the 'yes' or 'no' image was clicked
                    if yes_img.get_rect(topleft=(280,200)).collidepoint(event.pos):
                        # Handle 'yes' answer
                        screen.blit(textbox_image, (170, 300))
                        if current_answer == "yes":
                            screen.blit(correct_image,(230,360))
                            current_target_index += 1  # Move to the next target
                            questions_answers.pop(current_question)
                            current_question = None  # Reset the question
                            clickable=False
                        else:
                            screen.blit(wrong_image, (230,360))
                        pygame.display.update(question_card_rect)
                        await asyncio.sleep(float(1000) / 1000)
                    elif no_img.get_rect(topleft=(420, 200)).collidepoint(event.pos):
                        screen.blit(textbox_image, (170, 300))
                        textbox_image_rect = textbox_image.get_rect()
                        # Handle 'no' answer
                        if current_answer == "no":
                            screen.blit(correct_image, (230,360))
                            current_target_index += 1 # Move to the next target
                            questions_answers.pop(current_question)
                            current_question = None  # Reset the question
                            clickable = False
                        else:
                            screen.blit(wrong_image, (230,360))
                        pygame.display.update(question_card_rect)
                        await asyncio.sleep(float(1000) / 1000)
        # Cap the frame rate
        pygame.time.wait(1000 // FPS)
        await asyncio.sleep(0)

asyncio.run(main_loop())

pmp-p commented 8 months ago

try adding random.seed(time.time()) before the loop to generate some randomness, by default pygbag starts games in reproducible mode.

ref : https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED

probably would be good to add this to wiki https://pygame-web.github.io/wiki/pygbag-code/

Subisuresh321 commented 8 months ago

thanks