Пример #1
0
def load_buttons(game_state, splice_canvas, confirm_splice, confirm_crop):
    x = game_state.get('screen_size')[0]
    y = game_state.get('screen_size')[1]
    
    helpbutton =ButtonImageSprite(0.25*x, 0.8*y, os.getcwd() + "/data/imgbase/helpbuttonsmall.png", toggle_help, [])
    helpbutton.rect.centerx = (0.28 * x) - (((0.07 * x) - 70) / 2)
    workshop=ButtonSprite(0.25*x, 0.92*y, 'WORKSHOP', switch_to_workshop, [])
    workshop.rect.centerx = 0.28*x - ((0.07*x-70)/2)
    quit =ButtonSprite(0.25*x, 0.96*y, 'QUIT', quit_game, [])
    quit.rect.centerx = 0.28*x - ((0.07*x-70)/2)

    splice =ButtonImageSprite(0.05*x, 0.68*y, os.getcwd() + "/data/imgbase/" + "splicebuttonsmall.png", screenshot, [splice_canvas, confirm_splice])
    splice.rect.centerx = 0.11*x

    control_sprites.add(
        ButtonImageSprite(0.21*x, 0.18*y, os.getcwd() + "/data/imgbase/addbuttonsmall.png", add_sprite, ["1"]),
        ButtonImageSprite(0.28*x, 0.18*y, os.getcwd() + "/data/imgbase/cropbuttonsmall.png", crop, ["1", confirm_crop]),
        ButtonImageSprite(0.21*x, 0.295*y, os.getcwd() + "/data/imgbase/flipbuttonsmall.png", add_sprite, ["1", True]),
        #ButtonImageSprite(0.28*x, 0.295*y, os.getcwd() + "/data/imgbase/mirrorcropbuttonsmall.png", crop, ["1", confirm_crop, True] ),

        ButtonImageSprite(0.21*x, 0.43*y, os.getcwd() + "/data/imgbase/addbuttonsmall.png", add_sprite, ["2"]),
        ButtonImageSprite(0.28*x, 0.43*y, os.getcwd() + "/data/imgbase/cropbuttonsmall.png", crop, ["2", confirm_crop]),
        ButtonImageSprite(0.21*x, 0.545*y, os.getcwd() + "/data/imgbase/flipbuttonsmall.png", add_sprite, ["2", True]),
        #ButtonImageSprite(0.28*x, 0.545*y, os.getcwd() + "/data/imgbase/mirrorcropbuttonsmall.png", crop, ["2", confirm_crop, True]),

        ButtonImageSprite(0.21*x, 0.675*y, os.getcwd() + "/data/imgbase/copybuttonsmall.png", toggle_copy_mode, []),
        ButtonImageSprite(0.28*x, 0.675*y, os.getcwd() + "/data/imgbase/delbuttonsmall.png", toggle_delete_mode, []),

        splice,
        helpbutton,
        workshop,
        quit,      
    )
    return game_state
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 end_game(game_state):
    return switch_to_screen(game_state, 'game_end_screen')


# Main group of sprites to display.
general_sprites = pygame.sprite.OrderedUpdates()
splice_button = pygame.sprite.OrderedUpdates()
scrollable_sprites = pygame.sprite.OrderedUpdates()
left_sprite = pygame.sprite.OrderedUpdates()
right_sprite = pygame.sprite.OrderedUpdates()

left_remove_button = ButtonSprite(300,
                                  375,
                                  'X',
                                  remove_workbench_item, ['left'],
                                  w=50)  #350, 400
right_remove_button = ButtonSprite(600,
                                   375,
                                   'X',
                                   remove_workbench_item, ['right'],
                                   w=50)


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)
Пример #4
0
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
Пример #5
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
Пример #6
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
Пример #7
0
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