Esempio n. 1
0
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2, 8)

    doc_surf = pygame.Surface((DISPLAY.get_width(), DISPLAY.get_height()))

    demo_surf = pygame.Surface((DISPLAY.get_width()/2 - 13, DISPLAY.get_height()), pygame.SRCALPHA)

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 2, "l", 13, 15, (230,230,230))
    back_button = button.button(doc_screen.surface, [255,255,255,150], [255,255,255,200], grid.get_column(0.06), grid.get_row(0.2), 85,35, "BACK", anim=True) 

    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40), (255,255,255))
    title.message("Button", (grid.get_column(0.5), grid.get_row(1)), center=True)

    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35), (230,230,230))
    heading.message("Methods", (grid.get_column(0.06), grid.get_row(4.5)))
    
    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30), (200,200,200))
    heading_2.message("pgscript.button.button()", (grid.get_column(0.06), grid.get_row(4.8)))
    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20), (255,255,255))

    paragraph.wrapped_text("", (grid.get_column(0.06), grid.get_row(2)), 580, 2)

    #paragraph.wrapped_text("", (grid.get_column(0.06),grid.get_row(5.5)))


     
    doc_screen.add_objects([title, heading, heading_2, paragraph, back_button])

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg") 

    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_surf.fill([200,200,200,200])
        doc_screen.surface.fill([255,255,255,0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_surf, (grid.get_column(1) + 13, 0))

        for event in pygame.event.get():
            doc_screen.update(event)
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 5, 4)

    bg_surf_l = pygame.Surface((grid.get_column(1), DISPLAY.get_height()))
    bg_surf_r = pygame.Surface((grid.get_column(1), DISPLAY.get_height()))

    background_l = bg.scrolling_bg(bg_surf_l, ["tringle.png"], (82, 9, 124),
                                   30)
    background_r = bg.scrolling_bg(bg_surf_r, ["tringle.png"], (82, 9, 124),
                                   30)

    background_r.anim_start()
    background_l.anim_start()

    heading = text.text(DISPLAY, pygame.font.SysFont('arial', 40),
                        (255, 255, 255))
    heading.message("PGSCRIPT TUTOR", (grid.get_column(2.5), grid.get_row(1)),
                    center=True)

    cont_button = button.button(DISPLAY, [200, 200, 200, 100],
                                [200, 200, 200, 200],
                                grid.get_column(2.5) - 80,
                                (grid.get_row(3)) - 37,
                                160,
                                75,
                                "Continue",
                                anim=True)

    while not cont_button.get_state():
        pygame.display.flip()
        DISPLAY.fill((82, 9, 124))
        heading.draw()
        cont_button.draw()

        background_l.draw()
        background_r.draw()

        DISPLAY.blit(bg_surf_l, (0, 0))
        DISPLAY.blit(bg_surf_r,
                     (DISPLAY.get_width() - bg_surf_r.get_width(), 0))

        for event in pygame.event.get():
            cont_button.update(event)
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2,8)

    doc_surf = pygame.Surface((DISPLAY.get_width(), DISPLAY.get_height()))# surface where text from documentation is placed

    demo_screen = pygame.Surface((DISPLAY.get_width()/2 - 13, DISPLAY.get_height()), pygame.SRCALPHA) #surface where users can experiment with the demonstration

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 2, "l", 13, 15, (230,230,230))
    back_button = button.button(doc_screen.surface, [255,255,255,150], [255,255,255,200], grid.get_column(0.06), grid.get_row(0.2), 85,35, "BACK", anim=True) 
    
    #DEMO OBJECTS

    demo = bg.scrolling_bg(demo_screen, ["black_triangle.png"], (200,200,200,200),10)
    demo.anim_start()
    demo_text = 'demo = bg.scrolling_bg(demo_screen,["black_triangle.png"], (200,200,200,200), 10)'

    demo_input = text_input.text_input(demo_screen, grid.get_column(0), grid.get_row(6.5), 610, 30, demo_text, (0,0,0),(25,25,25),(100,100,100), 15)

    run_demo = button.button(demo_screen, [0,0,0,240], [0,0,0,190],grid.get_column(0.02), demo_input.rect.bottom + 10, 85,30, "Run", (200,200,200), anim=True)
    reset_demo = button.button(demo_screen, [0,0,0,240], [0,0,0,190],grid.get_column(0.02) + 90, demo_input.rect.bottom + 10, 85,30, "Reset", (200,200,200), anim=True)

    #ERROR
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20), (230,0,0))


    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40), (255,255,255))
 
    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35), (230,230,230))
 
    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30), (200,200,200))
 
    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20), (255,255,255))

    #ARGUMENTS
    arg_heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20), (255,255,255))
    arg_heading.font.set_underline(True)
    
    
#--------------------------------------------------------------------------------

    title.message("Scrolling Background", (grid.get_column(0.5), grid.get_row(1)), center=True) 

    paragraph.wrapped_text("Creates a background with multiple moving images. This class has an update method however it must not be used. Instead, anim_start should be called (just once, not in a loop). This starts updating the background on a separate thread to avoid slowing down the main loop.", (grid.get_column(0.06), title.text_end[1] + 125), 580, 2)

    heading.message("Methods", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    heading_2.message("pgscript.bg.scrolling_bg()", (grid.get_column(0.06), heading.text_end[1] + 100))

    paragraph.wrapped_text("Constructor method of the scrolling background class. Background color and the image paths are passed as arguments here.", (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("DISPLAY (pygame.Surface):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("The surface that the object needs to be drawn to. Usually this surface is the display surface of the window.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    arg_heading.message("bg_color (list [r,g,b]):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("The colour that will be filled to the background.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    arg_heading.message("image_paths (list):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("A list of images to be used as scrolling objects in the background.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    arg_heading.message("objects (int):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("The number of scrolling objects in the background.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    heading_2.message("pgscript.scrolling_bg.draw()", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text("Draws the background onto the DISPLAY surface. This method is meant to be called in the same loop where DISPLAY is updated.", (grid.get_column(0.06),heading_2.text_end[1] +75), 580, 2)

    heading_2.message("pgscript.scrolling_bg.anim_start()", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text("Starts updating the scrolling objects of the background on a separate thread. Therefore, this method must only be called once.", (grid.get_column(0.06),heading_2.text_end[1] +75), 580, 2)

#--------------------------------------------------------------------------------

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg") 
    doc_screen.add_objects([title, heading, heading_2,arg_heading, paragraph, back_button])

    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200,200,200,200])
        demo.draw()
        demo_input.draw()
        run_demo.draw()
        reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255,255,255,0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[37:-1]
            print(demo_args)
            try:
                demo = bg.scrolling_bg(demo_screen,["black_triangle.png"], *parse_args(demo_args)) #generate the object with args in the text field
                demo.anim_start()
            except:
                print(*parse_args)
                error_text.message("Invalid syntax", (grid.get_column(0.5), grid.get_row(7)), 3, center=True)
            run_demo.reset_state()
        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
            run_demo.update(event, (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
            demo_input.update(event, (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
            #demo.update(event , (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
            reset_demo.update(event, (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
Esempio n. 4
0
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2, 8)

    doc_surf = pygame.Surface(
        (DISPLAY.get_width(), DISPLAY.get_height()
         ))  # surface where text from documentation is placed

    demo_screen = pygame.Surface(
        (DISPLAY.get_width() / 2 - 13, DISPLAY.get_height()), pygame.SRCALPHA
    )  #surface where users can experiment with the demonstration

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 3.2, "l", 13, 15,
                                                   (230, 230, 230))
    back_button = button.button(doc_screen.surface, [255, 255, 255, 150],
                                [255, 255, 255, 200],
                                grid.get_column(0.06),
                                grid.get_row(0.2),
                                85,
                                35,
                                "BACK",
                                anim=True)

    #DEMO OBJECTS
    demo = text_input.text_input(demo_screen, 245, 285, 150, 50, "example")
    demo_text = 'demo = text_input.text_input(demo_screen, 245,285,150,50, "example")'

    demo_input = text_input.text_input(demo_screen, grid.get_column(0),
                                       grid.get_row(6.5), 610, 30, demo_text,
                                       (0, 0, 0), (25, 25, 25),
                                       (100, 100, 100), 15)
    run_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                             grid.get_column(0.02),
                             demo_input.rect.bottom + 10,
                             85,
                             30,
                             "Run", (200, 200, 200),
                             anim=True)
    reset_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                               grid.get_column(0.02) + 90,
                               demo_input.rect.bottom + 10,
                               85,
                               30,
                               "Reset", (200, 200, 200),
                               anim=True)

    #ERROR
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20),
                           (230, 0, 0))

    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40),
                      (255, 255, 255))

    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35),
                        (230, 230, 230))

    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30),
                          (200, 200, 200))

    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20),
                          (255, 255, 255))

    #ARGUMENTS
    arg_heading = text.text(doc_screen.surface,
                            pygame.font.SysFont('arial', 20), (255, 255, 255))
    arg_heading.font.set_underline(True)

    #--------------------------------------------------------------------------------

    title.message("Text Input", (grid.get_column(0.5), grid.get_row(1)),
                  center=True)

    paragraph.wrapped_text(
        "This object allows the input of text into the graphic program, which can be retrieved within the program as a string.",
        (grid.get_column(0.06), title.text_end[1] + 125), 580, 2)

    heading.message("Methods",
                    (grid.get_column(0.06), paragraph.text_end[1] + 100))

    heading_2.message("pgscript.text_input.text_input()",
                      (grid.get_column(0.06), heading.text_end[1] + 100))

    paragraph.wrapped_text(
        "The constructor method of the text field. It's appearance and position is passed as arguments to this constructor.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("DISPLAY (pygame.Surface):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "The surface that the object needs to be drawn to. Usually this surface is the display surface of the window.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("x (int):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "position of the text field on the x-axis",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("y (int):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "position of the text field on the y-axis",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("w (int):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "width of the text field.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("h (int):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "height of the text field.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("text (str):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "used to specify text pre-written in the field",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("text_col (list [r,g,b]):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "Colour of the text",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("active_col (list [r,g,b]):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "color of text field outline when focused",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("passive_col (list [r,g,b]):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "color of text field outline when not focused",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    heading_2.message("pgscript.text_input.draw()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "Draws the text field object onto the DISPLAY surface. This method is meant to be called in the same loop where DISPLAY is updated",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.text_input.update(event)",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method uses pygame events to check for mouse clicks to determine whether or not the text field is focused. While the text field is focused, this method also listens for keypresses from the user, which is stored in the textfield.text attribute. It is meant to be run in a loop with each event given by pygame.event.get()",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.textinput.get_text()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method returns the text that is currently in the textfield object.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.textinput.clear_text()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "Deletes any text that is currently in the textfield object.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.textinput.set_text(text)",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "Used to set the text inside the text input object after it has been initialized. Useful in a program for 'resetting' a text field to some text.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    #--------------------------------------------------------------------------------

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg")
    doc_screen.add_objects(
        [title, heading, heading_2, arg_heading, paragraph, back_button])
    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200, 200, 200, 200])
        demo.draw()
        demo_input.draw()
        run_demo.draw()
        reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255, 255, 255, 0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[29:-1]
            print(demo_args)
            try:
                demo = text_input.text_input(
                    demo_screen, *parse_args(demo_args)
                )  #generate the object with args in the text field
            except:
                error_text.message("Invalid syntax",
                                   (grid.get_column(0.5), grid.get_row(7)),
                                   3,
                                   center=True)
            run_demo.reset_state()
        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
            run_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo_input.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            reset_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
def draw(DISPLAY):
    screen = scrolling_screen.scrolling_screen(DISPLAY, 2, "r", 20, 15,
                                               (230, 230, 230))
    grid = draw_grid.draw_grid(screen.surface, 5, 16)

    #surfaces to draw scrolling bgs on. this way, the background will only be drawn where these surfaces exist. (sides of the screen)
    bg_surf_l = pygame.Surface(
        (grid.get_column(1), screen.surface.get_height()))
    bg_surf_r = pygame.Surface(
        (grid.get_column(1), screen.surface.get_height()))

    background_l = bg.scrolling_bg(bg_surf_l, ["tringle.png"], (82, 9, 124),
                                   60)
    background_r = bg.scrolling_bg(bg_surf_r, ["tringle.png"], (82, 9, 124),
                                   60)

    background_r.anim_start()
    background_l.anim_start()

    heading = text.text(screen.surface, pygame.font.SysFont('arial', 40),
                        (255, 255, 255))
    heading.message("Select an Object",
                    (grid.get_column(2.5), grid.get_row(1)),
                    center=True)

    #BUTTONS

    button_btn = button.button(screen.surface, [200, 200, 200, 100],
                               [255, 130, 245, 200],
                               grid.get_column(1.5) - 75,
                               grid.get_row(3.5) - 75,
                               150,
                               150,
                               "Button",
                               anim=True,
                               font_size=24)
    text_input_btn = button.button(screen.surface, [200, 200, 200, 100],
                                   [255, 130, 245, 200],
                                   grid.get_column(2.5) - 75,
                                   grid.get_row(3.5) - 75,
                                   150,
                                   150,
                                   "Text Input",
                                   anim=True,
                                   font_size=24)
    slider_btn = button.button(screen.surface, [200, 200, 200, 100],
                               [255, 130, 245, 200],
                               grid.get_column(3.5) - 75,
                               grid.get_row(3.5) - 75,
                               150,
                               150,
                               "Slider",
                               anim=True,
                               font_size=24)

    draggable_btn = button.button(screen.surface, [200, 200, 200, 100],
                                  [255, 130, 245, 200],
                                  grid.get_column(1.5) - 75,
                                  grid.get_row(6) - 75,
                                  150,
                                  150,
                                  "Draggable Surface",
                                  anim=True,
                                  wrapping=9,
                                  font_size=24)
    text_btn = button.button(screen.surface, [200, 200, 200, 100],
                             [255, 130, 245, 200],
                             grid.get_column(2.5) - 75,
                             grid.get_row(6) - 75,
                             150,
                             150,
                             "Text",
                             anim=True,
                             font_size=24)
    s_bg_btn = button.button(screen.surface, [200, 200, 200, 100],
                             [255, 130, 245, 200],
                             grid.get_column(3.5) - 75,
                             grid.get_row(6) - 75,
                             150,
                             150,
                             "Scrolling Background",
                             anim=True,
                             wrapping=10,
                             font_size=24,
                             text_center=True)
    p_bg_btn = button.button(screen.surface, [200, 200, 200, 100],
                             [255, 130, 245, 200],
                             grid.get_column(1.5) - 75,
                             grid.get_row(8.5) - 75,
                             150,
                             150,
                             "Parallax Background",
                             anim=True,
                             wrapping=10,
                             font_size=24,
                             text_center=True)

    grid_btn = button.button(screen.surface, [200, 200, 200, 100],
                             [255, 130, 245, 200],
                             grid.get_column(3.5) - 75,
                             grid.get_row(8.5) - 75,
                             150,
                             150,
                             "Grid",
                             anim=True,
                             font_size=24)
    scroll_btn = button.button(screen.surface, [200, 200, 200, 100],
                               [255, 130, 245, 200],
                               grid.get_column(2.5) - 75,
                               grid.get_row(8.5) - 75,
                               150,
                               150,
                               "Scrolling Screen",
                               anim=True,
                               font_size=24,
                               wrapping=10)

    screen.add_objects([
        heading, button_btn, text_input_btn, slider_btn, draggable_btn,
        text_btn, s_bg_btn, p_bg_btn, grid_btn, scroll_btn
    ])

    while True:
        pygame.display.flip()
        screen.surface.fill((82, 9, 124))

        background_l.draw()
        background_r.draw()

        screen.surface.blit(bg_surf_l, (0, 0))
        screen.surface.blit(bg_surf_r,
                            (DISPLAY.get_width() - bg_surf_r.get_width(), 0))
        if button_btn.get_state():
            return "button"
        elif text_input_btn.get_state():
            return "text_input"
        elif slider_btn.get_state():
            return "slider"
        elif draggable_btn.get_state():
            return "drag"
        elif text_btn.get_state():
            return "text"
        elif s_bg_btn.get_state():
            return "s_bg"
        elif p_bg_btn.get_state():
            return "p_bg"
        elif grid_btn.get_state():
            return "grid"
        elif scroll_btn.get_state():
            return "scroll"

        screen.draw()

        for event in pygame.event.get():
            screen.update(event)
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2, 8)

    doc_surf = pygame.Surface(
        (DISPLAY.get_width(), DISPLAY.get_height()
         ))  # surface where text from documentation is placed

    demo_screen = pygame.Surface(
        (DISPLAY.get_width() / 2 - 13, DISPLAY.get_height()), pygame.SRCALPHA
    )  #surface where users can experiment with the demonstration

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 3.2, "l", 13, 15,
                                                   (230, 230, 230))
    back_button = button.button(doc_screen.surface, [255, 255, 255, 150],
                                [255, 255, 255, 200],
                                grid.get_column(0.06),
                                grid.get_row(0.2),
                                85,
                                35,
                                "BACK",
                                anim=True)

    #DEMO OBJECTS
    demo_text = 'demo = title.message("Example", (245,285), 0, True)'

    demo_input = text_input.text_input(demo_screen, grid.get_column(0),
                                       grid.get_row(6.5), 610, 30, demo_text,
                                       (0, 0, 0), (25, 25, 25),
                                       (100, 100, 100), 15)
    run_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                             grid.get_column(0.02),
                             demo_input.rect.bottom + 10,
                             85,
                             30,
                             "Run", (200, 200, 200),
                             anim=True)
    reset_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                               grid.get_column(0.02) + 90,
                               demo_input.rect.bottom + 10,
                               85,
                               30,
                               "Reset", (200, 200, 200),
                               anim=True)

    #ERROR
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20),
                           (230, 0, 0))

    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40),
                      (255, 255, 255))

    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35),
                        (230, 230, 230))

    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30),
                          (200, 200, 200))

    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20),
                          (255, 255, 255))

    #ARGUMENTS
    arg_heading = text.text(doc_screen.surface,
                            pygame.font.SysFont('arial', 20), (255, 255, 255))
    arg_heading.font.set_underline(True)

    #--------------------------------------------------------------------------------

    title.message("TEXT", (grid.get_column(0.5), grid.get_row(1)), center=True)

    paragraph.wrapped_text(
        "This class provides a simple method to display text surfaces for a specified duration or forever. The text class works slightly different to other objects that have previously been shown. This class does not have an update method, since it does not require user input. Furthermore, multiple text surfaces of the same font can be displayed using the same object. All the user needs to do is to make sure that the draw method is being continuously called in a loop, and call the message method with the text, position and duration when desired in the program. When the message method is called, it will submit a rendered text surface to the draw method, which will begin displaying the surface until the specified duration.",
        (grid.get_column(0.06), title.text_end[1] + 125), 580, 2)

    heading.message("Methods",
                    (grid.get_column(0.06), paragraph.text_end[1] + 100))

    heading_2.message("pgscript.text.text()",
                      (grid.get_column(0.06), heading.text_end[1] + 100))

    paragraph.wrapped_text(
        "The constructor method of the class. Text color and the font to be used are passed as arguments to this method.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("DISPLAY (pygame.Surface):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "The surface that the object needs to be drawn to. Usually this surface is the display surface of the window.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("font (pygame.font):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "The font that the class will use to render the text.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("color (list [r,g,b]):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "colour of the text.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    heading_2.message("pgscript.text.message()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method is used to display a single line of text forever or for a specific amount of time.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("text (string):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "the string that needs to be displayed",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("pos (tuple (x,y))",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "coordinates from where the text surface will be drawn.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("duration (float):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "How long the text needs to be drawn for, given in seconds. If duration is set to 0, the text is displayed forever.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("center (bool):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "If this is set to true, the text will be centered on the given pos instead of drawing from the top left corner.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    heading_2.message("pgscript.text.draw()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method draws all text surfaces of the text class onto the Display surface. It is meant to be called in the same loop where the display surface is updated.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.text.wrapped_text()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method is used to make displaying large amounts of texts such as paragraphs easier in pygame.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("text (string):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "the string that needs to be displayed",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("pos (tuple (x,y))",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "coordinates from where the text surface will be drawn.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("length_limit (int):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "the maximum width that the text can take up before wrapping to the next line.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("line_spacing (int):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))
    paragraph.wrapped_text(
        "How many pixels of vertical space between each line",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    #--------------------------------------------------------------------------------

    error_text.message("Demo unavailable.",
                       (grid.get_column(0.5), grid.get_row(7)),
                       0,
                       center=True)
    background = bg.parallax_bg(doc_surf, "obj_bg.jpg")
    doc_screen.add_objects(
        [title, heading, heading_2, arg_heading, paragraph, back_button])
    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200, 200, 200, 200])
        #demo.draw()
        demo_input.draw()
        #run_demo.draw()
        #reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255, 255, 255, 0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[29:-1]
            print(demo_args)
            try:
                demo = title.message(
                    *parse_args(demo_args)
                )  #generate the object with args in the text field
            except:
                error_text.message("Invalid syntax",
                                   (grid.get_column(0.5), grid.get_row(7)),
                                   3,
                                   center=True)
            run_demo.reset_state()
        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2,8)

    doc_surf = pygame.Surface((DISPLAY.get_width(), DISPLAY.get_height()))# surface where text from documentation is placed

    demo_screen = pygame.Surface((DISPLAY.get_width()/2 - 13, DISPLAY.get_height()), pygame.SRCALPHA) #surface where users can experiment with the demonstration

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 2.6, "l", 13, 15, (230,230,230))
    back_button = button.button(doc_screen.surface, [255,255,255,150], [255,255,255,200], grid.get_column(0.06), grid.get_row(0.2), 85,35, "BACK", anim=True) 
    
    #DEMO OBJECTS
    demo_surface = pygame.Surface((100,100))
    demo_surface.fill((0,0,0))

    demo = draggable_surface.draggable_surface(demo_screen, demo_surface, (245,285))
    demo_text = 'draggable_surface.draggable_surface(demo_screen, demo_surface, (245,285))'

    demo_input = text_input.text_input(demo_screen, grid.get_column(0), grid.get_row(6.5), 610, 30, demo_text, (0,0,0),(25,25,25),(100,100,100), 15)
    run_demo = button.button(demo_screen, [255,255,255,150], [255,255,255,200],grid.get_column(0.02), demo_input.rect.bottom + 10, 85,30, "Run" )
    reset_demo = button.button(demo_screen, [255,255,255,150], [255,255,255,200],grid.get_column(0.02) + 90, demo_input.rect.bottom + 10, 85,30, "Reset")

    #ERROR
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20), (230,0,0))


    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40), (255,255,255))
 
    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35), (230,230,230))
 
    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30), (200,200,200))
 
    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20), (255,255,255))

    #ARGUMENTS
    arg_heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20), (255,255,255))
    arg_heading.font.set_underline(True)
    
    
#--------------------------------------------------------------------------------

    title.message("Draggable Surface", (grid.get_column(0.5), grid.get_row(1)), center=True) 

    paragraph.wrapped_text("This class creates a pygame surface that can be moved with the mouse by clicking and dragging.", (grid.get_column(0.06), title.text_end[1] + 125), 580, 2)

    heading.message("Methods", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    heading_2.message("pgscript.draggable_surface()", (grid.get_column(0.06), heading.text_end[1] + 100))

    paragraph.wrapped_text("Constructor method of the class. The surface that needs to be made movable and its starting position is passed to this method as arguments.", (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("DISPLAY (pygame.Surface):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("The surface that the object needs to be drawn to. Usually this surface is the display surface of the window.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    arg_heading.message("Suface (pygame.Surface):", (grid.get_column(0.06), paragraph.text_end[1] + 50))
    
    paragraph.wrapped_text("The surface that needs to be made movable.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    arg_heading.message("start_pos (tuple (x,y)):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("The position at which the surface is initially drawn.", (arg_heading.text_end[0]+7,arg_heading.text_end[1]), 580 - arg_heading.text_end[0]-7, 2)

    heading_2.message("pgscript.draggable_surface.draw()", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text("Draws the draggable surface object onto the DISPLAY surface. This method is meant to be called in the same loop where DISPLAY is updated", (grid.get_column(0.06),heading_2.text_end[1] +75), 580, 2)

    heading_2.message("pgscript.draggable_surface.update(event)", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text("Pygame events are used to check for mouse activity and clicks.  It is meant to be run in a loop with each event given by pygame.event.get()", (grid.get_column(0.06),heading_2.text_end[1] +75), 580, 2)

    heading_2.message("pgscript.draggable_surface.get_pos()", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text("Returns a tuple (x,y) of the current coordinates of the surface.", (grid.get_column(0.06),heading_2.text_end[1] +75), 580, 2)

    heading_2.message("pgscript.draggable_surface.set_pos(pos)", (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text("Sets the position of the draggable surface to pos", (grid.get_column(0.06),heading_2.text_end[1] +75), 580, 2)

    arg_heading.message("pos (tuple (x,y)):", (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text("New desired position of the object.", (arg_heading.text_end[0] + 7,arg_heading.text_end[1]), 580, 2)

#--------------------------------------------------------------------------------

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg") 
    doc_screen.add_objects([title, heading, heading_2,arg_heading, paragraph, back_button])
    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200,200,200,200])
        demo.draw()
        demo_input.draw()
        run_demo.draw()
        reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255,255,255,0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[48:-1]
            print(demo_args)
            try:
                demo = draggable_surface.draggable_surface(demo_screen, demo_surface, *parse_args(demo_args)) #generate the object with args in the text field
            except:
                print(*parse_args(demo_args))
                error_text.message("Invalid syntax", (grid.get_column(0.5), grid.get_row(7)), 3, center=True)
            run_demo.reset_state()
        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
            run_demo.update(event, (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
            demo_input.update(event, (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
            if pygame.mouse.get_pos()[0] > (DISPLAY.get_width()/2) +13:
                demo.update(event , (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
            else:
                demo.active = False
            reset_demo.update(event, (pygame.mouse.get_pos()[0] - (DISPLAY.get_width()/2) - 13, pygame.mouse.get_pos()[1]))
Esempio n. 8
0
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2, 8)

    doc_surf = pygame.Surface(
        (DISPLAY.get_width(), DISPLAY.get_height()
         ))  # surface where text from documentation is placed

    demo_screen = pygame.Surface(
        (DISPLAY.get_width() / 2 - 13, DISPLAY.get_height()), pygame.SRCALPHA
    )  #surface where users can experiment with the demonstration

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 3.2, "l", 13, 15,
                                                   (230, 230, 230))
    back_button = button.button(doc_screen.surface, [255, 255, 255, 150],
                                [255, 255, 255, 200],
                                grid.get_column(0.06),
                                grid.get_row(0.2),
                                85,
                                35,
                                "BACK",
                                anim=True)

    #DEMO OBJECTS
    demo_display = pygame.Surface((600, 337), pygame.SRCALPHA)

    demo = scrolling_screen.scrolling_screen(demo_display, 2.5, 'r', 10, 15,
                                             (255, 255, 255))
    demo_text = 'demo = scrolling_screen.scrolling_screen(demo_display, 2.5, "r", 10, 15, (255,255,255))'

    d_head = text.text(demo.surface, pygame.font.SysFont('arial', 35),
                       (230, 230, 230))
    d_head.message("Example", (300, int(337 / 2)), 0, True)

    demo.add_objects([d_head])

    demo_input = text_input.text_input(demo_screen, grid.get_column(0),
                                       grid.get_row(6.5), 610, 30, demo_text,
                                       (0, 0, 0), (25, 25, 25),
                                       (100, 100, 100), 15)
    run_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                             grid.get_column(0.02),
                             demo_input.rect.bottom + 10,
                             85,
                             30,
                             "Run", (200, 200, 200),
                             anim=True)
    reset_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                               grid.get_column(0.02) + 90,
                               demo_input.rect.bottom + 10,
                               85,
                               30,
                               "Reset", (200, 200, 200),
                               anim=True)

    #ERROR
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20),
                           (230, 0, 0))

    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40),
                      (255, 255, 255))

    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35),
                        (230, 230, 230))

    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30),
                          (200, 200, 200))

    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20),
                          (255, 255, 255))

    #ARGUMENTS
    arg_heading = text.text(doc_screen.surface,
                            pygame.font.SysFont('arial', 20), (255, 255, 255))
    arg_heading.font.set_underline(True)

    #--------------------------------------------------------------------------------
    title.message("Parallax Background",
                  (grid.get_column(0.5), grid.get_row(1)),
                  center=True)

    paragraph.wrapped_text(
        "This object shifts the background based on mouse movements to simulate a parallax effect. The parallax background does not need an update method since the mouse movements are used directly within the draw method.",
        (grid.get_column(0.06), title.text_end[1] + 125), 580, 2)

    heading.message("Methods",
                    (grid.get_column(0.06), paragraph.text_end[1] + 100))

    heading_2.message("pgscript.bg.parallax_bg()",
                      (grid.get_column(0.06), heading.text_end[1] + 100))

    paragraph.wrapped_text(
        "Constructor method of the parallax background class. The path to the background image is passed as an argument to this method.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("DISPLAY (pygame.Surface):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "The surface that the object needs to be drawn to. Usually this surface is the display surface of the window.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("image_path (string):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "The relative path to the background image.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("ratio (float):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "how much to scale the image by in relation to the display surface. Dictates how sensitive the background will be to mouse movements",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    heading_2.message("pgscript.parallax_bg.draw()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "Draws the background onto the DISPLAY surface. This method is meant to be called in the same loop where DISPLAY is updated.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    #--------------------------------------------------------------------------------

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg")
    doc_screen.add_objects(
        [title, heading, heading_2, arg_heading, paragraph, back_button])
    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200, 200, 200, 200])
        #demo_display.fill((0,0,0,0))
        demo_screen.blit(demo_display, (13, grid.get_row(1.5)))
        demo.surface.fill([0, 0, 0])
        demo.draw()
        demo_input.draw()
        run_demo.draw()
        reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255, 255, 255, 0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[42:-1]
            print(demo_args)
            try:
                demo = scrolling_screen.scrolling_screen(
                    demo_display, *parse_args(demo_args)
                )  #generate the object with args in the text field
                d_head = text.text(demo.surface,
                                   pygame.font.SysFont('arial', 35),
                                   (230, 230, 230))
                d_head.message("Example", (300, int(337 / 2)), 0, True)

                demo.add_objects([d_head])
            except:
                error_text.message("Invalid syntax",
                                   (grid.get_column(0.5), grid.get_row(7)),
                                   3,
                                   center=True)
            run_demo.reset_state()
        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
            run_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo_input.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo.update(event)
            reset_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
Esempio n. 9
0
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2, 8)

    doc_surf = pygame.Surface((DISPLAY.get_width(), DISPLAY.get_height()))

    demo_screen = pygame.Surface(
        (DISPLAY.get_width() / 2 - 13, DISPLAY.get_height()), pygame.SRCALPHA)

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 4, "l", 13, 15,
                                                   (230, 230, 230))
    back_button = button.button(doc_screen.surface, [255, 255, 255, 150],
                                [255, 255, 255, 200],
                                grid.get_column(0.06),
                                grid.get_row(0.2),
                                85,
                                35,
                                "BACK",
                                anim=True)

    demo_button = button.button(demo_screen, (0, 0, 0), (75, 75, 75), 245, 285,
                                150, 100, "example", (200, 200, 200))

    demo_text = 'demo_button = button.button(demo_screen, (0,0,0), (75,75,75), 245, 285, 150,100, "example", (200,200,200))'
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20),
                           (230, 0, 0))

    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40),
                      (255, 255, 255))
    title.message("Button", (grid.get_column(0.5), grid.get_row(1)),
                  center=True)

    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35),
                        (230, 230, 230))
    heading.message("Methods", (grid.get_column(0.06), grid.get_row(4.5)))

    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30),
                          (200, 200, 200))
    heading_2.message("pgscript.button.button()",
                      (grid.get_column(0.06), grid.get_row(5)))
    heading_2.message("pgscript.button.draw()",
                      (grid.get_column(0.06), grid.get_row(16.5)))
    heading_2.message("pgscript.button.update()",
                      (grid.get_column(0.06), grid.get_row(19)))

    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20),
                          (255, 255, 255))
    # 1 line: 0.6, 2 lines: 0.8, 3 lines: 0.9
    paragraph.wrapped_text(
        "This object creates a button (pygame surface) that can be drawn onto a display surface. The object also indicates when a mouse button is pressed within the button’s boundaries.The button is drawn onto a display surface using the draw() method. To allow the button to indicate presses, the update()  method needs to be called. If the button has been pressed, the button.pressed attribute is set to true.",
        (grid.get_column(0.06), grid.get_row(2)), 580, 2)

    paragraph.wrapped_text(
        "The constructor method of the button. The button’s appearance and position is passed as arguments to this constructor.",
        (grid.get_column(0.06), grid.get_row(6)), 580, 2)
    #args
    paragraph.wrapped_text(
        "Display_surface: (Pygame.surface) The surface that the button needs to be drawn to. Usually this surface is the display surface of the window.",
        (grid.get_column(0.06), grid.get_row(6.7)), 580, 2)
    paragraph.wrapped_text(
        "passive_color: (list [r,g,b]) The color of the button when the mouse cursor is not hovering over the button",
        (grid.get_column(0.06), grid.get_row(7.7)), 580, 2)
    paragraph.wrapped_text(
        "active_color: (list [r,g,b]) the colour of the button when the mouse cursor is hovering over the button",
        (grid.get_column(0.06), grid.get_row(8.5)), 580, 2)
    paragraph.wrapped_text(
        "x_postion: (int) The coordinate of the top left corner of the button on the x axis",
        (grid.get_column(0.06), grid.get_row(9.3)), 580, 2)
    paragraph.wrapped_text(
        "y_position: (int) The coordinate of the top left corner of the button on the y axis",
        (grid.get_column(0.06), grid.get_row(10.1)), 580, 2)
    paragraph.wrapped_text(
        "length: (int) The length (in pixels) of the button",
        (grid.get_column(0.06), grid.get_row(10.9)), 580, 2)
    paragraph.wrapped_text(
        "height: (int) the height (in pixels) of the button",
        (grid.get_column(0.06), grid.get_row(11.5)), 580, 2)
    paragraph.wrapped_text(
        "text_col: (list [r,g,b]) The color of the text within the button. Default value is black.",
        (grid.get_column(0.06), grid.get_row(12.1)), 580, 2)
    paragraph.wrapped_text(
        "anim: (bool) If set to true, the button fades into the other color instead of instantly switching to it.",
        (grid.get_column(0.06), grid.get_row(12.9)), 580, 2)
    paragraph.wrapped_text(
        "font_size: (int) size of the text in the button. Font size is set to 30 by default.",
        (grid.get_column(0.06), grid.get_row(13.7)), 580, 2)
    paragraph.wrapped_text(
        "Wrapping: (int)  how many characters to wrap the text inside the button by. If the value is 0, the text will not be wrapped. Default value is 0",
        (grid.get_column(0.06), grid.get_row(14.5)), 580, 2)
    paragraph.wrapped_text(
        "text_center: (bool) states whether the text inside the button should be vertically centered. Default value is true.",
        (grid.get_column(0.06), grid.get_row(15.5)), 580, 2)

    paragraph.wrapped_text(
        "This method draws the button surface onto the display_surface of the object, which is set using it’s constructor method. It is meant to be called in the same loop as where display_surface is updated.",
        (grid.get_column(0.06), grid.get_row(17.1)), 580, 2)
    paragraph.wrapped_text(
        "This method checks if the cursor is hovering over the button and is pressed. If the conditions are met, the button object’s pressed attribute is set to true. It is meant to be called in a loop with each pygame event given by pygame.event.get() as an argument.",
        (grid.get_column(0.06), grid.get_row(19.6)), 580, 2)

    heading_2.message("pgscript.button.get_state()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 70))

    paragraph.wrapped_text(
        "This method returns true if the button has been pressed.",
        (grid.get_column(0.06), heading_2.text_end[1] + 20), 580, 2)

    heading_2.message("pgscript.button.reset_state()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 70))

    paragraph.wrapped_text(
        "This method resets the button so that the get_state() method will return false until the button is pressed again. This needs to be used when the button is required to be pressable after it has already been pressed before.",
        (grid.get_column(0.06), heading_2.text_end[1] + 20), 580, 2)

    #DEMO
    #heading.message("DEMO", (grid.get_column(0.06), paragraph.text_end[1] + 70))

    demo_input = text_input.text_input(demo_screen, grid.get_column(0),
                                       grid.get_row(6.5), 610, 30, demo_text,
                                       (0, 0, 0), (25, 25, 25),
                                       (100, 100, 100), 15)
    run_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                             grid.get_column(0.02),
                             demo_input.rect.bottom + 10,
                             85,
                             30,
                             "Run", (200, 200, 200),
                             anim=True)
    reset_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                               grid.get_column(0.02) + 90,
                               demo_input.rect.bottom + 10,
                               85,
                               30,
                               "Reset", (200, 200, 200),
                               anim=True)

    doc_screen.add_objects([title, heading, heading_2, paragraph, back_button])

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg")

    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200, 200, 200, 200])
        demo_button.draw()
        demo_input.draw()
        run_demo.draw()
        reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255, 255, 255, 0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[28:-1]
            try:
                demo_button = button.button(demo_screen,
                                            *parse_args(demo_args))
                print(*parse_args(demo_args))
            except:
                error_text.message("Invalid syntax",
                                   (grid.get_column(0.5), grid.get_row(7)),
                                   3,
                                   center=True)
            run_demo.reset_state()

        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
            run_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo_input.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo_button.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            reset_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
def draw(DISPLAY):
    grid = draw_grid.draw_grid(DISPLAY, 2, 8)

    doc_surf = pygame.Surface(
        (DISPLAY.get_width(), DISPLAY.get_height()
         ))  # surface where text from documentation is placed

    demo_screen = pygame.Surface(
        (DISPLAY.get_width() / 2 - 13, DISPLAY.get_height()), pygame.SRCALPHA
    )  #surface where users can experiment with the demonstration

    doc_screen = scrolling_screen.scrolling_screen(doc_surf, 2.6, "l", 13, 15,
                                                   (230, 230, 230))
    back_button = button.button(doc_screen.surface, [255, 255, 255, 150],
                                [255, 255, 255, 200],
                                grid.get_column(0.06),
                                grid.get_row(0.2),
                                85,
                                35,
                                "BACK",
                                anim=True)

    #DEMO OBJECTS
    demo = slider.slider(demo_screen, (0, 0, 0), (255, 255, 255),
                         (245, 285, 150, 25))
    demo_text = 'slider.slider(demo_screen, (0,0,0),(255,255,255), (245,285,150,25))'

    demo_input = text_input.text_input(demo_screen, grid.get_column(0),
                                       grid.get_row(6.5), 610, 30, demo_text,
                                       (0, 0, 0), (25, 25, 25),
                                       (100, 100, 100), 15)
    run_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                             grid.get_column(0.02),
                             demo_input.rect.bottom + 10,
                             85,
                             30,
                             "Run", (200, 200, 200),
                             anim=True)
    reset_demo = button.button(demo_screen, [0, 0, 0, 240], [0, 0, 0, 190],
                               grid.get_column(0.02) + 90,
                               demo_input.rect.bottom + 10,
                               85,
                               30,
                               "Reset", (200, 200, 200),
                               anim=True)

    #ERROR
    error_text = text.text(demo_screen, pygame.font.SysFont('arial', 20),
                           (230, 0, 0))

    #TITLE
    title = text.text(doc_screen.surface, pygame.font.SysFont('arial', 40),
                      (255, 255, 255))

    #HEADING
    heading = text.text(doc_screen.surface, pygame.font.SysFont('arial', 35),
                        (230, 230, 230))

    #HEADING 2
    heading_2 = text.text(doc_screen.surface, pygame.font.SysFont('arial', 30),
                          (200, 200, 200))

    #PARAGRAPHS
    paragraph = text.text(doc_screen.surface, pygame.font.SysFont('arial', 20),
                          (255, 255, 255))

    #ARGUMENTS
    arg_heading = text.text(doc_screen.surface,
                            pygame.font.SysFont('arial', 20), (255, 255, 255))
    arg_heading.font.set_underline(True)

    #--------------------------------------------------------------------------------

    title.message("Slider", (grid.get_column(0.5), grid.get_row(1)),
                  center=True)

    paragraph.wrapped_text(
        "This object allows input from a specific range of numbers. The user can slide the bar inside the rectangle to their desired value. This value can be retrieved from the slider as a percentage of the slider’s length.",
        (grid.get_column(0.06), title.text_end[1] + 125), 580, 2)

    heading.message("Methods",
                    (grid.get_column(0.06), paragraph.text_end[1] + 100))

    heading_2.message("pgscript.slider.slider()",
                      (grid.get_column(0.06), heading.text_end[1] + 100))

    paragraph.wrapped_text(
        "Constructor method of the slider. It’s appearance and position are passed as arguments to this constructor.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    arg_heading.message("DISPLAY (pygame.Surface):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "The surface that the object needs to be drawn to. Usually this surface is the display surface of the window.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("colour (list [r,g,b]):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "colour of the slider bar.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("border_colour (list [r,g,b]):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "colour of the outline around the slider bar.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    arg_heading.message("rect (list (x,y,length,height)):",
                        (grid.get_column(0.06), paragraph.text_end[1] + 50))

    paragraph.wrapped_text(
        "Position of and size of the slider object.",
        (arg_heading.text_end[0] + 7, arg_heading.text_end[1]),
        580 - arg_heading.text_end[0] - 7, 2)

    heading_2.message("pgscript.slider.draw()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "Draws the slider object onto the DISPLAY surface. This method is meant to be called in the same loop where DISPLAY is updated",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.slider.update()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method uses pygame events to check for mouse positions and clicks in order to update the value of the slider.  It is meant to be run in a loop with each event given by pygame.event.get()",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.slider.get_value()",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method allows the program to read the value of the slider. It returns a float between 0 and 1 depending on how full the slider is.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)

    heading_2.message("pgscript.slider.set_value(value)",
                      (grid.get_column(0.06), paragraph.text_end[1] + 100))

    paragraph.wrapped_text(
        "This method allows the program to read the value of the slider. It returns a float between 0 and 1 depending on how full the slider is.",
        (grid.get_column(0.06), heading_2.text_end[1] + 75), 580, 2)
    #--------------------------------------------------------------------------------

    background = bg.parallax_bg(doc_surf, "obj_bg.jpg")
    doc_screen.add_objects(
        [title, heading, heading_2, arg_heading, paragraph, back_button])
    while not back_button.get_state():
        pygame.display.update()
        background.draw()

        demo_screen.fill([200, 200, 200, 200])
        demo.draw()
        demo_input.draw()
        run_demo.draw()
        reset_demo.draw()
        error_text.draw()
        doc_screen.surface.fill([255, 255, 255, 0])

        doc_screen.draw()
        DISPLAY.blit(doc_surf, (0, 0))
        DISPLAY.blit(demo_screen, (grid.get_column(1) + 13, 0))

        if run_demo.get_state():
            demo_args = demo_input.get_text()[14:-1]
            try:
                demo = slider.slider(
                    demo_screen, *parse_args(demo_args)
                )  #generate the object with args in the text field
            except:
                error_text.message("Invalid syntax",
                                   (grid.get_column(0.5), grid.get_row(7)),
                                   3,
                                   center=True)
            run_demo.reset_state()
        elif reset_demo.get_state():
            demo_input.set_text(demo_text)
            run_demo.pressed = True
            reset_demo.reset_state()

        for event in pygame.event.get():
            doc_screen.update(event)
            run_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo_input.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))
            reset_demo.update(
                event,
                (pygame.mouse.get_pos()[0] -
                 (DISPLAY.get_width() / 2) - 13, pygame.mouse.get_pos()[1]))