Related Stack Overflow questions:
Related Stack Overflow questions:
- Why is nothing drawn in PyGame at all?
- Why is screen not background surface does not blit onto screen?
- Why does my Pygame window flicker when animating objects?
- How do I draw a line without updating the whole screen in pygame?
- How can I move the ball instead of leaving a trail all over the screen in pygame?
- pygame - surface from part of another surface
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()
.
This will update the contents of the entire display.
Related Stack Overflow questions:
- Why is the PyGame animation is flickering
- How to stop sprites from "blinking" when drawn all at once
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.
- Pygame display.update() refreshes the whole width of window instead of requested Rect only
- How to clean up sprites without covering other things?
Tag: delete object, remove object, erase object, delete image, remove image, erase image
Related Stack Overflow questions:
- how to make image/images disappear in pygame?
- How do I delete rect object from screen once player collides with it?
- How to delete one object from a Surface instance (pygame)?
- How to clean up sprites without covering other things?
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.
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
Related Stack Overflow questions:
- python pygame the background image changes with the screen size
- How to transform current pygame surface to a larger one?
- Resize proportionally to the screen
- How to put limits on resizing a window in pygame
- python pygame use VIDEORESIZE to update the new window but the item in window not update its new position
- How do I make my pygame window minimze and maximize?
- How to make my pygame game window resizeable?
- Get screen orientation with Python
- Update during resize in Pygame
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)
Related Stack Overflow questions:
Related Stack Overflow questions:
- The fullscreen mode in pygame is entirely black
- Switching to Pygame Fullscreen Mode working only one time
- Pygame switching from fullscreen to normal does not work
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))
- pygame.display.toggle_fulscreen() not working
- pygame fullscreen on second monitor
- How to make a pygame window fullscreen without hiding the taskbar
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
Related Stack Overflow questions:
-
How to move a no frame pygame windows when user click on it?
-
Make a pygame resizable window that can be snapped to the screen
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))
Related Stack Overflow questions:
-
How to zoom in and out of an image pygame and using the mousposition as the center of the zoom
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
Related Stack Overflow questions:
📁 Minimal example - Rotate screen
- Assosiate rects to specific window in pygame
- How do I change the Pygame coordinate system so that the center of the window is (0,0)?
- pushMatrix, popMatrix, translate, and rotate in pygame?
- Polygons on created surfaces
- pygame define different position order make the different result
📁 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.
Related Stack Overflow questions:
- How to Change the Name of a Pygame window?
- Icons are not displayed, displayed only after closing for half a second
- Why can't I change the python window icon?
- pygame.display.set_icon(pygame.image obj) doesn't work in python 3.9.1
- how to set a picture as an app icon in pygame in ubuntu
- pygame collision detection causes my computer to hang
See Scale background
Related Stack Overflow questions:
Related Stack Overflow questions: