How to display a game in Full screen with Pygame

In this example we will see how to display at full screen a game. A text is displayied on the screen surface first and then, this surface, is scaled on the main surface.

import pygame
import sys


pygame.init()
info = pygame.display.Info()
SIZE = WIDTH, HEIGHT = info.current_w, info.current_h
print(WIDTH, HEIGHT)
# flags = pygame.DOUBLEBUF | pygame.FULLSCREEN
mainsurface = pygame.display.set_mode(SIZE)
screen = pygame.Surface((800, 600))
screen.fill((10, 200, 255))
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 72)
text = "Hello World in a Full screen"
rt = font.render(text, 1, pygame.Color("White"))

while True:
	for event in pygame.event.get():
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE:
				pygame.quit()
				sys.exit()
	screen.blit(rt, (0, 0))
	mainsurface.blit(
		pygame.transform.scale(screen, (WIDTH, HEIGHT)), # scale to full
		(0, 0)) # the position of the scaled screen
	pygame.display.update()

The video about making a fullscreen videogame

the screen of the script

Why do not make the window half the screen then?

Some commented code here (a lot of comments, indeed) to show how to use the previous code to make a window half the size of the screen and eventually any screen size related kind of size.

import pygame
import sys

# __________________#
# 9.4.201 - version 1                          _____
# ------------------#
            #                                  | 1 |
			# ==================================== |
			# The most epic pygame commented code  |
			#             ever: fullscreen mode    |________________________
			######################################## brought you by         |
			                             # pythonprogramming.altervista.org |
			                             # Giovanni Gatto on Youtube, too   |
                                         ###################################'
                                         # P.S.: some ironic comments included

# Let's initialize pygame first, it's a good behaviour to do this
pygame.init()

		# --------- Let's get info about screen size ------ #
info = pygame.display.Info()
SIZE = WIDTH, HEIGHT = info.current_w, info.current_h
print(WIDTH, HEIGHT)
# flags = pygame.DOUBLEBUF | pygame.FULLSCREEN

	# --------- Now, what ? --------- #
		# Now I use the screen size to adapt the size of the main 
		# Surface (callad - btw - mainsurface)
mainsurface = pygame.display.set_mode((WIDTH//2, HEIGHT//2))

# =========== Temporary surface that will be scaled on the main one in the 
            # while game loop (at the bottom of this lines of code)
screen = pygame.Surface((800, 600))
screen.fill((10, 200, 255)) # A nice background color

# Ever heard of frame rate? There it is
clock = pygame.time.Clock()

# I'm gonna creat a font type, render a text with that font type
# finally, in the while game loop, I will blit it on the screen
# otherwise there would be nothing on the screen except a blue color,
# nice but useles for the user... we do not want to see just blue color, right,
# even if it's a nice color... perhaps I'm commenting too much... ok, end!
font = pygame.font.SysFont("Arial", 72)
text = "Hello World in a Full screen"
rt = font.render(text, 1, pygame.Color("White"))


                       #-----------#
# ===================== GAME - LOOP ================================= #
                 # Everythings runs here   #
                 # keep your hands tied to #
                 # the belts, here we go   #
            # --- do not say I did not warnt you --- #


def quit():
	""" get_events sent here """
	print("EXIT!")
	pygame.quit()
	sys.exit()


# This uses the function up here
def get_user_events():
	""" Get the user events here, quitely """
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			quit()
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_ESCAPE:
				quit()


# it uses the function up here, that uses the function up there
def gameloop_start():
	''' Everything starts here - this I said this before?'''
	while True:
		get_user_events()
		screen.blit(rt, (0, 0))
		mainsurface.blit(
			pygame.transform.scale(screen, (WIDTH//2, HEIGHT//2)), # scale to full
			(0, 0)) # the position of the scaled screen
		pygame.display.update()


# I wanted to be very explicit here in my intentions
# THIS IS WHERE WE REALLY START... well, there are a couple of lines
# above.... screen, clock... and other stuff you should have
# notice above, as I said above (too many aboves...)
gameloop_start()

''''
I started from this functions chain of code... logic, uh? 
gamelooop_start -> get_user_events -> quit
* ps: remember to put 2 empty lines afeter functions... it's nice!
'''

The output is the same, just half sized.

See ya soon.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.