Pygame exiting fullscreen mode sets window outside screen - python

I have a problem when going from fullscreen mode to a smaller screen in Pygame. The window appears on the top left and I can't see any exit button, nor can I drag it to the center. Here is the code I am using:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
run = True
while run:
for event in pygame.event.get():
if event == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
# Exit fullscreen with escape key
if event.key == pygame.K_ESCAPE:
if screen.get_flags() & FULLSCREEN:
pygame.display.set_mode((400, 400))
else:
pygame.display.set_mode((0, 0), FULLSCREEN)
I have tried to center the smaller window on the screen which usually works fine, but not in this case.
import os
# .........
if screen.get_flags() & FULLSCREEN:
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.display.set_mode((400, 400))
else:
pygame.display.set_mode((0, 0), FULLSCREEN)
Any ideas how to center the window after exiting fullscreen?

This is a bug in pygame.
If you need this behavior, someone who reported the issue on github found a workaround (https://github.com/pygame/pygame/issues/2360)
Hopefully it'll be fixed in 2.0.2, I've written a patch to fix it (https://github.com/pygame/pygame/pull/2460)

Related

Pygame always showing a black screen

I am trying to recreate this pygame but i keep getting a black screen without anything displayed. I get no error's so i don't know where i need to start.
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption("Runner")
clock = pygame.time.Clock()
test_surface = pygame.image.load('graphics/Sky.png')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(test_surface,(0, 0))
pygame.display.update()
clock.tick(60)
The problem got solved after reinstalling python and pycharm. Not sure if both were needed but it did the trick for me, thanks all.

Pygame screen dosen't close

I'm a Macbook user and I have just recently started learning programming games using python.
my issue is that every time I try to close the screen it doesn't close.
import pygame
import sys
pygame.init()
width = 800
hight = 600
screen = pygame.display.set_mode((width , hight))
game_over= False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT :
sys.exit()
When I point the cursor to the screen it turns to a circle... Cursur, so usually I just press force to quit "on the snake icone" in the dock
So how can I fix this and make the screen close just by pressing the close button ?
I'm using python 3.
Thank you.
Full edited and tested code :
import pygame
import sys
pygame.init()
width = 800
hight = 600
running = True
screen = pygame.display.set_mode((width , hight))
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT :
pygame.quit()
sys.exit()
Test on Linux mint with visual studio code. Tested more than 5 times. Expected results.

Pygame game crashes when trying to close the app

So im just following a simple Pygame tutorial on youtube and it should work but for some reason every time i try to close the app it crashes and doesn't close normally, here's the code:
import pygame
# Initialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((800, 600))
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# Run game until x is pressed
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
im using the latest version of pycharm and python.
Add a pygame.display.quit() at the last line, you need to tell it to close the window.

Python-pygame KEYDOWN not working in macbook

I ran the code on my Macbook,it didn't work, while I ran the code on my Ubuntu it worked.
My macOS is 10.12.6
(ps:when I used mouse to control the image in pygame(just input the position of my mouse) ,if i didn't clicked the mouse , i can't dragged the image, but i didn't do anything in my program about click event)
This is my code
import pygame
pygame.init()
size = (800,600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('My Demo')
done = False
BLACK = (0,0,0)
WHITE = (255,255,255)
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
print('work')
screen.fill(WHITE)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Pygame sometimes mixes up the keys. I already have encountered this problem on Windows, it seems to be an issues on macOS too.
This approach might solve your problem: Pygame keyboard layouts mixed up.

Pygame screen freezes when I close it

The code loads up a pygame screen window, but when I click the X to close it, it becomes unresponsive. I'm running on a 64-bit system, using a 32-bit python and 32-bit pygame.
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
games.screen.mainloop()
Mach1723's answer is correct, but I would like to suggest another variant of a main loop:
while 1:
for event in pygame.event.get():
if event.type == QUIT: ## defined in pygame.locals
pygame.quit()
sys.exit()
if event.type == ## Handle other event types here...
## Do other important game loop stuff here.
I'd recommend the following code. First, it includes Clock so your program doesn't eat the CPU doing nothing but polling for events. Second, it calls pygame.quit() which prevents the program from freezing when running under IDLE on windows.
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
pygame.init()
# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
#Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while done==False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Set the screen background
screen.fill(black)
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit ()
This is a pretty simple issue, you need to handle the "QUIT" event, see the event documentation at: http://www.pygame.org/docs/ref/event.html
EDIT:
It occurs to me now that you might be handling the "QUIT" event and its not working
but without more details to your code I dunno.
A quick example of a simple way to handle the "QUIT" event:
import sys
import pygame
# Initialize pygame
pygame.init()
pygame.display.set_mode(resolution=(640, 480))
# Simple(ugly) main loop
curEvent = pygame.event.poll()
while curEvent.type != pygame.QUIT:
# do something
curEvent = pygame.event.poll()
In using pygame, you have to handle all events including QUIT so if you don't handle the quit event, your program will not quit. Here's a code.
import sys
import pygame
from pygame.locals import *
def main():
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT: #QUIT is defined at pygame.locals
runnning = False
#other game stuff to be done
if __name__=='__main__':
pygame.init()
pygame.display.set_mode((640,480))
main()

Categories

Resources