1

what function do I use for it to print something if it detects a mouse hovering over an object or element? Is it box.collidepoint(pos)? Also, I am having some problems with importing playsound using from playsound import playsound. It just opens a black screen with a completely different height and width than I set it to be. How do I fix this? The error is ModuleNotFoundError: No module named 'playsound' here is my code:

try:
    logname = 'c:/temp/pgzrun.log'
    fontname = 'arial.ttf'

    import faulthandler
    fh_file = open('c:/temp/fault_handler.log', 'a', \
    encoding = 'utf-8', buffering = 1)
    faulthandler.enable(fh_file)
    
    import os, sys
    
    script_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    os.chdir(script_dir)

    
    with open('c:/temp/debug.log','a', encoding = 'utf-8') as f: f.write \
    (f'script_dir "{script_dir}", work_dir "{os.getcwd()}"\n')
    

    import pgzrun
    from playsound import playsound 
    import random 
    from random import randint
    
    

    WIDTH = 1280
    HEIGHT = 720
    camultiplier = 1500
    boxcolor = "blue"

    def draw():
        screen.fill("green yellow")
        screen.draw.filled_rect(main_box, "sky blue")
        screen.draw.filled_rect(timer_box, "sky blue")
        screen.draw.text("Score: " + str(score), color="black", topleft=(10, 10), fontname=fontname)

        for box in answer_boxes:
            screen.draw.filled_rect(box, boxcolor)

        screen.draw.textbox(str(time_left), timer_box, color=('black'), fontname=fontname)
        screen.draw.textbox(question[0], main_box, color=('black'), fontname=fontname)

        index = 1
        for box in answer_boxes:
            screen.draw.textbox(question[index], box, color=('black'), fontname=fontname)
            index = index + 1

    def game_over():
        global question, time_left, scoredecreaserps
        scoredecreaserps = 0
        message = 'Game Over. You got %s questions correct' %str(numques)
        question = [message, '-', '-', '-', '-', 5]
        time_left = 0

    def correct_answer():
        global question, score, numques, time_left, camultiplier 

        numques = numques + 1
        score = score + camultiplier
        camultiplier = camultiplier - 80

        if questions:
            question = questions.pop()
            time_left = 10
        else:
            print('End of questions')
            game_over()

    def on_mouse_down(pos):
        global boxcolor 
        index = 1

        for box in answer_boxes:

            if box.collidepoint(pos):
                print('Clicked on answer' + str(index))

                if index == question[5]:
                    print('You got it correct!')
                    correct_answer()
                else:
                    playsound('bruh_sound_effect.mp3')
                    game_over()

            index = index + 1

    
        
                
    def update_time_left():
        global time_left

        if time_left:
            time_left = time_left - 1
        else:
           
            game_over()

    def score_lower():
        global score, scoredecreaserps 

        if score:
            score = score - scoredecreaserps
            scoredecreaserps = scoredecreaserps + 8

    main_box = Rect(50, 40, 820, 240)
    timer_box = Rect(990, 40, 240, 240)

    answer_box1 = Rect(50, 358, 495, 165)
    answer_box2 = Rect(735, 358, 495, 165)
    answer_box3 = Rect(50, 538, 495, 165)
    answer_box4 = Rect(735, 538, 495, 165)

    answer_boxes = [answer_box1, answer_box2, answer_box3, answer_box4]

    scoredecreaserps = 80
    numques = 0
    score = 0
    time_left = 10

    q1 = ["Who was the third president of the Philippines?",
          'Rodrigo Duterte', 'Ramon Magsaysay', 'Jose P. Laurel',
          'Fidel V. Ramos', 3]

    q2 = ['When was the Philippines granted independece from the US?'
          , '1977', '1946', '1935', '1907', 2]

    q3 = ['When was the Philippines colonized by Spain', '1898', '1523', '1654',
          '1521', 4]

    questions = [q1, q2, q3]

    random.shuffle(questions)

    question = questions.pop(0)
    clock.schedule_interval(update_time_left, 1.0)
    clock.schedule_interval(score_lower, 1.0)
   

    pgzrun.go()
        
except:
    import traceback
    with open(logname, 'a', encoding = 'utf-8') as f:
        f.write(''.join(traceback.format_exc()) + '\n')
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Please clarify your question. It looks like this is for the pygame package? – anon01 Nov 01 '20 at 05:51
  • yes :) I am using pygame. – Parul Deep Singh Nov 01 '20 at 05:52
  • Have you installed the playsound package? `pip install playsound` – sytech Nov 01 '20 at 06:57
  • Where do you want to print the text do you want it print it on the screen if so it needs a font to be rendered –  Nov 01 '20 at 07:02
  • [`collidepoint()`](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.collidepoint) is the right choice. – Rabbid76 Nov 01 '20 at 07:02
  • No I haven't installer the playsound package, I guess that will work. – Parul Deep Singh Nov 01 '20 at 07:06
  • so, I should put a collidepoint() in a function, then use clock.schedule_interval to call it every second? cause that's what I wanna do. – Parul Deep Singh Nov 01 '20 at 07:07
  • just call in your main loop like in this example: https://stackoverflow.com/questions/41064365/pygame-detecting-mouse-cursor-over-object but you want to keep a flag so you can do a mouse-enter mouse-leave logic. – RufusVS Nov 01 '20 at 07:24
  • so I do this, `if box.get_rect().collidepoint(pygame.mouse.get_pos()):` – Parul Deep Singh Nov 01 '20 at 07:42
  • @ParulDeepSingh What is `box`? – Rabbid76 Nov 01 '20 at 14:19
  • @ParulDeepSingh Have you solved already your question by suggested comments above? Or question still needs some help? – Arty Nov 04 '20 at 07:16
  • I still need some help @Arty. I cant seem to figure it out :( – Parul Deep Singh Nov 09 '20 at 08:46
  • @ParulDeepSingh So problem with `playsound` you solved? By installing `python -m pip install playsound`? What's next? What error do you have now? – Arty Nov 09 '20 at 09:13
  • @ParulDeepSingh In order to catch mouse inside box you need to define `on_mouse_move()` event handler, like [in this code snippet](https://tio.run/##K6gsycjPM/7/XwEIUlLTFPLz4nPzS4tTgWRZqkZBfrGmFZcCFGSmKSTlV@ilp5bEF6Uml2ho6iXn5@RkpqQW5GfmlaApBgFlBV@QWQqZecVAVSDNiv//AwA). – Arty Nov 09 '20 at 09:21
  • Ok so I updated my code. And it is still not working, but I don't think it is a problem with me hovering over the object. I think it works, but the code for the action I want to be done if I am hovering over it is not written correctly. There are no errors though. Ill delete this and add another question that makes this more clear when you are online. @Arty – Parul Deep Singh Nov 16 '20 at 12:48
  • @ParulDeepSingh I think you can just extend your this question probably by new details. – Arty Nov 16 '20 at 14:58

0 Answers0