PyGameExamplesAndAnswers

StackOverflow            reply.it reply.it


Display, display position, resize, coordinate system and scroll

Video driver

Related Stack Overflow questions:

Display update

Related Stack Overflow questions:

Each object in the scene is drawn to the pygame.Surface object associated with the display. To create animated objects, the entire scene must be redrawn in each frame. Therefore, the display must be cleared at the beginning of each frame in the application loop. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visible, when the display is updated with either pygame.display.update() or pygame.display.flip().

See pygame.display.flip():

This will update the contents of the entire display.

Flickering

Related Stack Overflow questions:

One update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.

Updating areas

Delete, remove, replace Object

Tag: delete object, remove object, erase object, delete image, remove image, erase image

Related Stack Overflow questions:

You cannot remove or delete an image or anything else drawn on the display or on any other Surface. The display is simply represented by a Surface object. A Surface contains no object and is simply a collection of pixels arranged in rows and columns. So you can’t “delete” an object from a Surface, but you can simply paint the Surface with a different color. Therefore, the entire scene (including the background) must be redrawn in the application loop, and “delete” means to just not drawing it anymore.
fill or pygame.draw.rect just changes the color of all pixels in an area to a uniform color, including the background of the sprite or image.

Size

Related Stack Overflow questions:

You can get the pygame.Surface object associated with the display with pygame.display.get_surface():

window_surface = pygame.display.get_surface()

Each Surface object can be asked for its with and height with get_width() respectively get_height():

window_width = pygame.display.get_surface().get_width()
window_height = pygame.display.get_surface().get_height()

get_size() returns a tuple with the width and height of the window:

window_width, window_height = pygame.display.get_surface().get_size()

get_rect() returns a rectangle the size of the window, always starting at (0, 0):

This can be used to get the center of the display:

window_center = pygame.display.get_surface().get_rect().center

It can even be used to limit the movement of an object to the window area:

object_rect = pygame.Rect(x, y, w, h)
object_rect.clamp_ip(pygame.display.get_surface().get_rect())

📁 minimal example - Move object with keys limit it to the window borders
run minimal example - Move object with keys limit it to the window borders

How do I get the size (width x height) of my pygame window

Resize and resize event

Related Stack Overflow questions:

When the display option RESIZABLE (see pygame.display.set_mode()) is set, then the VIDEORESIZE event (see pygame.event) occurs when the window is resized. The new size of the window can be get form the even attributes:

width, height = event.w, event.h

Create a new Surface associated to the window and scale the background to the new size:

p.init()
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)

run = True
while run:
    for event in p.event.get():
        if event.type == pygame.QUIT:
            run == False
        elif event.type == pygame.VIDEORESIZE:
            width, height = event.w, event.h
            screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)

📁 Minimal example - Resize

SDL Resize issues on Windows

Related Stack Overflow questions:

Fullscreen

Related Stack Overflow questions:

pygame.display.set_mode creates a pygame.Surface object, which is associated to the window. When pygame.display.set_mode() is called a again, then the object which was associated to the surface before gets invalide.

You’ve to copy() the “old” surface:

is_fullscreen = not is_fullscreen

old_surface = display_surf.copy()

setmode = FULLSCREEN if is_fullscreen else RESIZABLE
display_surf = pygame.display.set_mode(size, setmode)
display_surf.blit(old_surface, (0,0))

Toggle fullscreen and maximize window

See the documentation of pygame.display.toggle_fullscreen():

Switches the display window between windowed and fullscreen modes. This function only works under the UNIX X11 video driver.

A workaround is presented on the Pygame Wiki toggle_fullscreen

Set position

Related Stack Overflow questions:

PyGame is based on Simple DirectMedia Layer (SDL). Hence you can set SDL environment variables.

See pygame wiki - SettingWindowPosition:

You can set the position of the window by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.

x = 100
y = 0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)

import pygame
pygame.init()
screen = pygame.display.set_mode((100,100))

Scale and zoom window

Related Stack Overflow questions:

I’m wondering if there’s a way I can globally scale everything up in my game without either having to scale each sprite up individually […]”

No there is no way. You have to scale each coordinate, each size and each surface individually. PyGame is made for images (Surfaces) and shapes in pixel units. Anyway up scaling an image will result in either a fuzzy, blurred or jagged (Minecraft) appearance.

Is there a way I could make a seperate surface and just put that on top of the base window surface, and just scale that?

Yes of course.

Create a Surface to draw on (win). Use pygame.transform.scale() or pygame.transform.smoothscale() to scale it to the size of the window and blit it to the actual display Surface (display_win):

display_win = pygame.display.set_mode((WINDOW_WIDTH*2, WINDOW_HEIGHT*2))
win = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT))

while running:
    # [...]

    # draw to "win"

    # [...]

    scaled_win = pygame.transform.smoothscale(win, display_win.get_size())
    # or scaled_win = pygame.transform.scale(win, display_win.get_size())
    display_win.blit(scaled_win, (0, 0))
    pygame.display.flip()

📁 Minimal example - Scale up display window

repl.it/@Rabbid76/PyGame-ZoomInAndOut

Coordinate system and transformations

Related Stack Overflow questions:

📁 Minimal example - Rotate screen

📁 Minimal example - Center the origin of the coordinate system

Be aware that the y-axis needs to be reversed (-y respectively y1-y2) because the y-axis is generally pointing up but in the PyGame coordinate system the y-axis is pointing down.

Window caption and icon

Related Stack Overflow questions:

Scale background

See Scale background

Scroll background

See Scroll background

Message box

Related Stack Overflow questions:

Multiple Windows

Related Stack Overflow questions: