def workshop_loop(game_state):
    """The workshop screen loop.
    """
    built_sprites = game_state.get('built_sprites')
    if len(built_sprites) > 2:  # should be > 2, 0 for testing end game screen
        game_state = end_game(game_state)
        return game_state

    #remove all sprites from previous time screen was open
    general_sprites.empty()
    scrollable_sprites.empty()
    left_sprite.empty()
    right_sprite.empty()
    game_state.update({'active_sprite1': None, 'active_sprite2': None})

    game_surface = game_state.get('game_surface')
    clock = game_state.get('clock')
    fps = game_state.get('fps')
    click = game_state.get('click_sound')
    size = game_state.get('screen_size')
    screen_width = size[0]
    screen_height = size[1]

    toast_stack = game_state.get('toast_stack')
    available_funds = game_state.get('available_funds')
    company = game_state.get('company_name')

    held_down = False

    scroll_surface = pygame.surface.Surface(
        (screen_width * 0.2, screen_height * 0.8))
    scroll_rect = scroll_surface.get_rect(x=50, y=50)

    background_image = ImageSprite(0, 0,
                                   os.getcwd() + '/data/imgbase/workshop.png')
    general_sprites.add(background_image)

    #little hacky
    f = pygame.font.SysFont(None, 30)
    rendered_company_name_width = f.render(company, True,
                                           (0, 0, 0)).get_size()[0]
    general_sprites.add(
        TextSprite((screen_width * 0.31) + (250 * 0.5) -
                   (rendered_company_name_width * 0.5), screen_height * 0.32,
                   rendered_company_name_width, screen_height * 0.2, company))

    general_sprites.add(
        ButtonSprite(screen_width * 0.8, screen_height * 0.05, 'QUIT',
                     quit_game, []), )
    splice_button.add(
        ButtonSprite(screen_width * 0.4,
                     screen_height * 0.5,
                     'Splice!',
                     start_splicer, [],
                     color=(0, 255, 0),
                     text_color=(0, 0, 0)))

    items = os.listdir(os.getcwd() + '/data/pixel-components')

    x = 10
    y = 10

    general_sprites.add(
        ButtonSprite(50,
                     50 - 20,
                     'Up',
                     scroll_up, [scroll_surface],
                     w=screen_width * 0.2))
    general_sprites.add(
        ButtonSprite(50, screen_height * 0.8 + 50, 'Down', scroll_down,
                     [scroll_surface], screen_width * 0.2))

    for item in sorted(items):
        item_file = os.getcwd() + '/data/pixel-components/' + item
        temp_item = ButtonImageSprite(x,
                                      y,
                                      item_file,
                                      add_to_workbench, [item_file],
                                      w=100,
                                      h=100)
        temp_item.rect.centerx = x + (temp_item.w / 2)
        scrollable_sprites.add(temp_item)
        item_text = item[6:-4]
        scrollable_sprites.add(
            ButtonSprite(x + 110,
                         y + 40,
                         item_text,
                         add_to_workbench, [item_file],
                         w=100))
        y += 125

    frame_x = screen_width - 356
    frame_y = screen_height - 155

    for keepsake_entry in built_sprites:
        keepsake = keepsake_entry.get('sprite')
        keepsake.rect.x = frame_x
        keepsake.rect.y = frame_y - keepsake.rect.h
        general_sprites.add(keepsake)

        keepsake_name = keepsake_entry.get('name')
        general_sprites.add(
            TextSprite(frame_x, frame_y, screen_width * 0.1,
                       screen_height * 0.05, keepsake_name))

        frame_x += screen_width * 0.1
        frame_y += screen_height * 0.05

    # Want to refactor this body into seperate functions.
    while not game_state.get('screen_done'):

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game(game_state)

            elif event.type == pygame.MOUSEBUTTONDOWN:
                if scroll_rect.collidepoint(event.pos) and event.button == 4:
                    scroll_up(game_state, scroll_surface)
                elif scroll_rect.collidepoint(event.pos) and event.button == 5:
                    scroll_down(game_state, scroll_surface)
                elif (event.button == 1):
                    held_down = True
                    b = button_at_point(general_sprites, event.pos)
                    c = button_at_point(scrollable_sprites,
                                        (event.pos[0] - 50, event.pos[1] - 50))
                    if b:
                        click.play()
                        game_state = b.on_click(game_state)

                    if c and scroll_rect.collidepoint(event.pos):
                        click.play()
                        game_state = c.on_click(game_state)
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    held_down = False

        # Needed to hold down up and down scroll buttons
        if held_down:
            b = button_at_point(general_sprites, pygame.mouse.get_pos())
            b2 = button_at_point(splice_button, pygame.mouse.get_pos())
            if b:
                game_state = b.on_click(game_state)
            elif b2:
                game_state = b2.on_click(game_state)

        # Update.
        toast_stack.update()

        # Display.
        game_surface.fill((255, 0, 0))
        scroll_surface.fill((200, 200, 200))

        general_sprites.draw(game_surface)

        # only draw splice button if both sprites present
        if len(left_sprite.sprites()) and len(right_sprite.sprites()):
            splice_button.draw(game_surface)

        # draw scrollable items (hacky, obvs)
        offset = scrollable_sprites.sprites()[0].rect.y - 10
        for i, s in enumerate(scrollable_sprites.sprites()):
            bg = pygame.Surface((screen_width * 0.2, 125))
            if i % 2:
                bg.fill((150, 150, 150))
            else:
                bg.fill((50, 50, 50))
            scroll_surface.blit(bg, (0, (i * 125) + offset))
            scroll_surface.blit(s.image, s.rect)

        left_sprite.draw(game_surface)
        right_sprite.draw(game_surface)
        toast_stack.draw(game_surface)
        game_surface.blit(scroll_surface, (50, 50))

        funds_string = "Lifetime earnings: £{0:.2f}".format(available_funds)
        rendered_text = pygame.font.SysFont(None,
                                            25).render(funds_string, True,
                                                       (0, 0, 0))
        game_surface.blit(rendered_text,
                          (screen_width * 0.7, screen_height * 0.1))

        pygame.display.update()

        clock.tick(fps)

    return game_state
def main_menu_loop(game_state):
    """The main menu screen loop.
    """

    game_surface = game_state.get('game_surface')
    clock = game_state.get('clock')
    fps = game_state.get('fps')
    screen_size = game_state.get('screen_size')
    screen_width = screen_size[0]
    screen_height = screen_size[1]
    framecount = 1

    #toast_stack = game_state.get('toast_stack')
    logo_sprites = pygame.sprite.OrderedUpdates()
    logo = ImageSprite(screen_width * 0.315, screen_height * 0.15,
                       os.getcwd() + "/images/Tank.png")
    logo.rect.centerx = (screen_width / 2)
    logo_sprites.add(logo)

    # company_name = game_state.get('company_name')
    # input_font = pygame.font.Font("ARCADECLASSIC.TTF", 40)
    # input_width, input_height = 0.1* screen_width, 0.0625*screen_height

    # company_name_input = InputBox(
    #     (0.5*screen_width) - (0.5*input_width),
    #     0.68*screen_height,
    #     input_width + 10,
    #     input_height + 10,
    #     input_font,
    #     (0, 0, 255),
    #     (255, 255, 0),
    #     center_x=0.5*screen_width,
    #     text=company_name,
    #     max_width=500
    # )
    # company_name_input.active = True

    # Main group of sprites to display.
    all_sprites = pygame.sprite.OrderedUpdates()
    all_sprites.add(
        ButtonSprite((screen_width * 0.455), (screen_height * 0.8), 'Play!',
                     start_game, []),
        ButtonSprite(
            (screen_width * 0.455),
            (screen_height * 0.9),
            'Quit',
            quit_game,
            [],
        ),
    )

    # prompt = TextSprite((0.43*screen_width) , 0.62 *screen_height, 400, 30, "Enter Company Name", text_color=(255,255,255), arcade_font=True)
    # prompt.rect.centerx = (screen_width/2)
    # name_prompt = pygame.sprite.Group()
    # name_prompt.add(prompt)

    # Want to refactor this body into seperate functions.
    while not game_state.get('screen_done'):

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game(game_state)

            elif event.type == pygame.MOUSEBUTTONDOWN:
                b = button_at_point(all_sprites, event.pos)
                if b:
                    game_state = b.on_click(game_state)

            # elif event.type == pygame.KEYDOWN:
            #     if event.key == pygame.K_RETURN:
            #         game_state.update({'company_name': company_name_input.text})
            #         start_game(game_state)
            #     else:
            #         company_name_input.event_handle(event) #Input Box Class has inbuilt event handling function for key down events.

        # Update.
        all_sprites.update()
        #toast_stack.update()

        # Display.
        game_surface.fill((0, 0, 0))
        logo_sprites.draw(game_surface)
        all_sprites.draw(game_surface)

        pygame.display.update()

        clock.tick(fps)

    return game_state
Esempio n. 3
0
def game_end_loop(game_state):
    #The game end screen loop.

    game_surface = game_state.get('game_surface')
    clock = game_state.get('clock')
    click = game_state.get('click_sound')
    size = game_state.get('screen_size')
    screen_width = size[0]
    screen_height = size[1]

    built_sprites = game_state.get('built_sprites')

    toast_stack = game_state.get('toast_stack')
    available_funds = game_state.get('available_funds')

    general_sprites.add(
        ButtonSprite(screen_width * 0.5, screen_height * 0.05, 'QUIT',
                     quit_game, []))

    frame_x = screen_width * 0.2
    frame_y = screen_height * 0.2

    for keepsake_entry in built_sprites:
        keepsake = keepsake_entry.get('sprite')
        keepsake_name = keepsake_entry.get('name')
        keepsake.rect.x = frame_x
        keepsake.rect.y = frame_y
        frame_sprites.add(keepsake)
        general_sprites.add(
            TextSprite(frame_x, frame_y + 200, 200, 200, keepsake_name))
        frame_x += screen_width * 0.25

    # Want to refactor this body into seperate functions.
    while not game_state.get('screen_done'):
        # Handle events.
        hover_rect = None
        for sprite in frame_sprites:
            if sprite.rect.collidepoint(pygame.mouse.get_pos()):
                hover_rect = sprite.rect

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game(game_state)

            elif event.type == pygame.MOUSEBUTTONDOWN:

                if (event.button == 1):
                    for sprite in frame_sprites:
                        if sprite.rect.collidepoint(pygame.mouse.get_pos()):
                            game_state = blimp_screen(game_state, sprite)

                    b = button_at_point(general_sprites, event.pos)
                    if b:
                        click.play()
                        game_state = b.on_click(game_state)

        # Update.
        toast_stack.update()

        # Display.
        game_surface.fill((150, 150, 150))
        #background_sprite.draw(game_surface)
        general_sprites.draw(game_surface)
        frame_sprites.draw(game_surface)

        toast_stack.draw(game_surface)

        end_game_text = "Choose an item to take to the worlds fair!"

        rendered_text = pygame.font.SysFont(None,
                                            50).render(end_game_text, True,
                                                       (0, 0, 0))
        game_surface.blit(rendered_text,
                          (screen_width * 0.25, screen_height * 0.7))

        if hover_rect:
            pygame.draw.rect(game_surface, (255, 0, 0), hover_rect, 5)

        victory_screen_sprites.draw(game_surface)

        pygame.display.update()

        clock.tick(60)

    return game_state
Esempio n. 4
0
def arena_loop(game_state):
    """The arena screen loop."""

    game_surface = game_state.get('game_surface')
    clock = game_state.get('clock')
    fps = game_state.get('fps')
    size = game_state.get('screen_size')
    screen_width = size[0]
    screen_height = size[1]

    background_image = ImageSprite(0, 0,
                                   os.getcwd() + '/images/Background.png')
    general_sprites.add(background_image)

    general_sprites.add(
        ButtonSprite(screen_width * 0.8, screen_height * 0.05, 'QUIT',
                     quit_game, []), )

    tank1 = ImageSprite(0, 0, os.getcwd() + '/images/Tank.png')
    tank2 = ImageSprite(300, 0, os.getcwd() + '/images/Tank.png')
    tank_sprites.add(tank1, tank2)

    while not game_state.get('screen_done'):

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game(game_state)

            elif event.type == pygame.MOUSEBUTTONDOWN:

                b = button_at_point(general_sprites, event.pos)
                if b:
                    game_state = b.on_click(game_state)

            elif event.type == pygame.KEYDOWN:
                keys = pygame.key.get_pressed()
                if keys[pygame.K_RIGHT]:
                    tank1.move((10, 0))
                elif keys[pygame.K_LEFT]:
                    tank1.move((-10, 0))
                elif keys[pygame.K_UP]:
                    tank1.move((0, -10))
                elif keys[pygame.K_DOWN]:
                    tank1.move((0, 10))
                elif keys[pygame.K_d]:
                    tank2.move((10, 0))
                elif keys[pygame.K_a]:
                    tank2.move((-10, 0))
                elif keys[pygame.K_w]:
                    tank2.move((0, -10))
                elif keys[pygame.K_s]:
                    tank2.move((0, 10))

        # Display.
        game_surface.fill((255, 0, 0))

        general_sprites.draw(game_surface)
        tank_sprites.draw(game_surface)

        pygame.display.update()

        clock.tick(fps)

    return game_state
def result_loop(game_state):
    """The result screen loop.
    """

    game_surface = game_state.get('game_surface')
    click = game_state.get('click_sound')
    clock = game_state.get('clock')
    fps = game_state.get('fps')
    screen_size = game_state.get('screen_size')
    screen_width = screen_size[0]
    screen_height = screen_size[1]
    product = game_state.get('latest_product')
    company = game_state.get('company_name')

    toast_stack = game_state.get('toast_stack')
    newspaper = os.getcwd() + '/data/imgbase/Newspaper.png'

    # Main group of sprites to display.
    all_sprites = pygame.sprite.OrderedUpdates()
    w = (screen_width * 0.6)
    h = (screen_height * 0.6)
    x = (screen_width - w) * 0.5
    y = (screen_height - h) * 0.4
    newspaper = NewspaperSprite(x, y, newspaper, w, h, company, product)
    all_sprites.add(newspaper)

    # Money counter, gets added after newspaper is done.
    available_funds = game_state.get('available_funds')
    profit = sell(product, newspaper.review_type)
    game_state.update({'available_funds': available_funds + profit})
    money = MoneySprite((screen_width * 0.5), (screen_height * 0.85), profit)
    no_money = True

    # Done button, gets added after money is counted.
    done_button = ButtonSprite((screen_width * 0.05), (screen_height * 0.05),
                               'Done!', switch_to_screen, ['workshop_screen'])
    no_button = True

    # Want to refactor this body into seperate functions.
    while not game_state.get('screen_done'):

        # Handle events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game(game_state)

            elif event.type == pygame.MOUSEBUTTONDOWN:
                b = button_at_point(all_sprites, event.pos)
                if b:
                    click.play()
                    game_state = b.on_click(game_state)

        # Update.
        all_sprites.update()
        toast_stack.update()

        if (no_money and newspaper.done):
            pygame.time.wait(400)
            all_sprites.add(money)
            no_money = False
        if (no_button and money.done):
            all_sprites.add(done_button)
            no_button = False

        # Display.
        game_surface.fill((0, 0, 0))
        all_sprites.draw(game_surface)
        pygame.display.update()

        toast_stack.draw(game_surface)

        clock.tick(fps)

    return game_state
Esempio n. 6
0
def splicer_loop(game_state):
    """The splicer screen loop.
    """
    display_width = game_state.get('screen_size')[0]
    display_height = game_state.get('screen_size')[1]
    game_surface = game_state.get('game_surface')
    click = game_state.get('click_sound')
    clock = game_state.get('clock')
    fps = game_state.get('fps')
    active_sprite1 = game_state.get('active_sprite1')
    active_sprite2 = game_state.get('active_sprite2')
    game_state.update({'crop_sprite' : None})
    thumbnail_size = [0.2*display_width, 0.2*display_height]
    hover_rects1= []
    hover_rects2 = []
    game_state.update({'delete_mode': False })
    game_state.update({'copy_mode': False})

    splice_canvas = pygame.Rect(0.35*display_width, 0.035*display_height, 0.635* display_width, 0.93*display_height) #set splice canvas area that is captured by screenshot.
    splice_canvas_surface = pygame.Surface((splice_canvas.w, splice_canvas.h), pygame.SRCALPHA, 32)
    game_state.update({'splice_canvas': splice_canvas})
    
    # make the input box
    active_input = InputBox(
        0.05*display_width,
        0.0625*display_height,
        0.2*display_height,
        0.0625*display_height,
        pygame.font.Font("ARCADECLASSIC.TTF", 40),
        (0,0,255),
        (255,255,0),
        0.175*display_width,
        '',
        0.25*display_width,
        0.33*display_width
    )

    confirm_splice = ConfirmBox( display_width/2, display_height/2 , "Confirm Splice")
    confirm_crop = ConfirmBox( display_width/6, (display_height*3)/4 , "Confirm Crop")
    
    #generate all confirmation boxes that could be spawned by this screen

    toast_stack = game_state.get('toast_stack')

    splice_sprites.empty()
    splice_thumb1.empty()
    splice_thumb2.empty()
    thumb1 = ThumbnailSprite(0.1*display_width, 0.2*display_height, active_sprite1, thumbnail_size[0], thumbnail_size[1] )
    thumb1.rect.centerx = 0.1*display_width
    thumb2 = ThumbnailSprite(0.1*display_width, 0.45*display_height, active_sprite2,  thumbnail_size[0], thumbnail_size[1])
    thumb2.rect.centerx = 0.1*display_width
    splice_thumb1.add(thumb1)
    splice_thumb2.add(thumb2)

    #make the thumbnails of your activesprites

    help_sprites = load_help_sprites(game_state)
    
    #make sprites for the help pop-up

    load_buttons(game_state, splice_canvas, confirm_splice, confirm_crop)
    
    # Want to move these elsewhere/design them away.
    dragging = False
    dragged_sprite = None
    selected = None

    while not game_state.get('screen_done'):
        if pygame.mouse.get_pos():
            s = top_draggable_sprite_at_point(splice_sprites, get_relative_mouse_pos((splice_canvas.x, splice_canvas.y)))
        else:
            s = None
        if selected:
            s= selected

        if s:
            hover_rects1 = [s.rect]
            if s.selected == True:
                r = s.rect
                hover_rects2 = [
                    pygame.Rect((r.x - 2), (r.y - 2), 10, 10),
                    pygame.Rect((r.x + r.w - 8), (r.y - 2), 10, 10), 
                    pygame.Rect((r.x + r.w - 8), (r.y + r.h - 8), 10, 10),
                    pygame.Rect((r.x - 2), (r.y + r.h - 8), 10, 10)
                ]

            else:
                hover_rects2 = []

        else:
            hover_rects1 = []
            hover_rects2 = []
        # Handle events.
        for event in pygame.event.get():
           
            if event.type == pygame.QUIT:
                quit_game(game_state)

            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:

                    # Hacky, but the expectation here is real
                    if splice_thumb1.sprites()[0].rect.collidepoint(pygame.mouse.get_pos()):
                            game_state = add_sprite(game_state, "1")
                    elif splice_thumb2.sprites()[0].rect.collidepoint(pygame.mouse.get_pos()):
                            game_state = add_sprite(game_state, "2")

                    else:
                        s = top_draggable_sprite_at_point(splice_sprites, get_relative_mouse_pos((splice_canvas.x, splice_canvas.y)))
                        if s:
                            if game_state.get('delete_mode') == True:
                                splice_sprites.remove(s)

                            elif game_state.get('copy_mode') == True:
                                copied_image = s.clone(offset=(20, 20))

                                splice_sprites.add(copied_image)

                                #s.update_sprite()

                            else:
                                dragging = True
                                dragged_sprite = s
                                splice_sprites.remove(s)
                                splice_sprites.add(s)
                        if active_input.rect.collidepoint(pygame.mouse.get_pos()) == True:
                            active_input.toggle_active()
                
                    b = button_at_point(control_sprites, pygame.mouse.get_pos())
                    if b:
                        game_state.update({'new_sprite_name': active_input.text}) # TODO: this is a little hacky.
                        game_state = b.on_click(game_state)
                    if event.button == 3: #right click to select lock a sprite you are hovering on
                        if s:
                            if s.selected == False:
                                s.toggle_selected()
                                selected = s
                            elif s.selected == True:
                                s.toggle_selected()
                                selected = None

            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:
                    dragging = False
                    dragged_sprite = None

            elif event.type == pygame.MOUSEMOTION:
                if dragging:
                    dragged_sprite.move(event.rel)
                
            
            if active_input.active == True:      
                active_input.event_handle(event) #Input Box Class has inbuilt event handling function for key down events.
            elif active_input.active == False:
                if event.type == pygame.KEYDOWN:
                    if s:
                        if event.key == pygame.K_LEFT:
                            s.rotate_counterclockwise()
                        if event.key == pygame.K_RIGHT:
                            s.rotate_clockwise()
                        if event.key == pygame.K_DELETE:
                            splice_sprites.remove(s)


        keys = pygame.key.get_pressed()
        if s:
            if keys[pygame.K_UP]:
                s.scale_up()
            if keys[pygame.K_DOWN]:
                s.scale_down()

        # Update.
        toast_stack.update()

        # Display.
        game_surface.fill(dark_brown)
        splice_thumb1.draw(game_surface)
        splice_thumb2.draw(game_surface)

        if game_state.get('delete_mode') == True:
            pygame.draw.rect(game_surface, (200,100, 200), ((0.28*display_width-2), (0.675*display_height -2), 74, 74))
        elif game_state.get('copy_mode') == True:
            pygame.draw.rect(game_surface, (200,100, 200), ((0.21*display_width-2), (0.675*display_height -2), 74, 74))

        control_sprites.draw(game_surface)
        
        # Only draw the splice sprites inside the splice canvas
        splice_canvas_surface.fill(white)
        splice_sprites.draw(splice_canvas_surface)
        draw_rects(hover_rects1, splice_canvas_surface, black, 2)
        draw_rects(hover_rects2, splice_canvas_surface, red, 0)
        game_surface.blit(splice_canvas_surface, (splice_canvas.x, splice_canvas.y))

        active_input.draw_input_box(game_state)

        #confirm_splice.draw_confirm_box(game_state)

        if game_state.get('tutorial') == True:
            open_help(game_state, help_sprites)

        
            
            
        toast_stack.draw(game_surface)

        pygame.display.update()

        clock.tick(fps)

    return game_state