Remove black border in pygame fullscreen - python

I am creating a game using pygame 1.9.6. But, as you can see by running this simple example, some black borders appear around the window, even with the FULLSCREEN flag.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((900, 500), FULLSCREEN)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill((255, 255, 255))
pygame.display.flip()
I tried to add the flag NOFRAME in the screen initialisation, but it didn't work.
I wonder if it is possible to remove the border, for example by increasing the screen size to just fit in the current screen. But I also want to keep the 900x500 resolution.
Can pygame resize the entire screen? Do I have to blit() everything on a Surface, then rescale it and draw it on the actual screen?

Thanks #Glenn Mackintosh!
The best way to remove the black border is actually to add the SCALED flag in the screen declaration like this:
screen = pygame.display.set_mode((900, 500), FULLSCREEN|SCALED)
But to use it, the pygame version 2.0.0 or more is required.

The only way to remove the black bars is to set the pygame.display.set_mode((width, height)) to the users resolution. Use pygame.display.Info() to get the users' display resolution. Furthermore, you can have a preferred resolution such as 900 x 500 to get a "differential number" to get a retro/pixelated look.
resolution = pygame.display.Info()
width = resolution.current_w
height = resolution.current_h
dx = width // 900
dy = height // 500
This dx and dy can then be used to scale everything to a bigger size.
I would try this code:
import pygame
from pygame.locals import *
pygame.init() # You forgot the initialize the pygame window!
resolution = pygame.display.Info() # Get the users resolution
width = resolution.current_w
height = resolution.current_h
screen = pygame.display.set_mode((width, height), FULLSCREEN, 0, 32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill((255, 255, 255))
mouse_pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, (0, 0, 255), mouse_pos, 20)
pygame.draw.circle(screen, (0, 0, 200), mouse_pos, 18)
pygame.draw.circle(screen, (0, 0, 100), mouse_pos, 15)
pygame.draw.circle(screen, (0, 0, 50), mouse_pos, 10)
pygame.display.flip()

Related

How to get the X size of the screen in pygame? [duplicate]

I have created a window using
pygame.display.set_mode((width, height),
pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)
Later on in the app I want to be able to ask that window its width and height so I can decide how to process the mouse position.
How do I access the dimensions of that pygame window elsewhere in the program? (event processing loop)
You want to get the surface, then get the size from the surface object.
Using tuple unpacking:
w, h = pygame.display.get_surface().get_size()
You can get the windows size by first getting the display surface. pygame.display.get_surface(), then calling get_width() / get_height() on the surface.
Like this!
surface = pygame.display.get_surface() #get the surface of the current active display
x,y = size = surface.get_width(), surface.get_height()#create an array of surface.width and surface.height
import pygame as pg
def create_window(width, height):
"""create a width x height resizable window/frame"""
win = pg.display.set_mode((width, height), pg.RESIZABLE)
# optional fill bg red, default is black
win.fill((255, 0, 0))
# optional title info
sf = "size x=%s y=%s" % (width, height)
pg.display.set_caption(sf)
# any extra code here ...
# draw a white circle
white = (255, 255, 255)
# center can be fixed or calculated for resize
center = (150, 150)
radius = 100
pg.draw.circle(win, white, center, radius)
pg.display.flip()
return win
pg.init()
width = 300
height = 200
win = create_window(width, height)
# event loop and exit conditions (windows titlebar x click)
while True:
for event in pg.event.get():
if event.type == pg.VIDEORESIZE:
width, height = event.size
# redraw win in new size
win = create_window(width, height)
print(win.get_size()) # test
if event.type == pg.QUIT:
raise SystemExit
Try this out.
Hope it helps.
Adding another answer since I haven't seen this one yet.
screen = pygame.display.set_mode() # Default to screen resolution.
area = screen.get_rect()
We can call get_rect() on screen because it is a surface object. area will be a Rect object, with (0, 0, width, height) information in it.
changed = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if event.type == pygame.VIDEORESIZE:
scrsize = event.size # or event.w, event.h
screen = pygame.display.set_mode(scrsize,RESIZABLE)
changed = True
it's almost exactly like this in the comments for pygame.display.init doc page https://www.pygame.org/docs/ref/display.html#comment_pygame_display_update
edited only slightly from 2011-01-25T23:10:35 - Dave Burton thanks dave
gameDisplay = pygame.display.set_mode((800,600))
x=gameDisplay.get_width()
y=gameDisplay.get_height()
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:
import pygame
pygame.init()
pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
object_rect = pygame.Rect(pygame.display.get_surface().get_rect().center, (0, 0)).inflate(30, 30)
vel = 5
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
keys = pygame.key.get_pressed()
object_rect.move_ip(
(keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel,
(keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel)
object_rect.clamp_ip(pygame.display.get_surface().get_rect())
pygame.display.get_surface().fill([64] * 3)
pygame.draw.rect(pygame.display.get_surface(), (255, 0, 0), object_rect)
pygame.display.flip()
pygame.quit()
exit()

Rotate pygame screen

I want to rotate the screen of my pygame window by 90 degrees and am unable to find any function to do so. I tried p.transform.rotate() but i guess that is used for Images only. Any help would be deeply appreciated
Of course you can use pygame.transform.rotate(). pygame.display.set_mode() just generates a pygame.Surface which is associated to the display.
However, pygame.transform.rotate() returns a new but rotated pygame.Surface object. Therefore you must blit the surface back on the dispaly:
window.blit(pygame.transform.rotate(window, 90), (0, 0))
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((300, 300))
font = pygame.font.SysFont(None, 100)
clock = pygame.time.Clock()
text = font.render("Display", True, (255, 255, 0))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(text, text.get_rect(center = window.get_rect().center))
window.blit(pygame.transform.rotate(window, 90), (0, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
exit()

Why my rectrangle does not appere(python 3.8 windoes 7) [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I tried to make a clone of a game but the rectangle (which is the basic shape that I will need)does not appear. What did I do wrong or its just pygame going crazy?
code:
# importing modules
import pygame
import sys
import random
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.display.quit()
pygame.quit()
exit()
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# cube
if running == True:
pygame.draw.rect(screen, blue, (395, 0, 10, 10)),
pygame.draw.rect(screen, blue, (395, 10, 10, 10)),
pygame.draw.rect(screen, blue, (395, 20, 10, 10)),
clock.tick(60)
and also how am I going to make it empty and 3d. I know I am asking for a lot but I believe someone can explain
You have to draw the scene in the application loop:
# importing modules
import pygame
import sys
import random
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.rect(screen, blue, (395, 0, 10, 10))
pygame.draw.rect(screen, blue, (395, 10, 10, 10))
pygame.draw.rect(screen, blue, (395, 20, 10, 10))
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
exit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick

Lag when win.blit() background pygame

I'm having trouble with the framerate in my game. I've set it to 60 but it only goes to ~25fps. This was not an issue before displaying the background (was fine with only win.fill(WHITE)). Here is enough of the code to reproduce:
import os, pygame
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50, 50)
pygame.init()
bg = pygame.image.load('images/bg.jpg')
FPS = pygame.time.Clock()
fps = 60
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
winW = 1227
winH = 700
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_icon(win)
def redraw_window():
#win.fill(WHITE)
win.blit(bg, (0, 0))
win.blit(text_to_screen('FPS: {}'.format(FPS.get_fps()), BLUE), (25, 50))
pygame.display.update()
def text_to_screen(txt, col):
font = pygame.font.SysFont('Comic Sans MS', 25, True)
text = font.render(str(txt), True, col)
return text
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redraw_window()
FPS.tick(fps)
pygame.quit()
Ensure that the background Surface has the same format as the display Surface. Use convert() to create a Surface that has the same pixel format. That should improve the performance, when the background is blit to the display, because the formats are compatible and blit do not have to do an implicit transformation.
bg = pygame.image.load('images/bg.jpg').convert()
Furthermore, it is sufficient to create the font once, rather than every time when a text is drawn. Move font = pygame.font.SysFont('Comic Sans MS', 25, True) to the begin of the application (somewhere after pygame.init() and before the main application loop)
Instead use screen.blit(pygame.image.load(picture.png))
Just image = pygame.image.load(picture.png) then screen.blit(image)
( if you keep loading your pictures continuously it will get lag )

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

I have created a window using
pygame.display.set_mode((width, height),
pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)
Later on in the app I want to be able to ask that window its width and height so I can decide how to process the mouse position.
How do I access the dimensions of that pygame window elsewhere in the program? (event processing loop)
You want to get the surface, then get the size from the surface object.
Using tuple unpacking:
w, h = pygame.display.get_surface().get_size()
You can get the windows size by first getting the display surface. pygame.display.get_surface(), then calling get_width() / get_height() on the surface.
Like this!
surface = pygame.display.get_surface() #get the surface of the current active display
x,y = size = surface.get_width(), surface.get_height()#create an array of surface.width and surface.height
import pygame as pg
def create_window(width, height):
"""create a width x height resizable window/frame"""
win = pg.display.set_mode((width, height), pg.RESIZABLE)
# optional fill bg red, default is black
win.fill((255, 0, 0))
# optional title info
sf = "size x=%s y=%s" % (width, height)
pg.display.set_caption(sf)
# any extra code here ...
# draw a white circle
white = (255, 255, 255)
# center can be fixed or calculated for resize
center = (150, 150)
radius = 100
pg.draw.circle(win, white, center, radius)
pg.display.flip()
return win
pg.init()
width = 300
height = 200
win = create_window(width, height)
# event loop and exit conditions (windows titlebar x click)
while True:
for event in pg.event.get():
if event.type == pg.VIDEORESIZE:
width, height = event.size
# redraw win in new size
win = create_window(width, height)
print(win.get_size()) # test
if event.type == pg.QUIT:
raise SystemExit
Try this out.
Hope it helps.
Adding another answer since I haven't seen this one yet.
screen = pygame.display.set_mode() # Default to screen resolution.
area = screen.get_rect()
We can call get_rect() on screen because it is a surface object. area will be a Rect object, with (0, 0, width, height) information in it.
changed = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if event.type == pygame.VIDEORESIZE:
scrsize = event.size # or event.w, event.h
screen = pygame.display.set_mode(scrsize,RESIZABLE)
changed = True
it's almost exactly like this in the comments for pygame.display.init doc page https://www.pygame.org/docs/ref/display.html#comment_pygame_display_update
edited only slightly from 2011-01-25T23:10:35 - Dave Burton thanks dave
gameDisplay = pygame.display.set_mode((800,600))
x=gameDisplay.get_width()
y=gameDisplay.get_height()
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:
import pygame
pygame.init()
pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
object_rect = pygame.Rect(pygame.display.get_surface().get_rect().center, (0, 0)).inflate(30, 30)
vel = 5
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))
keys = pygame.key.get_pressed()
object_rect.move_ip(
(keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel,
(keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel)
object_rect.clamp_ip(pygame.display.get_surface().get_rect())
pygame.display.get_surface().fill([64] * 3)
pygame.draw.rect(pygame.display.get_surface(), (255, 0, 0), object_rect)
pygame.display.flip()
pygame.quit()
exit()

Categories