Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update window_resize.py #481

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 80 additions & 40 deletions pygame_menu/examples/window_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,95 @@
"""

import pygame
import random, sys, math, time

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'math' is not used.
import pygame_menu
from pygame_menu import themes

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'themes' is not used.

pygame.init()
current_time = time.time()
random.seed(current_time)
# Seting up the display
screenWidth, screenHeight = 700, 700
screen = pygame.display.set_mode((screenWidth, screenHeight), pygame.RESIZABLE)
pygame.display.set_caption("Race Game")
clock = pygame.time.Clock()

surface = pygame.display.set_mode((600, 400), pygame.RESIZABLE)
pygame.display.set_caption("Example resizable window")
# color
def random_color():
return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
colors = {
"RED": (255, 0, 0),
"GREEN": (0, 255, 0),
"BLUE": (0, 0, 255),
"WHITE": (255, 255, 255),
"BLACK": (0, 0, 0),
"GRAY": (128, 128, 128),
"DARK_GRAY": (169, 169, 169),
}

menu = pygame_menu.Menu(
height=100,
theme=pygame_menu.themes.THEME_BLUE,
title='Welcome',
width=100
)
# Function checked if the window is resized.
def on_resize() -> None:
window_size = screen.get_size()
new_w, new_h = window_size[0], window_size[1]
menu.main_menu.resize(new_w, new_h)
menu.player_screen.resize(new_w, new_h)
menu.settings_screen.resize(new_w, new_h)

# main menu
class MainMenu:
def __init__(self, screen, width, height):
self.screen = screen
self.width = width
self.height = height
self.create_main_menu()

def create_main_menu(self):
self.main_menu = pygame_menu.Menu('Welcome', self.width, self.height, theme=pygame_menu.themes.THEME_DARK)
self.player_screen = pygame_menu.Menu('Player Screen', self.width, self.height, theme=pygame_menu.themes.THEME_DEFAULT)
self.settings_screen = pygame_menu.Menu('Settings', self.width, self.height, theme=pygame_menu.themes.THEME_DARK)
self.main_menu.add.button('Play', self.game)
self.main_menu.add.button('Settings', self.settings)
self.main_menu.add.button('Quit', pygame_menu.events.EXIT)

def quitMenu(self):
self.main_menu.disable()

def on_resize() -> None:
"""
Function checked if the window is resized.
"""
window_size = surface.get_size()
new_w, new_h = 0.75 * window_size[0], 0.7 * window_size[1]
menu.resize(new_w, new_h)
print(f'New menu size: {menu.get_size()}')


menu.add.label('Resize the window!')
user_name = menu.add.text_input('Name: ', default='John Doe', maxchar=10)
menu.add.selector('Difficulty: ', [('Hard', 1), ('Easy', 2)])
menu.add.button('Quit', pygame_menu.events.EXIT)
menu.enable()
on_resize() # Set initial size

if __name__ == '__main__':
while True:
def game(self):
self.player_screen.add.text_input(title="Country Name: ")
self.player_screen.add.button('Play', self.quitMenu)
self.main_menu._open(self.player_screen)

def settings(self):
self.music_volume = self.settings_screen.add.range_slider('Music Volume', default=50, range_values=(0, 100), increment=1)
self.settings_screen.add.range_slider('Sound Effects', default=50, range_values=(0, 100), increment=1)
self.settings_screen.add.range_slider('Frame Rate', default=60, range_values=(30, 120), increment=1)
self.settings_screen.add.range_slider('Brightness', default=100, range_values=(0, 100), increment=1)
self.settings_screen.add.button('Return to Main Menu', pygame_menu.events.BACK)
self.main_menu._open(self.settings_screen)
menu = MainMenu(screen, screenWidth, screenHeight)
on_resize()

# main
def main():
global screen
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
break
if event.type == pygame.VIDEORESIZE:
# Update the surface
surface = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
# Call the menu event
running = False
sys.exit()
elif event.type == pygame.VIDEORESIZE:
screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
on_resize()

# Draw the menu
surface.fill((25, 0, 50))

menu.update(events)
menu.draw(surface)

screen.fill(colors["WHITE"])
menu.main_menu.update(events)
menu.main_menu.draw(screen)
# This is to update the scene
clock.tick(64)
pygame.display.flip()
pygame.display.update()

# loop
if __name__ == "__main__":
main()
Loading