I am not getting an option to close the window in pygame

Question:

So, this is a simple code written to make the window and the background image. I need help with 2 things. 1. I am not getting the option to close, minimize or maximize the window, the entire thing is covered with my background image. 2. I need help in not making my image so bloated, or stretched. It is quite high-res but I believe I have made it stretched.

Code is below:

import pygame as p
import time
import random
import sys

# Defining global variables

HEIGHT = 800
WIDTH = 1000

# Adding a background image
BACKGROUND = p.image.load("background_image.jpg")


def draw():
    WINDOW.blit(BACKGROUND, (0, 0))
    p.display.update()


# Making the window
WINDOW = p.display.set_mode((WIDTH, HEIGHT))
p.display.set_caption("Space Dodge")


# Window will quit after pressing the X
def main():
    run = True
    while run:
        for event in p.event.get():
            if event.type == p.QUIT:
                run = False
                break

        WINDOW.fill((0, 0, 0))
        draw()

    p.quit()
    sys.exit()


if __name__ == "__main__":
    main()
Asked By: Mustafa Anandwala

||

Answers:

Ther is a problem with your height of the screen.
The Option are available but it is not visible in you screen.

Solution : Reduce the HEIGHT and run the code Sure It will work.

Answered By: MUKILAN S

To solve the issue with the window not allowing you to close, minimize or maximize it, you need to add a p.display.flip() function call after you update the window with p.display.update() in the draw() function. The p.display.flip() function will update the entire screen so that it displays the modifications made to the window, and also allow you to access the window’s controls.

To fix the background image’s stretching, you can resize the image to fit the window’s size appropriately. You can do this by using the p.transform.scale() method to scale the BACKGROUND image to match the WINDOW size like this:

BACKGROUND = p.image.load("background_image.jpg")

BACKGROUND = p.transform.scale(BACKGROUND, (WIDTH, HEIGHT))

The resized BACKGROUND image will now fit the WINDOW size without stretching it.

Here is the updated code with the changes mentioned above:

import pygame as p
import time
import random
import sys

# Defining global variables

HEIGHT = 800
WIDTH = 1000

# Adding a background image
BACKGROUND = p.image.load("background_image.jpg")
BACKGROUND = p.transform.scale(BACKGROUND, (WIDTH, HEIGHT))


def draw():
    WINDOW.blit(BACKGROUND, (0, 0))
    p.display.update()
    p.display.flip()  # Added flip function call to update the screen


# Making the window
WINDOW = p.display.set_mode((WIDTH, HEIGHT))
p.display.set_caption("Space Dodge")


# Window will quit after pressing the X
def main():
    run = True
    while run:
        for event in p.event.get():
            if event.type == p.QUIT:
                run = False

            if event.type == p.KEYDOWN:
                if event.key == p.K_ESCAPE:
                    run = False

        WINDOW.fill((0, 0, 0))
        draw()
        
        p.display.update()

    p.quit()
    sys.exit()


if __name__ == "__main__":
    main()
Answered By: Dylan P.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.