How to make a Splash text page with Pygame

Some little changes to make the code more usable to get this result. Now you can omit the vertical position. To put an empty line, use two commas ,,

import pygame
 
 
def render_multiline(data):
        "Shows a multiline string with text, y pos and color for each line separated by comma"
        tc = []
        for line in data.split("\n"):
 
            if line != "":
                text, height, color = line.split(",")
                if height == " " or height == "":
                    height = 0
                    if color == " " or color == "":
                        color = "red"
                else:
                    height = int(height)
                tc.append([text, height, color])
        # 2. Each list of the list above is send to write to render text
        cnt = 0
        for t, height, c in tc:
            cnt += 30
            # calls write passing the text, the vertical position and the color
            for i in t.split("\n"):
                if height == 0:
                    height = cnt
                write(i, 200, height, color=c)
                height += 30
 
"""
INSERT:
text, vertical position, color

1. you can omit the vertical position
2. To put a blank line use ,,
"""
TEXT1 = """*** ARKAGAME ***, , gold
A Game by Giovanni Gatto, , red
,,
pythonprogramming.altevista.org, , coral
Game vaguely inspired by classic games, , cyan
like breakout or Arkanoid, , cyan
,,
CHOOSE YOUR GAME, , green
1 - Monochromatic, , cyan
2 - Full color, , cyan
3 - Tiny breaks, , cyan
4 - Tiny version 2, , cyan
5 - Randomized Versions, , cyan
,,
Use the mouse to move the bar, , coral
,,
************ August 2020 - Genuary 2021 *************, , gray"""
 
pygame.init()
 
 
def write(text, x, y, color="Coral",):
    "Returns a surface with a text in the center of the screen, at y coord."
    
    surface_text = font1.render(text, 1, pygame.Color(color))
    text_rect = surface_text.get_rect(center=(500 // 2, y))
    screen.blit(surface_text, text_rect)
    return surface_text
 


def mainloop():
    """Here's where all happens"""
    global screen, clock, font1, font2

    screen = pygame.display.set_mode((500, 600))
    clock = pygame.time.Clock()

    Font = pygame.font.SysFont
    font1 = Font("Arial", 24)
    font2 = Font("Arial", 20)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            render_multiline(TEXT1)
            clock.tick(30)
            pygame.display.update()

mainloop()

Splash page with particles

import pygame
import random 

class Particles(pygame.sprite.Sprite):
    "Creates particles starting from pos with a color"

    def __init__(self, pos, direction, color, sparse=0, turn="on"):
        super(Particles, self).__init__()
        self.list_of_particles = []
        self.pos = pos
        self.color = color
        self.direction = direction
        self.sparse = sparse # generate particles not from the same starting point
        # self.generate_particles()
        group.append(self)
        self.turn = turn # this makes the effect visible

    def choose_directions(self):
        "Makes particles go in every direction you want"
        # Make the flow go down
        if self.direction == "down":
            self.direction = 1
        elif self.direction == "up":
            self.direction = -1
        # Make the particles spread all directions
        elif self.direction == "all":
            self.direction = random.randint(-1, 1)

    def generate_particles(self):
        "List with position etc of particles"

        if self.sparse == 1:
            self.pos[0] = random.randint(0, 600)
        self.choose_directions()
        if random.randint(1, 30) == 1:
            self.list_of_particles.append([
                [self.pos[0], self.pos[1]],
                [random.randint(0, 20) / 10 - 1, self.direction],
                random.randint(4,6)])
        
        # Moving the coordinates and size of self.list_of_particles
        for particle in self.list_of_particles[:]:
            particle[0][0] += particle[1][0]
            particle[0][1] += particle[1][1]
            particle[2] -= 0.005 # how fast circles shrinks
            particle[1][1] += 0.01 # circles speed
            # if particle[2] <= 0:
            c1 = particle[2] < 0 or particle[0][1] > 900 
            c2 = particle[0][1] < 0 or particle[0][0] < 0 or particle[0][0] > 800
            if c1 or c2:
                self.list_of_particles.remove(particle)
                
            # do not call draw from here: it slows down the frame rate
            # self.draw()
        return self.list_of_particles
    
    # def draw(self):
    #     "Draws particles based on data in the self.list_of_particles"
    #     if self.turn == "on":
    #         for particle in self.list_of_particles:
    #              pygame.draw.circle(
    #                 window2, (self.color),
    #             (round(particle[0][0]), round(particle[0][1])),
    #              round(particle[2]))
group = []
p1 = 1
# Some random colors
RED = (255, 0, 0) 
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
GRAY = (255, 255, 244, 100)
GRAY1 = (240, 240, 220, 200)
GRAY2 = (200, 200, 244, 0)
GRAY3 = (220, 200, 220, 200)
# Creating the list with particles ready to be drawn
Particles([0, 0], direction="down", color=GRAY, sparse=1)
Particles([0, 0], direction="down", color=GRAY1, sparse=1)
Particles([0, 0], direction="down", color=GRAY2, sparse=1)
Particles([0, 0], direction="down", color=GRAY3, sparse=1)



def generate():
    "Start drawing all circles on the screen"
    for par in group:
        particles = par.generate_particles()
        draw(par, particles)

def draw(par, particles):
    for particle in particles:
         pygame.draw.circle(
            window2, (par.color),
        (round(particle[0][0]), round(particle[0][1])),
         round(particle[2]))

def render_multiline(data):
        "Shows a multiline string with text, y pos and color for each line separated by comma"
        tc = []
        for line in data.split("\n"):
 
            if line != "":
                text, height, color = line.split(",")
                if height == " " or height == "":
                    height = 0
                    if color == " " or color == "":
                        color = "red"
                else:
                    height = int(height)
                tc.append([text, height, color])
        # 2. Each list of the list above is send to write to render text
        cnt = 0
        for t, height, c in tc:
            cnt += 30
            # calls write passing the text, the vertical position and the color
            for i in t.split("\n"):
                if height == 0:
                    height = cnt
                write(i, 200, height, color=c)
                height += 30
 
"""
INSERT:
text, vertical position, color

1. you can omit the vertical position
2. To put a blank line use ,,
"""
TEXT1 = """*** ARKAGAME ***, , gold
A Game by Giovanni Gatto, , red
,,
pythonprogramming.altevista.org, , coral
Game vaguely inspired by classic games, , cyan
like breakout or Arkanoid, , cyan
,,
CHOOSE YOUR GAME, , green
1 - Monochromatic, , cyan
2 - Full color, , cyan
3 - Tiny breaks, , cyan
4 - Tiny version 2, , cyan
5 - Randomized Versions, , cyan
,,
Use the mouse to move the bar, , coral
,,
************ August 2020 - Genuary 2021 *************, , gray"""
 
pygame.init()
 
 
def write(text, x, y, color="Coral",):
    "Returns a surface with a text in the center of the screen, at y coord."
    
    surface_text = font1.render(text, 1, pygame.Color(color))
    text_rect = surface_text.get_rect(center=(500 // 2, y))
    screen.blit(surface_text, text_rect)
    return surface_text
 

window2 = pygame.Surface((500, 600))
def mainloop():
    """Here's where all happens"""
    global screen, clock, font1, font2

    screen = pygame.display.set_mode((500, 600))
    clock = pygame.time.Clock()

    Font = pygame.font.SysFont
    font1 = Font("Arial", 24)
    font2 = Font("Arial", 20)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
        window2.fill(0)
        generate()
        screen.blit(window2, (0, 0))
        render_multiline(TEXT1)
        clock.tick(60)
        pygame.display.update()

mainloop()

This post is the fusion of 2 older posts

  • post about font and splash screen in pygame
    • first (how to center text)
    • second (how to render text in pygame)
    • third (make a presentation)
  • post about particles in pygame

Github source code

For particles: https://github.com/formazione/pygame_particles

Arkapygame – A tutorial about making a game like arkanoid

For game Arkanoid like for the splash page: https://github.com/formazione/arkapygame

You can download it also on itch.io. There are different versions of the game.


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

hide and show frames

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.