Home > database >  Python: Is there any way to resize a pygame window only once without using pygame.RESIZABLE
Python: Is there any way to resize a pygame window only once without using pygame.RESIZABLE

Time:01-10

I want to be able to create the main menu for my game that's 500x500 and within that main menu loop, I want to run the main game loop with the command 'q'. If 'q' is pressed then the window needs to be resized In order to fit all the assets of the game

code:

import pygame
import sys

win = pygame.display.set_mode((500, 500))

def main_menu():

    running = True
    frame = 30
    clock = pygame.time.Clock()


    while running:
            
        for event in pygame.event.get():
        
            win.fill(220,220,220)
            
            # When the main menu is run, the window should be 500x500
        
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                sys.exit()
        
            if event.type == KEYDOWN:
            
                if event.key == K_q:
                    main()
                    # When main is run, I want the window to be resized to 900, 700
                   

        pygame.display.update()

    clock.tick(frame)
        


 if __name__ == '__main__':
     main_menu()

I originally tried doing this but it kept glitching out the window so I stopped:

if event.key == K_q:
    pygame.display.set_mode((900, 700))
    main()

CodePudding user response:

Hey are you new to pygame? Your code is not properly indended and you have placed many things on wrong place:-

if '__name__ = __main__' had a space

and main() also

also your win.fill(color) needs to be win.fill((color))

and to change screen size you need to do win = pygame.display.setmode((new screen size))

I have fixed all the errors in the code and when you press q screen size changes:-

here's the code:- (I am also new here, and i hope this helped, pls upvote my answer and set this as correct answer)

import pygame
from pygame.locals import *
import sys

win = pygame.display.set_mode((500, 500))

def main_menu():
    running = True
    frame = 30
    clock = pygame.time.Clock()
    while running:
        global win
        win.fill((255,255,255))
        
        for event in pygame.event.get():
        
        # When the main menu is run, the window should be 500x500
    
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
                sys.exit()
    
            if event.type == KEYDOWN:
        
                if event.key == K_q:
                    win = pygame.display.set_mode((900,00))
                    # When main is run, I want the window to be resized to 900, 700
        pygame.display.update()
    clock.tick(frame)
if __name__ == '__main__':
    main_menu()
  •  Tags:  
  • Related