Pygame Readable code: Make a window + Frame rate

This is the simplest thing to do, but with style… and readable code.

import pygame as pg
import sys


# # ============== FRAME RATE FUNCTIONS =====

# def create_fonts(font_sizes_list):
#     "Creates different fonts with one list"
#     fonts = []
#     for size in font_sizes_list:
#         fonts.append(
#             pg.font.SysFont("Arial", size))
#     return fonts


# def render(fnt, what, color, where):
#     "Renders the fonts as passed from display_fps"
#     text_to_show = fnt.render(what, 0, pg.Color(color))
#     screen.blit(text_to_show, where)


# def display_fps():
#     "Data that will be rendered and blitted in _display"
#     render(
#         fonts[0],
#         what=str(int(clock.get_fps())),
#         color="white",
#         where=(0, 0))
# # put this after pygame.init() or pg.init()
# # fonts = create_fonts([32, 16, 14, 8])
# # ==== END ==== #

# =============== Generate the Window =====

def window(title):
    "Create the screen + title and the clock object for the frame"
    screen = pg.display.set_mode((400, 300))
    pg.display.set_caption(title)
    return screen, pg.time.Clock()


def quitbutton():
    "Look for the key events"
    global loop
    for event in pg.event.get():
        if event.type == pg.QUIT:
            loop = 0


def clear_screen():
    screen.fill((0, 64, 128))


def refresh_screen():
    pg.display.flip()
    clock.tick(60)


def update_screen():
    "The while loop updates"
    quitbutton()
    clear_screen()
    # display_fps()
    # show_sprites()
    refresh_screen()

pg.init()
# fonts = create_fonts([32, 16, 14, 8])
screen, clock = window("Game")
loop = 1
# ============== Where everything happens
while loop:
    update_screen()
# ======================== engine end ===

pg.quit()
sys.exit()

We made a function for quitting the window, refreshing it etc. You may found it redounding, but I like it.

The result is just this:

You have seen that there are some commented lines. Let’s uncomment them

import pygame as pg
import sys


# ============== FRAME RATE FUNCTIONS =====

def create_fonts(font_sizes_list):
    "Creates different fonts with one list"
    fonts = []
    for size in font_sizes_list:
        fonts.append(
            pg.font.SysFont("Arial", size))
    return fonts


def render(fnt, what, color, where):
    "Renders the fonts as passed from display_fps"
    text_to_show = fnt.render(what, 0, pg.Color(color))
    screen.blit(text_to_show, where)


def display_fps():
    "Data that will be rendered and blitted in _display"
    render(
        fonts[0],
        what=str(int(clock.get_fps())),
        color="white",
        where=(0, 0))
# put this after pygame.init() or pg.init()
# fonts = create_fonts([32, 16, 14, 8])
# ==== END ==== #

# =============== Generate the Window =====

def window(title):
    "Create the screen + title and the clock object for the frame"
    screen = pg.display.set_mode((400, 300))
    pg.display.set_caption(title)
    return screen, pg.time.Clock()


def quitbutton():
    "Look for the key events"
    global loop
    for event in pg.event.get():
        if event.type == pg.QUIT:
            loop = 0


def clear_screen():
    screen.fill((0, 64, 128))


def refresh_screen():
    pg.display.flip()
    clock.tick(60)


def update_screen():
    "The while loop updates"
    quitbutton()
    clear_screen()
    display_fps()
    # show_sprites()
    refresh_screen()

pg.init()
fonts = create_fonts([32, 16, 14, 8])
screen, clock = window("Game")
loop = 1
# ============== Where everything happens
while loop:
    update_screen()
# ======================== engine end ===

pg.quit()
sys.exit()

You will see this


Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts