Pygame Display Position While Running - python

I am making a pygame project, and have an issue where the window sets itself in the corner of the screen when I back out of full screen mode. Normally, this wouldn't be an issue but it hides the toolbar off screen, making it impossible to drag the screen around or resize it. I have found pygame.display.toggle_fullscreen() to be far to unreliable and breaks whenever I use it, so I made my own method of toggling fullscreen:
import pygame, tkinter
fullscr = False
scrw, srch = tkinter.Tk().winfo_screenwidth(), tkinter.Tk().winfo_screenheight()
while True:
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_F11:
if fullscr:
screen = pygame.display.set_mode((scrw, scrh), pygame.RESIZABLE)
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((980, 720), pygame.RESIZABLE)
fullscr = not fullscr
For those curious, I set the window to fill the screen before setting it to fullscreen because the window will maintain its aspect ratio, breaking my game and causing weird glitches. I am already aware of os.environ['SDL_VIDEO_CENTERED'] = '1' as a way to center the screen, but this does not work after running pygame.init(). Are there any other ways I can change the windows position, or at least prevent it from hiding the toolbar off screen when toggling out of fullscreen?

This is a pygame 2 bug.
It's reported on github here: https://github.com/pygame/pygame/issues/2360,
and the author there realized that you can get around this for now by calling pygame.quit() and then reinitializing to have the SDL_VIDEO_CENTERED work.
Hopefully this will be fixed in pygame 2.0.2. I've written a patch for it at https://github.com/pygame/pygame/pull/2460

Related

Pygame switching from fullscreen to normal does not work

im currently trying to have a setting to switch from windowed mode and fullscreen. But after getting into fullscreen and trying to go back, the game bugs really weird and sticks to the topleft corner
Btw:
display_width = 1280
display_height = 720
elif (Settings_Menu == True):
screen.fill((0,0,0))
screen.blit(settingsscreen, (0,0))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if checkbox1.collidepoint(event.pos):
if(Fullscreen == False):
Fullscreen = True
screen = pygame.display.set_mode((display_width, display_height), pygame.FULLSCREEN)
else:
Fullscreen = False
screen = pygame.display.set_mode((display_width, display_height), pygame.RESIZABLE)
This is a known bug in pygame 2.0 and 2.0.1. Reported on github here
The author there also found a workaround where you quit pygame between switching fullscreen.
Unfortunately, the toggle_fullscreen documentation is wrong. It does not work on windows. Reported on github
Now you might think- pygame has way too many problems with fullscreen, which is reasonable. But they are being fixed. I submitted a patch for your issue which was merged a week ago and will be included in pygame 2.0.2.
Use pygame.display.toggle_fullscreen() to switch between fullscreen and windowed displays.
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if checkbox1.collidepoint(event.pos):
pygame.display.toggle_fullscreen()
The documentation documentation mentions that display driver support "is not great" when using Pygame 1, but it should work for the following display drivers in Pygame 2:
windows (Windows)
x11 (Linux/Unix)
wayland (Linux/Unix)
cocoa (OSX/Mac)
However, at the time of writing this answer, there is a bug in toggle_fullscreen for Windows:
display.toggle_fullscreen does not work when toggling a maximized window to fullscreen #2380
i had this problem and fixed it with:
pygame.display.set_mode(size,pygame.SCALED)
and then i wrote pygame.display.toggle_fullscreen() and worked.

pygame - Fullscreen game graphics shift when re-entering the game window [duplicate]

im currently trying to have a setting to switch from windowed mode and fullscreen. But after getting into fullscreen and trying to go back, the game bugs really weird and sticks to the topleft corner
Btw:
display_width = 1280
display_height = 720
elif (Settings_Menu == True):
screen.fill((0,0,0))
screen.blit(settingsscreen, (0,0))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if checkbox1.collidepoint(event.pos):
if(Fullscreen == False):
Fullscreen = True
screen = pygame.display.set_mode((display_width, display_height), pygame.FULLSCREEN)
else:
Fullscreen = False
screen = pygame.display.set_mode((display_width, display_height), pygame.RESIZABLE)
This is a known bug in pygame 2.0 and 2.0.1. Reported on github here
The author there also found a workaround where you quit pygame between switching fullscreen.
Unfortunately, the toggle_fullscreen documentation is wrong. It does not work on windows. Reported on github
Now you might think- pygame has way too many problems with fullscreen, which is reasonable. But they are being fixed. I submitted a patch for your issue which was merged a week ago and will be included in pygame 2.0.2.
Use pygame.display.toggle_fullscreen() to switch between fullscreen and windowed displays.
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if checkbox1.collidepoint(event.pos):
pygame.display.toggle_fullscreen()
The documentation documentation mentions that display driver support "is not great" when using Pygame 1, but it should work for the following display drivers in Pygame 2:
windows (Windows)
x11 (Linux/Unix)
wayland (Linux/Unix)
cocoa (OSX/Mac)
However, at the time of writing this answer, there is a bug in toggle_fullscreen for Windows:
display.toggle_fullscreen does not work when toggling a maximized window to fullscreen #2380
i had this problem and fixed it with:
pygame.display.set_mode(size,pygame.SCALED)
and then i wrote pygame.display.toggle_fullscreen() and worked.

Force a fullscreen resolution that is not contained in display.list_modes()

I'm making a simulation for school and I'm trying to make pygame create a fullscreen display in my native resolution. However, I have a QHD screen (2560x1440), and it isn't working properly. As far as I can tell, pygame is rendering a screen at the correct resolution, but expanding it so it is scaled as if it were 1080p, so about 300-400 pixels are cut off around the edges. This causes, for example, a circle rendered at (200,200) to be completely invisible. After some research, I learned that this is because pygame doesn't officially support my resolution (it is not listed in pygame.display.list_modes()). Is there any way to force it to work? I would prefer if I could use my actual resolution instead of upscaled 1080p.
Here is the code that initializes the window:
import pygame
from pygame.locals import *
pygame.init()
w = pygame.display.Info().current_w
h = pygame.display.Info().current_h
S = pygame.display.set_mode((w,h), pygame.FULLSCREEN)
If you are using Windows, make sure in your display settings you have scaling set to 100%. This will make your text and everything smaller if you don't have it at that currently but I think Pygame windows get affected by this number for some reason.
See the below code snippet for making sure your window scales properly. Also see here.
import pygame
from ctypes import windll
def run():
# Force windows to ignore the display scaling value.
windll.user32.SetProcessDPIAware()
pygame.display.init()
# Make the screen the highest resolution that will fit the screen at 100% scaling.
screen = pygame.display.set_mode(pygame.display.list_modes()[0])
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
run()

PyGame any touchscreen

I am trying to create a simple program in which a user can move a shape on the screen using his finger (on a touchscreen).
this is my code so far:
import pygame
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((1000,500),0,32)
WHITE = (255,255,255)
blue = (0,0,255)
DISPLAY.fill(WHITE)
pygame.mouse.set_visible(False)
pygame.draw.rect(DISPLAY, blue,(480,200,50,250))
pygame.display.update()
pygame.mouse.set_pos(480, 200)
while True:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
pygame.draw.rect(DISPLAY, blue, (pos[0]-25,pos[1], 50, 250))
pygame.display. update()
DISPLAY.fill(WHITE)
main()
The problem is that when I touch the screen, nothing will happen until I move my finger. If I print the events I can see that there is no event listed until I start moving my finger, so that is probably the reason.
I want to be able to register the finger press (as an event I guess) on the screen even before it starts to move, is there anyway to do this using PyGame?
Thanks.
In case anyone is stumbling across this later, in pygame 2 there is much better touch support. You can install with
pip install pygame==2.0.0.dev6
(check the pygame github for the most recent version) And there are three new event types. pygame.FINGERDOWN, pygame.FINGERUP, and pygame.FINGERMOTION. The pygame.FINGERUP event registers the touch input right when the screen is touched instead of when the screen is released like pygame.MOUSEBUTTONDOWN
I have made a game that utilises a computer touch screen and manages the touch quite well by handling two types of events. One is pygame.MOUSEMOTION and the other one is pygame.MOUSEBUTTONDOWN. Both of them contain the attribute event.pos. Problem is, at least when it comes to my touch screen (Lenovo), that the initial touch is not registered by the event handler of pygame.
You can confirm this by printing all the events to the terminal while touching your screen. I get nothing until I release or move my finger but on release I get the event pygame.MOUSEBUTTONDOWN rapidly followed by pygame.MOUSEBUTTONUP.
I know that this doesn't really solve your problem, but it might perhaps help you in some way. Otherwise, check out Kivy. There might be a solution there.
import pygame
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((1000,500),0,32)
WHITE = (255,255,255)
blue = (0,0,255)
DISPLAY.fill(WHITE)
pygame.mouse.set_visible(False)
pygame.draw.rect(DISPLAY, blue,(480,200,50,250))
pygame.display.update()
pygame.mouse.set_pos(480, 200)
while True:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN or event.type == MOUSEMOTION:
pos = event.pos
pygame.draw.rect(DISPLAY, blue, (pos[0]-25,pos[1], 50, 250))
pygame.display. update()
DISPLAY.fill(WHITE)
main()

Window resize example from Pygame wiki not working

I took this tutorial from pygame.org which should show how to resize the window properly (image has to be supplied to it, you can use for instance my gravatar). The image should resize to the window, but this doesn't happen with me. Only one VideoResize event is created as soon as I resize the window even so slightly:
<Event(16-VideoResize {'h': 500, 'w': 501, 'size': (501, 500)})>
No other VideoResize events are created (other things like mouse movement or keypresses work). So is the tutorial wrong? Is my computer wrong? What is the proper way of doing it?
I'm running: Python 2.7.5, Pygame 1.9.1, Fedora 20, MATE 1.8.1, Toshiba Satellite.
Here's the code (slightly modified to print the event, but neither the original nor this one work):
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,500), RESIZABLE)
pic = pygame.image.load("example.png")
screen.blit(pygame.transform.scale(pic, (500,500)), (0,0))
pygame.display.flip()
done = False
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == VIDEORESIZE:
print event # show all VIDEORESIZE events
screen = pygame.display.set_mode(event.dict['size'], RESIZABLE) # A
screen.blit(pygame.transform.scale(pic, event.dict['size']), (0,0))
pygame.display.flip()
pygame.display.quit()
If I comment the line # A, then I get plenty of events, but this is the line which resizes the window.
Well I ran the tutorial example with only one change which is I used a pic of a cat called cat.png and it worked fine. The resize is working you just grab a corner and it allows me to adjust it freely with dragging. The picture fills the window whatever size I make it. Have you done other scripts with pygame successfully?

Categories