pygame.FULLSCREEN makes the display disappear - python

I was trying to do my project in full screen and i used full screen but sometimes the display comes for 1 second then it disappears and i tried it with no full screen and it works but it doesn't in full screen and my code is:
import pygame
from pygame.locals import*
pygame.init() screen=pygame.display.set_mode((1360,768),pygame.FULLSCREEN,pygame.RESIZABLE)
pygame.display.init()

It looks like you're having trouble because you pass pygame.RESIZABLE as the third argument (the depth) to pygame.display.set_mode whereas you should combine it with the pygame.FULLSCREEN flag with a bitwise OR.
screen = pygame.display.set_mode((1360,768), pygame.FULLSCREEN|pygame.RESIZABLE)

Related

How come I can't give my pygame window a name or an icon? [duplicate]

This question already has an answer here:
Icons are not displayed properly with pygame
(1 answer)
Closed 1 year ago.
A screenshot of me running my code is below
and here is my actual code:
import 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-flying.png')
pygame.display.set_icon(icon)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I don't understand why it's not working. My code looks right, my icons in my directory, but nothing is coming up. Is it something I forgot to download? Is my code wrong?
This is the tutorial I'm following: https://www.youtube.com/watch?v=_HF852IONl8
One last thing that might be important to know is that even when I run my window without trying to change the title and icon, it's blank. This leads me to believe that there's a package I still haven't downloaded but I really don't know.
First of all change the pixel size of this image 'ufo-flying.png' to 32 x 32. To do this open this image with any photo editor app on pc. Choose the "resize" option. Now change the width and height to 32 x 32. You may use this site to change the pixels of this image. https://www.reduceimages.com/ Now save this image to the folder which contains this python code file(main.py). If you are using Pycharm then you can find this folder in the "PycharmProjects" folder on your pc. After doing this, run your code and it will work fine. Cheers!
See pygame.display.set_icon():
[...] Some systems do not allow the window icon to change after it has been shown. This function can be called before pygame.display.set_mode() to create the icon before the display mode is set.
Set the icon before screen = pygame.display.set_mode((800, 600)):
icon = pygame.image.load('ufo-flying.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((800, 600))
In addition, the size of the icon is limited:
[...] You can pass any surface, but most systems want a smaller image around 32x32.
If the icon is not displayed, try a smaller icon.

No Video Mode Has Been Set Error, cannot load in images [duplicate]

I am just setting up some functions for my game but my script fails to load the image
#used variables
# x, y for alien location
# nPc for the aliens image
#
#
#
#
#
#
#
#
#
#
#set up
import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()
nPc = '/home/claude/Dropbox/Bowtie/Prisim/Images/Alien_Races/Standered/alien_1.png'
nPc = pygame.image.load(nPc).convert_alpha()
def loc_alien():
x = random.randint(0, 400)
y = randaom.randint(0, 400)
def spawn_alien(x, y):
screen.blit(nPc, (x, y))
when I run this I wont nothing to happen as I am not using the functions yet, but when I do run it I get this error
Traceback (most recent call last):
File "/home/claude/Dropbox/Bowtie/Prisim/Scripts/aliens.py", line 26, in <module>
nPc = pygame.image.load(nPc).convert_alpha()
error: No video mode has been set
anyone know what I'm doing wrong?
I believe that you need to call:
screen = pygame.display.set_mode((800, 600)) # change to the real resolution
this call will actually return the surface that you want to blit on. Below is the documentation from the linked resource.
pygame.display.set_mode()
Initialize a window or screen for display
set_mode(resolution=(0,0), flags=0, depth=0) -> Surface
This function will create a display Surface. The arguments passed in
are requests for a display type. The actual created display will be
the best possible match supported by the system.
The resolution argument is a pair of numbers representing the width
and height. The flags argument is a collection of additional options.
The depth argument represents the number of bits to use for color.
The Surface that gets returned can be drawn to like a regular Surface
but changes will eventually be seen on the monitor.
If no resolution is passed or is set to (0, 0) and pygame uses SDL
version 1.2.10 or above, the created Surface will have the same size
as the current screen resolution. If only the width or height are set
to 0, the Surface will have the same width or height as the screen
resolution. Using a SDL version prior to 1.2.10 will raise an
exception.
It is usually best to not pass the depth argument. It will default to
the best and fastest color depth for the system. If your game requires
a specific color format you can control the depth with this argument.
Pygame will emulate an unavailable color depth which can be slow.
When requesting fullscreen display modes, sometimes an exact match for
the requested resolution cannot be made. In these situations pygame
will select the closest compatible match. The returned surface will
still always match the requested resolution.
The flags argument controls which type of display you want. There are
several to choose from, and you can even combine multiple types using
the bitwise or operator, (the pipe “|” character). If you pass 0 or no
flags argument it will default to a software driven window. Here are
the display flags you will want to choose from:
pygame.FULLSCREEN create a fullscreen display
pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
pygame.OPENGL create an OpenGL renderable display
pygame.RESIZABLE display window should be sizeable
pygame.NOFRAME display window will have no border or controls
For example:
# Open a window on the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])
Do not forgot to create your surface before creating variables with images in it
like this:
win = pygame.display.set_mode((576, 1024))
background_day = pygame.image.load("background-day.png").convert()
not like this:
background_day = pygame.image.load("background-day.png").convert()
win = pygame.display.set_mode((576, 1024))

pygame with FULLSCREEN flag renders content scaled

I'm using pygame on a raspberry pi.
This same code used to cover the full screen at 800x600, now, at 1280x720 it doesn't, and it's not over/underscan:
Pygame example output:
Video playback via omxplayer:
All the code in the pygame example image is just a demo for the problem:
import pygame
import time
pygame.display.init()
pygame.font.init()
screen = pygame.display.set_mode((1280, 720)) #, pygame.FULLSCREEN)
screen.fill((255, 0, 0))
pygame.display.flip()
time.sleep(45)
You can use the list_modes function which returns a list of available full screen resolutions:
modes = pygame.display.list_modes()
if modes: # check if the list is not empty
screen = pygame.display.set_mode(modes[0], pygame.FULLSCREEN) # use the first one
else:
screen = pygame.display.set_mode((800, 600)) # use a default resolution
Note: if you have problems with high DPI scaling on Windows (e. g. a part of your display is not visible), you can use this code to fix them:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(1)

python error, "No video mode has been set" although it is set [duplicate]

I am just setting up some functions for my game but my script fails to load the image
#used variables
# x, y for alien location
# nPc for the aliens image
#
#
#
#
#
#
#
#
#
#
#set up
import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()
nPc = '/home/claude/Dropbox/Bowtie/Prisim/Images/Alien_Races/Standered/alien_1.png'
nPc = pygame.image.load(nPc).convert_alpha()
def loc_alien():
x = random.randint(0, 400)
y = randaom.randint(0, 400)
def spawn_alien(x, y):
screen.blit(nPc, (x, y))
when I run this I wont nothing to happen as I am not using the functions yet, but when I do run it I get this error
Traceback (most recent call last):
File "/home/claude/Dropbox/Bowtie/Prisim/Scripts/aliens.py", line 26, in <module>
nPc = pygame.image.load(nPc).convert_alpha()
error: No video mode has been set
anyone know what I'm doing wrong?
I believe that you need to call:
screen = pygame.display.set_mode((800, 600)) # change to the real resolution
this call will actually return the surface that you want to blit on. Below is the documentation from the linked resource.
pygame.display.set_mode()
Initialize a window or screen for display
set_mode(resolution=(0,0), flags=0, depth=0) -> Surface
This function will create a display Surface. The arguments passed in
are requests for a display type. The actual created display will be
the best possible match supported by the system.
The resolution argument is a pair of numbers representing the width
and height. The flags argument is a collection of additional options.
The depth argument represents the number of bits to use for color.
The Surface that gets returned can be drawn to like a regular Surface
but changes will eventually be seen on the monitor.
If no resolution is passed or is set to (0, 0) and pygame uses SDL
version 1.2.10 or above, the created Surface will have the same size
as the current screen resolution. If only the width or height are set
to 0, the Surface will have the same width or height as the screen
resolution. Using a SDL version prior to 1.2.10 will raise an
exception.
It is usually best to not pass the depth argument. It will default to
the best and fastest color depth for the system. If your game requires
a specific color format you can control the depth with this argument.
Pygame will emulate an unavailable color depth which can be slow.
When requesting fullscreen display modes, sometimes an exact match for
the requested resolution cannot be made. In these situations pygame
will select the closest compatible match. The returned surface will
still always match the requested resolution.
The flags argument controls which type of display you want. There are
several to choose from, and you can even combine multiple types using
the bitwise or operator, (the pipe “|” character). If you pass 0 or no
flags argument it will default to a software driven window. Here are
the display flags you will want to choose from:
pygame.FULLSCREEN create a fullscreen display
pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
pygame.OPENGL create an OpenGL renderable display
pygame.RESIZABLE display window should be sizeable
pygame.NOFRAME display window will have no border or controls
For example:
# Open a window on the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])
Do not forgot to create your surface before creating variables with images in it
like this:
win = pygame.display.set_mode((576, 1024))
background_day = pygame.image.load("background-day.png").convert()
not like this:
background_day = pygame.image.load("background-day.png").convert()
win = pygame.display.set_mode((576, 1024))

pygame dual monitors and fullscreen

I am using pygame to program a simple behavioral test. I'm running it on my macbook pro and have almost all the functionality working. However, during testing I'll have a second, external monitor that the subject sees and the laptop monitor. I'd like to have the game so up fullscreen on the external monitor and not on the laptop's monitor so that I can monitor performance. Currently, the start of the file looks something like:
#! /usr/bin/env python2.6
import pygame
import sys
stdscr = curses.initscr()
pygame.init()
screen = pygame.display.set_mode((1900, 1100), pygame.RESIZABLE)
I was thinking of starting the game in a resizable screen, but that OS X has problems resizing the window.
Pygame doesn't support two displays in a single pygame process(yet). See the question here and developer answer immediately after, where he says
Once SDL 1.3 is finished then pygame will get support for using multiple windows in the same process.
So, your options are:
Use multiple processes. Two pygame instances, each maximized on its own screen, communicating back and forth (you could use any of: the very cool python multiprocessing module, local TCP, pipes, writing/reading files, etc)
Set the same resolution on both of your displays, and create a large (wide) window that spans them with your information on one half and the user display on the other. Then manually place the window so that the user side is on their screen and yours is on the laptop screen. It's hacky, but might a better use of your time than engineering a better solution ("If it's studpid and it works, it ain't stupid" ;).
Use pyglet, which is similar to pygame and supports full screen windows: pyglet.window.Window(fullscreen=True, screens[1])
Good luck.
I do not know if you can do this in OS X, but this is worth mentioning for the Windows users out there, if you just want to have your program to run full screen on the second screen and you are on windows, just set the other screen as the main one.
The setting can be found under Rearrange Your Displays in settings.
So far for me anything that I can run on my main display can run this way, no need to change your code.
I did something silly but it works.
i get the number of monitors with get_monitors()
than i use SDL to change the pygame window's display position by adding to it the width of the smallest screen, to be sure that the window will be positionned in the second monitor.
from screeninfo import get_monitors
numberOfmonitors = 0
smallScreenWidth = 9999
for monitor in get_monitors():
#getting the smallest screen width
smallScreenWidth = min(smallScreenWidth, monitor.width)
numberOfmonitors += 1
if numberOfmonitors > 1:
x = smallScreenWidth
y = 0
#this will position the pygame window in the second monitor
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
#you can check with a small window
#screen = pygame.display.set_mode((100,100))
#or go full screen in second monitor
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
#if you want to do other tasks on the laptop (first monitor) while the pygame window is being displayed on the second monitor, you shoudn't use fullscreen but instead get the second monitor's width and heigh using monitor.width and monitor.height, and set the display mode like
screen = pygame.display.set_mode((width,height))
display = pyglet.canvas.get_display()
display = display.get_screens()
win = pyglet.window.Window(screen=display[1])
------------------------------------------------------
screen=display[Номер монитора]
------------------------------------------------------
display = pyglet.canvas.get_display()
display = display.get_screens()
print(display) # Все мониторы которые есть

Categories

Resources