5

I'm very new to python. I am working on a simple game. So far, what I am trying to do is to add a few second delay between it showing the question rectangle and then showing the options. How would I do this? I tried using time.sleep or pygame.time.wait, but all of those showed a black screen, and then showed both the question and the options at the same time. By the way I am using pygame :). Here is my code:

try:
    logname = 'c:/temp/pgzrun.log'
    fontname = 'arial.ttf'   
    import faulthandler
    import math
    faulthandler.enable()
    import time
    import os, sys, importlib
    from time import sleep 
   
    script_dir = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    os.chdir(script_dir)
    
    import pgzrun
    import playsound
    import pygame 
    
    import random 
    from random import randint

    WIDTH = 1280
    HEIGHT = 720
    sys.setrecursionlimit(10000000)
    q1 = ["SIFS", "ba", "bo", "bi", "blo", 1]
    q2 = ["AFST", "la", "lo", "li", "lloo", 3]
    q3 = ["jaks", "fa", "fo", "fi", "asdlo", 2]
    q4 = ["afsa", "afsfga", "dfsdff", "dfdf", "safaawr", 2]
    questions = [q1, q2, q3, q4]
    question_box = Rect(500, 400, 140, 100)
  
    def draw():
        
        index = 0
        screen.fill("purple")
        screen.draw.filled_rect(question_box, "blue")
        screen.draw.textbox(str(questions[index][0]), question_box)
        screen.draw.filled_rect(answer_boxes[0], "blue")
        screen.draw.filled_rect(answer_boxes[0], "blue")
        screen.draw.filled_rect(answer_boxes[1], "blue")
        screen.draw.filled_rect(answer_boxes[2], "blue")
        screen.draw.filled_rect(answer_boxes[3], "blue")
     
    ab1 = Rect(0, 0, 140, 100)
    ab2 = Rect(0, 0, 140, 100)
    ab3 = Rect(0, 0, 140, 100)
    ab4 = Rect(0, 0, 140, 100)
    ab1.move_ip(40, 80)
    ab2.move_ip(300, 80)
    ab3.move_ip(600, 80)
    ab4.move_ip(900, 80)
    answer_boxes = [ab1, ab2, ab3, ab4]
    random.shuffle(questions)
    game_over = False
   
    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
  • No you are not using [PyGame](https://www.pygame.org/news), but you are using [Pygame Zero](https://pygame-zero.readthedocs.io/en/stable/) – Rabbid76 Dec 15 '20 at 05:40
  • Oh ok :) Thanks for the feedback! – Parul Deep Singh Dec 15 '20 at 05:43
  • You cannot set "Sleep" in `draw`, as the display is only updated once after `draw`. The scene is continuously being redrawn, which means that `draw` is invoked continuously. You must show the questions in different frames. – Rabbid76 Dec 15 '20 at 05:47
  • So what I would do is have it call a function that does time.sleep? – Parul Deep Singh Dec 15 '20 at 06:02
  • No. I told you to use "sleep" at all. You need a state variable or counter. Change the state or increment the counter in `draw`. Ask questions dependent on the state variable or counter. – Rabbid76 Dec 15 '20 at 06:37
  • You've asked the same question some hours ago. It is not intended that way. You have to improve the original question. What would happen if everyone asked the same question multiple times? Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Rabbid76 Dec 15 '20 at 06:42
  • Um sorry I forgot to delete that question :) – Parul Deep Singh Dec 15 '20 at 09:07
  • Ok so I used `clock.schedule ` to schedule a function that makes a Boolean variable to True. And then I added an if statement to the draw function to only draw answer boxes if that Boolean variable to True. It didn't work. Is it possible for you to give me a way to do this or elaborate on what you said? – Parul Deep Singh Dec 15 '20 at 09:10
  • Oh ok, I will be sure to do that next time :) – Parul Deep Singh Dec 15 '20 at 09:12
  • Alright, thanks for trying. @Arty Is it possible for you to help me? – Parul Deep Singh Dec 15 '20 at 09:15

1 Answers1

1

When you program an interactive application, you have an event loop. In that case you shouldn't block the program with sleep or similar commands.

Instead, you should use timers to trigger events. In Pygame Zero, you would use clock.schedule to trigger a function call after a specified period of time.

Here's how I would implement such an application:

import pgzrun

questions = ["One", "Two"]

index = 0
can_answer = False

def show_answers():
    global can_answer
    can_answer = True

def on_mouse_down(pos):
    global can_answer, index
    if can_answer:
        can_answer = False
        index = index + 1
        clock.schedule(show_answers, 1.0)
    
def draw():
    screen.fill("black")
    screen.draw.textbox(questions[index], Rect(0, 0, 200, 100))
    if can_answer:
        screen.draw.filled_rect(Rect(0, 100, 200, 50), "blue")

clock.schedule(show_answers, 1.0)
pgzrun.go()
r0the
  • 617
  • 4
  • 13