def test_operations(self) -> None: """ Test the file operations. """ if SYS_PLATFORM_OSX: return image_original = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) self.assertTrue(image.equals(image_original)) # Flip image.flip(True, False) self.assertFalse(image.equals(image_original)) image.restore() self.assertTrue(image.equals(image_original)) # Checkpoint image.flip(True, False) self.assertFalse(image.equals(image_original)) image.checkpoint() self.assertFalse(image.equals(image_original)) image.restore() self.assertFalse(image.equals(image_original))
def test_from_bytesio(self) -> None: """ Test image load from base64. """ if SYS_PLATFORM_OSX: return photo = 'R0lGODlhRgBGAPZUAAAAAAAAMwAAzAArAAArMwArzAAr/wBVmQBVzABV/zMAADMAMzMrADMrMzMrmTMrzDMr/zNVADNVMzNVZjNVmTN' \ 'VzDNV/zOAADOAMzOAZjOA/zOqM2YAM2YrAGYrM2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmW' \ 'aqzJkrAJkrM5lVAJlVM5lVZplVmZmAAJmAM5mAZpmAmZmAzJmqZpmqmZmqzJmq/5nVmZnVzMxVAMxVM8xVZsyAAMyAM8yAZsyqZsyqm' \ 'cyqzMyq/8zVmczVzMzV/8z/zMz////VzP/V////zP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \ 'AAAAAAAAAAAAAACH5BAUAAFQALAAAAABGAEYAAAf/gFOCg4SFhoeIiYZMOScoKI45SUxJk4qXmJmJSSghKCI4OSInOT1JTT05TJqsrY' \ 'hMPCETnpCQIjyVSbiuvK1NjJ2jIiE5KI0ouJQ8PVC9zohPusWOndU4o5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ogf1rWcv61dDdBaih8wBDD9GEFFQgpglIbVutKDMnmO+WJyRPibBKnxLZdqS07' \ 'aySOBM6oe4grYN4aQEQi9oCUMAF20vCbNIEskuWiKQi9WtJgcokQ1lzB0yQXyGcoAMLEBe1BqhAD/jyAyJtwHcDCiMot+otInEKL9xZ' \ 'hAUKgFoEQDSmFngaXwDaH9Xsxz9/MY+nPvVFnZInjoaQgg8egNoGQxQCPrAY/3hgRiRbFuyW12wSf+jCA3t+xQdsBW8C7JuAFLS0tJH' \ 'DJXIYGDKo2tJF4nHlIJ7QhBGEeMT3fbAOgPCVHlQgBLUEVRiHXGH0aKqj5OGBdvkyAhITN8dNVQJfdACBCVjxoIMiF6D4palP8jNAv5' \ 'TAE7vpY+Iy9b70hWgFKmjaBBRAohsmWEGpmbC6DOwvdMMABVLQg5MloQIh/i5iC5BfQlRguDGtwEzVi9BGA8qHnuolqGgqAftaYM1SK' \ 'K2b6wtlNFfgxIdWLQIOoKS5HmeujU71eltnAAO82QcheeiTU0tf1Yb3BVGsgACIS+gCHMDKOtPQeDTEXLxM4AAIqHWrE/tw6bY2WLXg' \ 'RYAFqDWFlwpa1CN+8wEcgAAKdLvbB6AABcId7nFz+wCHJrGD171pKMfUACnwShTm22kRfzfN9M23fYfrZBH729nuRuyZE/CA8XZT1s+' \ '+9rvX/V12uzvN9m6wst1sgWR5t60QhXi/ax3xWWdcxBB3MIn7jdgH5JebU+jBxd+N2GUPt+VpbjnLF+7yZbe65iyX+c1H0APgCkLlBr' \ 'AAs5ktbWYHXdpBN7oFin5aoTcdARAYutCnHtEERJ3qQo960xPA81MdwgcqsEDYVaABEpD97CnQgArITgK1l33sYy+72+NudrGfXe1in' \ 'zveVVABFSQBYIgIBAA7' output = io.BytesIO(base64.b64decode(photo)) image = pygame_menu.BaseImage(output) self.assertEqual(image.get_width(), 70) self.assertEqual(image.get_height(), 70) self.assertEqual(image.get_bitsize(), 8) self.assertEqual(image.get_size(), (70, 70)) self.assertEqual(image.get_path(), output) self.assertEqual(image.get_extension(), 'BytesIO') # Assemble new from base64 bs image2 = pygame_menu.BaseImage(photo, frombase64=True) self.assertTrue(image.equals(image2)) self.assertEqual(image2.get_extension(), 'base64') # Clone base64 image3 = image2.copy() if PYGAME_V2: self.assertTrue(image2.equals(image3))
def test_transform(self) -> None: """ Test the image transformation. """ image_original = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) # Scale image.scale(0.5, 0.5) self.assertEqual(image.get_size(), (128, 128)) self.assertRaises(AssertionError, lambda: image.scale(0, 1)) image.scale(2, 1) self.assertEqual(image.get_size(), (256, 128)) self.assertFalse(image.equals(image_original)) # Set size image.restore() image.resize(100, 50) image.resize(100, 50) # This should do nothing self.assertEqual(image.get_size(), (100, 50)) image.restore() self.assertTrue(image.equals(image_original)) # As the example is not 24/32 bits smooth scale fails, but baseimage should notice that imag_c = image.copy() imag_c.resize(100, 100) # Get rect rect = image.get_rect() self.assertEqual(rect.width, 256) # Rotate image.rotate(45) self.assertEqual(image.get_size(), (362, 362)) image.restore() # Scale 2x image.scale2x() self.assertEqual(image.get_size(), (512, 512)) image.restore() # Scale should not change image.scale(1, 1) # Image bw image.to_bw() # Image channels # noinspection PyTypeChecker image.pick_channels(('r', 'g', 'b')) if not SYS_PLATFORM_OSX: self.assertEqual(image.get_at((10, 10)), (56, 56, 56, 255)) image.set_at((10, 10), (0, 0, 0))
def test_extension_validation(self) -> None: """ Validate a image extension. """ self.assertRaises(AssertionError, lambda: pygame_menu.BaseImage('invalid.pnng')) self.assertRaises(AssertionError, lambda: pygame_menu.BaseImage('invalid')) self.assertRaises(AssertionError, lambda: pygame_menu.BaseImage('file_invalid.png')) pygame_menu.BaseImage(pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES)
def test_widget_floating_zero(self) -> None: """ Test widgets with zero position if floated. """ menu = MenuUtils.generic_menu(title='Example menu') img = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_PYGAME_MENU) img.scale(0.3, 0.3) image_widget = menu.add.image(image_path=img.copy()) image_widget.set_border(1, 'black') image_widget.set_float(origin_position=True) menu.render() # Test position self.assertEqual(image_widget.get_position(), (8, 60)) # Image translate image_widget.translate(100, 100) self.assertEqual(image_widget.get_position(), (8, 60)) # Render, then update the position menu.render() self.assertEqual(image_widget.get_position(), (108, 160)) image_widget.translate(-50, 0) menu.render() self.assertEqual(image_widget.get_position(), (-42, 60))
def test_attributes(self) -> None: """ Test image attributes. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_offset=(100, 100)) self.assertFalse(image.has_attribute('epic')) self.assertRaises(IndexError, lambda: image.remove_attribute('epic')) image.set_attribute('epic', True) self.assertTrue(image.has_attribute('epic')) self.assertTrue(image.get_attribute('epic')) image.set_attribute('epic', False) self.assertFalse(image.get_attribute('epic')) image.remove_attribute('epic') self.assertFalse(image.has_attribute('epic')) self.assertEqual(image.get_attribute('epic', 420), 420) image.set_attribute('angle', 0) image2 = image.copy() self.assertTrue(image2.has_attribute('angle')) self.assertEqual(image2.get_attribute('angle'), 0) self.assertEqual(image.get_attribute('angle'), 0) image2.set_attribute('angle', 1) self.assertEqual(image2.get_attribute('angle'), 1) self.assertEqual(image.get_attribute('angle'), 0)
def test_scale(self) -> None: """ Test scale. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) w, h = image.get_size() self.assertEqual(w, 256) self.assertEqual(h, 256) image4 = image.copy().scale(4, 4) self.assertEqual(image4.get_size(), (1024, 1024)) # Apply scale2x algorithm image4a = image.copy().scale2x().scale2x() self.assertEqual(image4a.get_size(), (1024, 1024)) image4b = image.copy().scale4x() image.scale(2, 2).scale(2, 2) self.assertEqual(image.get_size(), (1024, 1024)) # Check if equal self.assertTrue(image.equals(image4)) if SYS_PLATFORM_OSX: return self.assertFalse(image.equals(image4a)) self.assertTrue(image4a.equals(image4b))
def test_drawing_position(self) -> None: """ Test drawing position. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_offset=(100, 100)) w, h = image.get_size() image.set_drawing_position(pygame_menu.locals.POSITION_NORTHWEST) self.assertEqual(image._get_position_delta(), (0, 0)) image.set_drawing_position(pygame_menu.locals.POSITION_NORTH) self.assertEqual(image._get_position_delta(), (w / 2, 0)) image.set_drawing_position(pygame_menu.locals.POSITION_NORTHEAST) self.assertEqual(image._get_position_delta(), (w, 0)) image.set_drawing_position(pygame_menu.locals.POSITION_WEST) self.assertEqual(image._get_position_delta(), (0, h / 2)) image.set_drawing_position(pygame_menu.locals.POSITION_CENTER) self.assertEqual(image._get_position_delta(), (w / 2, h / 2)) image.set_drawing_position(pygame_menu.locals.POSITION_EAST) self.assertEqual(image._get_position_delta(), (w, h / 2)) image.set_drawing_position(pygame_menu.locals.POSITION_SOUTHWEST) self.assertEqual(image._get_position_delta(), (0, h)) image.set_drawing_position(pygame_menu.locals.POSITION_SOUTH) self.assertEqual(image._get_position_delta(), (w / 2, h)) image.set_drawing_position(pygame_menu.locals.POSITION_SOUTHEAST) self.assertEqual(image._get_position_delta(), (w, h)) self.assertRaises( AssertionError, lambda: image.set_drawing_position(pygame_menu.locals.ALIGN_LEFT))
def test_subsurface(self) -> None: """ Test subsurface. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_TILED_BORDER) self.assertEqual(image.get_size(), (18, 18)) self.assertEqual(image.subsurface((0, 0, 3, 3)).get_size(), (3, 3))
def test_pathlib(self) -> None: """ Test image load with pathlib. """ path_img = Path(pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) image = pygame_menu.BaseImage(path_img) image.draw(surface) self.assertEqual(image.get_path(), str(path_img))
def menu(self): #bg_menu = pygame.image.load('E:/Python Online/Jalase 12/bird_shot/image/duck-hunt-header.png') menu = pygame_menu.Menu(480, 852, 'Welcome To Duck Shot',theme=pygame_menu.themes.THEME_GREEN) duck_image = pygame_menu.BaseImage('E:/Python Online/Jalase 12/bird_shot/image/duck.png') duck_donkey = pygame_menu.BaseImage('E:/Python Online/Jalase 12/bird_shot/image/donkey.png') duck_st = pygame_menu.BaseImage('E:/Python Online/Jalase 12/bird_shot/image/stork.png') #background_image.draw(self.display) menu.add.image(duck_image, angle=20, scale=(1, 1)) menu.add.image(duck_donkey, angle=20, scale=(1, 1)) menu.add.image(duck_st, angle=20, scale=(1, 1)) #mage_path, angle=0, image_id='', onselect=None, scale=(1, 1), scale_smooth=True, selectable=False, menu.add.text_input('Name :', default='Insert Name') #menu.add.selector('Difficulty :', [('Hard', 1), ('Easy', 2)], onchange=play) menu.add.button('Play', game.play) menu.add.button('Quit', pygame_menu.events.EXIT) menu.mainloop(self.display)
def test_drawing_offset(self) -> None: """ Test drawing offset. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_mode=IMAGE_MODE_CENTER) image.set_drawing_offset((50, 150)) self.assertEqual(image.get_drawing_offset(), (50, 150))
def test_image_path(self) -> None: """ Test path. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_mode=IMAGE_MODE_CENTER) self.assertEqual(image.get_path(), pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES)
def test_alpha(self) -> None: """ Test alpha modes. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) self.assertRaises(AssertionError, lambda: image.set_alpha(0.5)) self.assertRaises(AssertionError, lambda: image.set_alpha(-1)) self.assertRaises(AssertionError, lambda: image.set_alpha(267)) image.set_alpha(None)
def test_image_properties(self) -> None: """ Test the getters of the image object. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) w, h = image.get_size() self.assertEqual(w, 256) self.assertEqual(h, 256) self.assertEqual(image.get_filename(), 'gray_lines') self.assertEqual(image.get_extension(), '.png')
def test_methods(self) -> None: """ Test theme method. """ image = pygame_menu.BaseImage(pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) # Create theme theme = pygame_menu.themes.THEME_DEFAULT.copy() theme.background_color = image self.assertRaises(AssertionError, lambda: theme.set_background_color_opacity(0.5))
def test_copy(self) -> None: """ Test copy image. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) image_copied = image.copy() self.assertTrue(image.equals(image_copied)) image_copy = copy.copy(image) image_copy2 = copy.deepcopy(image) self.assertTrue(image.equals(image_copy)) self.assertTrue(image.equals(image_copy2))
def test_copy(self) -> None: """ Test theme copy. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) # Create theme theme = pygame_menu.themes.THEME_DEFAULT.copy() theme.background_color = image # Copy the theme theme_copy = theme.copy() self.assertNotEqual(theme.background_color, theme_copy.background_color) self.assertNotEqual(theme.background_color, pygame_menu.themes.THEME_DEFAULT.background_color) # Test attribute copy color_main = (29, 120, 107, 255) color_copy = (241, 125, 1) theme_white = pygame_menu.themes.Theme(scrollbar_thick=50, selection_color=color_main) sub_theme_white = theme_white.copy() sub_theme_white.selection_color = color_copy self.assertEqual(theme_white.selection_color, color_main) self.assertEqual(sub_theme_white.selection_color, color_copy) self.assertNotEqual(theme_white.selection_color, sub_theme_white.selection_color) self.assertNotEqual(theme_white.widget_selection_effect, sub_theme_white.widget_selection_effect) # Test the widget effect color is different in both objects m1 = MenuUtils.generic_menu(theme=theme_white) m2 = MenuUtils.generic_menu(theme=sub_theme_white) b1 = m1.add.button('1') b2 = m2.add.button('2') self.assertEqual(b1._selection_effect.color, b1.get_menu().get_theme().selection_color) self.assertEqual(b2._selection_effect.color, b2.get_menu().get_theme().selection_color) self.assertNotEqual(b1._selection_effect.color, b2._selection_effect.color) # Main Theme selection effect class should not be modified self.assertEqual(b1.get_menu().get_theme(), theme_white) self.assertEqual(theme_white.widget_selection_effect.color, (0, 0, 0))
def test_background(self) -> None: """ Test widget background. """ menu = MenuUtils.generic_menu() w = menu.add.label('Text') w.set_background_color((255, 255, 255), (10, 10)) w.draw(surface) self.assertEqual(w._background_inflate[0], 10) self.assertEqual(w._background_inflate[1], 10) w.set_background_color( pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES)) w.draw(surface)
def test_copy(self) -> None: """ Test theme copy. """ image = pygame_menu.BaseImage(pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) # Create theme theme = pygame_menu.themes.THEME_DEFAULT.copy() theme.background_color = image # Copy the theme themecopy = theme.copy() self.assertNotEqual(theme.background_color, themecopy.background_color) self.assertNotEqual(theme.background_color, pygame_menu.themes.THEME_DEFAULT.background_color)
def test_rotation(self) -> None: """ Test rotation. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) image.rotate(360) prev_size = image.get_size() self.assertEqual(prev_size, (256, 256)) i_sum = 0 for i in range(91): image.rotate(i_sum) i_sum += 1 # Rotate the image many angles self.assertEqual(image.get_size(), prev_size) image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_PYGAME_MENU) self.assertEqual(image.get_size(), (640, 505)) image.rotate(90) self.assertEqual(image.get_size(), (505, 640)) image.rotate(180) self.assertEqual(image.get_size(), (640, 505)) image.rotate(270) self.assertEqual(image.get_size(), (505, 640)) image.rotate(360) self.assertEqual(image.get_size(), (640, 505)) self.assertEqual(image.get_angle(), 0) image.rotate(60) self.assertEqual(image.get_size(), (757, 806)) self.assertEqual(image.get_angle(), 60) image.rotate(160) self.assertEqual(image.get_size(), (774, 693)) self.assertEqual(image.get_angle(), 160) image.rotate(180) self.assertEqual(image.get_angle(), 180) self.assertEqual(image.get_size(), (640, 505))
def test_attributes_copy(self) -> None: """ Test image attributes on object copy. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_offset=(100, 100)) image.set_attribute('angle', 0) image2 = image.copy() self.assertTrue(image2.has_attribute('angle')) self.assertEqual(image2.get_attribute('angle'), 0) self.assertEqual(image.get_attribute('angle'), 0) image2.set_attribute('angle', 1) self.assertEqual(image2.get_attribute('angle'), 1) self.assertEqual(image.get_attribute('angle'), 0)
def test_invalid_image(self) -> None: """ Test invalid image opening. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_PYTHON) self.assertEqual(image.get_size(), (110, 109)) image._drawing_position = 'invalid' self.assertRaises(ValueError, lambda: image._get_position_delta()) # Test invalid image self.assertRaises( Exception, lambda: load_pygame_image_file( pygame_menu.baseimage.IMAGE_EXAMPLE_PYTHON, test=True))
def test_modes(self) -> None: """ Test drawing modes. """ for mode in [ IMAGE_MODE_CENTER, IMAGE_MODE_FILL, IMAGE_MODE_REPEAT_X, IMAGE_MODE_REPEAT_XY, IMAGE_MODE_REPEAT_Y, IMAGE_MODE_SIMPLE ]: image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_mode=mode) image.draw(surface) # Attempt to create an invalid drawing mode self.assertRaises( AssertionError, lambda: pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_mode=-1)) # Get drawing mode image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_mode=IMAGE_MODE_CENTER) self.assertEqual(image.get_drawing_mode(), IMAGE_MODE_CENTER)
def test_format_opacity(self) -> None: """ Test format opacity color. """ t = pygame_menu.themes.THEME_DEFAULT self.assertEqual(t._format_opacity((1, 2, 3)), (1, 2, 3, 255)) self.assertEqual(t._format_opacity([1, 2, 3]), (1, 2, 3, 255)) self.assertEqual(t._format_opacity([1, 2, 3, 25]), (1, 2, 3, 25)) self.assertRaises(AssertionError, lambda: t._format_opacity([1, 2])) img = pygame_menu.BaseImage(pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) self.assertEqual(img, t._format_opacity(img)) self.assertEqual(t._format_opacity(None), None) self.assertRaises(ValueError, lambda: t._format_opacity('1,2,3')) self.assertRaises(AssertionError, lambda: t._format_opacity((1, 1, -1))) self.assertRaises(AssertionError, lambda: t._format_opacity((1, 1, 1.1)))
def test_crop(self) -> None: """ Test baseimage crop. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) image_c = image.get_crop(0, 0, 10, 10) image_cr = image.get_crop_rect(pygame.Rect(0, 0, 10, 10)) im1 = pygame.image.tostring(image_c, 'RGBA') im2 = pygame.image.tostring(image_cr, 'RGBA') self.assertTrue(im1 == im2) # Save the whole image crop w, h = image.get_size() image2 = image.copy() image2.crop(0, 0, w, h) self.assertTrue(image2.equals(image)) # Crop from rect image.crop_rect(pygame.Rect(0, 0, 8, 8)) self.assertEqual(image.get_size(), (8, 8))
def test_cache(self) -> None: """ Cache draw test. """ image = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) self.assertIsNone(image._last_transform[2]) image.set_drawing_mode(pygame_menu.baseimage.IMAGE_MODE_FILL) # Draw, this should force cache image.draw(surface) self.assertIsNotNone(image._last_transform[2]) s = image._last_transform[2] image.draw(surface) # Draw again, then the image should be the same self.assertEqual(image._last_transform[2], s) self.assertEqual(image._last_transform[0], 600) # Changes the area, then image should change r = image.get_rect() r.width = 300 image.draw(surface, r) self.assertNotEqual(image._last_transform[2], s) self.assertEqual(image._last_transform[0], 300)
def __init__(self) -> None: """ Constructor. """ self.surface = create_example_window('Example - Dynamic Widget Update', (640, 480), flags=pygame.NOFRAME) # Load image default_image = pygame_menu.BaseImage( image_path=pygame_menu.baseimage.IMAGE_EXAMPLE_PYGAME_MENU).scale( 0.2, 0.2) # Set theme theme = pygame_menu.themes.THEME_DEFAULT.copy() theme.title_bar_style = pygame_menu.widgets.MENUBAR_STYLE_UNDERLINE_TITLE theme.title_close_button_cursor = pygame_menu.locals.CURSOR_HAND theme.title_font_color = (35, 35, 35) # This dict stores the values of the widgets to be changed dynamically self.modes = { 1: { 'image': default_image.copy(), 'label': { 'color': theme.widget_font_color, 'size': theme.widget_font_size, 'text': 'The first one is very epic' } }, 2: { 'image': default_image.copy().to_bw(), 'label': { 'color': (0, 0, 0), 'size': 20, 'text': 'This other one is also epic, but fancy' } }, 3: { 'image': default_image.copy().flip(False, True).pick_channels('r'), 'label': { 'color': (255, 0, 0), 'size': 45, 'text': 'YOU D I E D' } } } # Create menus self.menu = pygame_menu.Menu(height=480, onclose=pygame_menu.events.CLOSE, theme=theme, title='Everything is dynamic now', width=640) self.selector_widget = self.menu.add.selector( title='Pick one option: ', items=[('The first', 1), ('The second', 2), ('The final mode', 3)], onchange=self._on_selector_change) self.image_widget = self.menu.add.image( image_path=self.modes[1]['image'], padding=(25, 0, 0, 0) # top, right, bottom, left ) self.item_description_widget = self.menu.add.label(title='') self.quit_button = self.menu.add.button('Quit', pygame_menu.events.EXIT) self.quit_button_fake = self.menu.add.button('You cannot quit', self.fake_quit, font_color=(255, 255, 255)) self.quit_button_fake.add_draw_callback(self.animate_quit_button) # Update the widgets based on selected value from selector get_value # returns selected item tuple and index, so [0][1] means the second object # from ('The first', 1) tuple self._update_from_selection(int( self.selector_widget.get_value()[0][1]))
def main(test: bool = False) -> None: """ Main program. :param test: Indicate function is being tested :return: None """ # ------------------------------------------------------------------------- # Globals # ------------------------------------------------------------------------- global main_menu global sound global surface # ------------------------------------------------------------------------- # Create window # ------------------------------------------------------------------------- surface = create_example_window('Example - Multi Input', WINDOW_SIZE) clock = pygame.time.Clock() # ------------------------------------------------------------------------- # Set sounds # ------------------------------------------------------------------------- sound = pygame_menu.sound.Sound() # Load example sounds sound.load_example_sounds() # Disable a sound sound.set_sound(pygame_menu.sound.SOUND_TYPE_ERROR, None) # ------------------------------------------------------------------------- # Create menus: Settings # ------------------------------------------------------------------------- settings_menu_theme = pygame_menu.themes.THEME_ORANGE.copy() settings_menu_theme.title_offset = (5, -2) settings_menu_theme.widget_alignment = pygame_menu.locals.ALIGN_LEFT settings_menu_theme.widget_font = pygame_menu.font.FONT_OPEN_SANS_LIGHT settings_menu_theme.widget_font_size = 20 settings_menu = pygame_menu.Menu(height=WINDOW_SIZE[1] * 0.85, theme=settings_menu_theme, title='Settings', width=WINDOW_SIZE[0] * 0.9) # Add text inputs with different configurations settings_menu.add.text_input('First name: ', default='John', onreturn=check_name_test, textinput_id='first_name') settings_menu.add.text_input('Last name: ', default='Rambo', maxchar=10, textinput_id='last_name', input_underline='.') settings_menu.add.text_input('Your age: ', default=25, maxchar=3, maxwidth=3, textinput_id='age', input_type=pygame_menu.locals.INPUT_INT, cursor_selection_enable=False) settings_menu.add.text_input('Some long text: ', maxwidth=19, textinput_id='long_text', input_underline='_') settings_menu.add.text_input('Password: '******'pass', input_underline='_') # Selectable items items = [('Easy', 'EASY'), ('Medium', 'MEDIUM'), ('Hard', 'HARD')] # Create selector with 3 difficulty options settings_menu.add.selector('Select difficulty:\t', items, selector_id='difficulty', default=1) settings_menu.add.selector('Select difficulty fancy', items, selector_id='difficulty_fancy', default=1, style='fancy') settings_menu.add.dropselect('Select difficulty (drop)', items, default=1, dropselect_id='difficulty_drop') settings_menu.add.dropselect_multiple( title='Pick 3 colors', items=[('Black', (0, 0, 0)), ('Blue', (0, 0, 255)), ('Cyan', (0, 255, 255)), ('Fuchsia', (255, 0, 255)), ('Green', (0, 255, 0)), ('Red', (255, 0, 0)), ('White', (255, 255, 255)), ('Yellow', (255, 255, 0))], dropselect_multiple_id='pickcolors', max_selected=3, open_middle=True, selection_box_height=6 # How many options show if opened ) # Create switch settings_menu.add.toggle_switch('First Switch', False, toggleswitch_id='first_switch') settings_menu.add.toggle_switch('Other Switch', True, toggleswitch_id='second_switch', state_text=('Apagado', 'Encencido')) def data_fun() -> None: """ Print data of the menu. :return: None """ print('Settings data:') data = settings_menu.get_input_data() for k in data.keys(): print(u'\t{0}\t=>\t{1}'.format(k, data[k])) settings_menu.add.clock(clock_format='%Y/%m/%d %H:%M', title_format='Clock: {0}') settings_menu.add.button('Store data', data_fun) # Call function settings_menu.add.button('Restore original values', settings_menu.reset_value) settings_menu.add.button('Return to main menu', pygame_menu.events.BACK, align=pygame_menu.locals.ALIGN_CENTER) # ------------------------------------------------------------------------- # Create menus: More settings # ------------------------------------------------------------------------- more_settings_menu = pygame_menu.Menu(height=WINDOW_SIZE[1] * 0.85, theme=settings_menu_theme, title='More Settings', width=WINDOW_SIZE[0] * 0.9) more_settings_menu.add.image( pygame_menu.baseimage.IMAGE_EXAMPLE_PYGAME_MENU, scale=(0.25, 0.25), align=pygame_menu.locals.ALIGN_CENTER) more_settings_menu.add.color_input('Color 1 RGB: ', color_type='rgb') more_settings_menu.add.color_input('Color 2 RGB: ', color_type='rgb', default=(255, 0, 0), input_separator='-') def print_color(color: Tuple) -> None: """ Test onchange/onreturn. :param color: Color tuple :return: None """ print('Returned color: ', color) more_settings_menu.add.color_input('Color in Hex: ', color_type='hex', hex_format='lower', onreturn=print_color) more_settings_menu.add.vertical_margin(25) more_settings_menu.add.button('Return to main menu', pygame_menu.events.BACK, align=pygame_menu.locals.ALIGN_CENTER) # ------------------------------------------------------------------------- # Create menus: Column buttons # ------------------------------------------------------------------------- button_column_menu_theme = pygame_menu.themes.THEME_ORANGE.copy() button_column_menu_theme.background_color = pygame_menu.BaseImage( image_path=pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES, drawing_mode=pygame_menu.baseimage.IMAGE_MODE_REPEAT_XY) button_column_menu_theme.widget_font_size = 25 button_column_menu = pygame_menu.Menu(columns=2, height=WINDOW_SIZE[1] * 0.45, rows=3, theme=button_column_menu_theme, title='Textures+Columns', width=WINDOW_SIZE[0] * 0.9) for i in range(4): button_column_menu.add.button('Button {0}'.format(i), pygame_menu.events.BACK) button_column_menu.add.button( 'Return to main menu', pygame_menu.events.BACK, background_color=pygame_menu.BaseImage( image_path=pygame_menu.baseimage.IMAGE_EXAMPLE_METAL) ).background_inflate_to_selection_effect() # ------------------------------------------------------------------------- # Create menus: Main menu # ------------------------------------------------------------------------- main_menu_theme = pygame_menu.themes.THEME_ORANGE.copy() main_menu_theme.title_font = pygame_menu.font.FONT_COMIC_NEUE main_menu_theme.widget_font = pygame_menu.font.FONT_COMIC_NEUE main_menu_theme.widget_font_size = 30 main_menu = pygame_menu.Menu( height=WINDOW_SIZE[1] * 0.7, onclose=pygame_menu.events.EXIT, # User press ESC button theme=main_menu_theme, title='Main menu', width=WINDOW_SIZE[0] * 0.8) main_menu.add.button('Settings', settings_menu) main_menu.add.button('More Settings', more_settings_menu) main_menu.add.button('Menu in textures and columns', button_column_menu) main_menu.add.selector('Menu sounds ', [('Off', False), ('On', True)], onchange=update_menu_sound) main_menu.add.button('Quit', pygame_menu.events.EXIT) # ------------------------------------------------------------------------- # Main loop # ------------------------------------------------------------------------- while True: # Tick clock.tick(FPS) # Paint background main_background() # Main menu main_menu.mainloop(surface, main_background, disable_loop=test, fps_limit=FPS) # Flip surface pygame.display.flip() # At first loop returns if test: break
def test_get(self) -> None: """ Test custom types get. """ t = pygame_menu.themes.THEME_DEFAULT img = pygame_menu.BaseImage( pygame_menu.baseimage.IMAGE_EXAMPLE_GRAY_LINES) class Test(object): """ Class to test. """ pass def dummy() -> bool: """ Callable. """ return True # Test correct t._get({}, '', 'tuple2', (1, 1)) t._get({}, '', str, 'hi') t._get({}, '', int, 4) t._get({}, '', bool, True) t._get({}, '', 'color', (1, 1, 1)) t._get({}, '', 'color', [11, 1, 0]) t._get({}, '', 'color', [11, 1, 0, 55]) t._get({}, '', 'color_none', (1, 1, 1)) t._get({}, '', 'color_none', [11, 1, 0]) t._get({}, '', 'color_none', [11, 1, 0, 55]) t._get({}, '', 'color_none', None) t._get({}, '', Test, Test()) t._get({}, '', callable, dummy) t._get({}, '', 'callable', dummy) t._get({}, '', 'callable', dummy) t._get({}, '', 'image', img) t._get({}, '', 'tuple2', (1, -1)) t._get({}, '', 'tuple2', [1, -1]) t._get({}, '', 'tuple3', [1, -1, 1]) t._get({}, '', 'tuple2int', (1.0, -1)) t._get({}, '', 'tuple2int', (0.000000000, 0.0)) t._get({}, '', 'tuple2int', [1, -1]) t._get({}, '', 'tuple3int', [1, -1.0, 1]) t._get({}, '', 'color_image', (1, 1, 1)) t._get({}, '', 'color_image', [11, 1, 0]) t._get({}, '', 'color_image', [11, 1, 0, 55]) t._get({}, '', 'color_image', img) t._get({}, '', 'alignment', pygame_menu.locals.ALIGN_LEFT) t._get({}, '', 'position', pygame_menu.locals.POSITION_SOUTHWEST) t._get({}, '', 'none', None) t._get({}, '', 'type', bool) t._get({}, '', 'color_image_none', [11, 1, 0, 55]) t._get({}, '', 'color_image_none', None) t._get({}, '', 'color_image_none', img) t._get({}, '', 'cursor', None) t._get({}, '', 'cursor', 1) t._get({}, '', 'font', 'font') t._get({}, '', 'font', Path('.')) self.assertTrue(t._get({}, '', 'callable', dummy)()) # Test raises self.assertRaises(AssertionError, lambda: t._get({}, '', 'type', 'bool')) self.assertRaises(AssertionError, lambda: t._get({}, '', 'none', 1)) self.assertRaises(AssertionError, lambda: t._get({}, '', 'none', bool)) self.assertRaises(AssertionError, lambda: t._get({}, '', 'image', bool)) self.assertRaises(AssertionError, lambda: t._get({}, '', 'callable', bool)) self.assertRaises(AssertionError, lambda: t._get({}, '', 'unknown', Test())) self.assertRaises(AssertionError, lambda: t._get({}, '', callable, Test())) self.assertRaises(AssertionError, lambda: t._get({}, '', 'color', None)) self.assertRaises(AssertionError, lambda: t._get({}, '', 'color', [11, 1, -1])) self.assertRaises(AssertionError, lambda: t._get({}, '', 'color', [11, 1, -1])) self.assertRaises(AssertionError, lambda: t._get({}, '', 'color', [11, 1])) self.assertRaises(AssertionError, lambda: t._get({}, '', 'color', [1, 1, 1, 256])) self.assertRaises(AssertionError, lambda: t._get({}, '', int, 4.4)) self.assertRaises(AssertionError, lambda: t._get({}, '', bool, 4.4)) self.assertRaises(AssertionError, lambda: t._get({}, '', 'tuple2.1', (1, 1))) self.assertRaises(AssertionError, lambda: t._get({}, '', 'tuple2', (1, 1, 1))) self.assertRaises(AssertionError, lambda: t._get({}, '', 'tuple2int', (1.5, 1))) self.assertRaises(AssertionError, lambda: t._get({}, '', 'tuple3', (1, 1, 1, 1))) self.assertRaises( AssertionError, lambda: t._get({}, '', 'tuple3int', (1, 1, 1.000001))) self.assertRaises(AssertionError, lambda: t._get({}, '', 'cursor', 'hi')) self.assertRaises(AssertionError, lambda: t._get({}, '', 'font', 1))