1

I was creating my Cuphead inspired game, a fan game and I was creating the warning text and it is lagging a lot like 0-2 fps if anyone can help me I would be very grateful, I don't know if programming on the cell phone interferes with anything or it's the same thing but anyway if you can help me thank you

My code:

import pygame
import sys
pygame.init()

resolutions = ['1152, 648', '1280, 720', '1365, 768', '1600, 900', '1920, 1080']

screen_max = ((1920, 1080))
screen = pygame.display.set_mode((screen_max), pygame.FULLSCREEN | pygame.SCALED)

game_fontT = int(screen_max[0]/30)
game_font = pygame.font.Font('Fonts/AlegreyaSansSC-Bold.ttf', int(game_fontT))

FPS = 60
click = False

linguas = ['espanhol', 'ingles', 'portugues']

lingua = linguas[2]

Warn_Screen = True

def show_fps():
    fr = str(int(clock.get_fps()))
    frt = game_font.render(str(fr), 1, pygame.Color("coral"))
    return frt

def text(text, posx, posy, R, G, B):
    posx = int(posx)
    posy = int(posy)
    R = int(R)
    G = int(G)
    B = int(B)
    score_surface = game_font.render(f'{text}', True, (R, G, B))
    score_rect = score_surface.get_rect(center = (posx, posy))
    rgb_surface1 = game_font.render(f'{text}', True, (0, 255, 0))
    rgb_rect1 = score_surface.get_rect(center = (posx-1, posy-1))
    rgb_surface2 = game_font.render(f'{text}', True, (255, 0, 0))
    rgb_rect2 = score_surface.get_rect(center = (posx+1, posy+1))
    screen.blit(rgb_surface1, rgb_rect1)
    screen.blit(rgb_surface2, rgb_rect2)
    screen.blit(score_surface, score_rect)

clock = pygame.time.Clock()
while True:
    screen.fill([255, 207, 17])
    ftr = show_fps()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        if event.type == pygame.MOUSEBUTTONDOWN:
            click = True
        
        if event.type == pygame.MOUSEBUTTONUP:
            click = False
                        
    if Warn_Screen:
        if lingua == linguas[2]:
            text('aviso', screen_max[0]/2, 45, 0, 0, 0)
            text(f'Todas as coisas desse jogo pertencem ao studio MDHR', screen_max[0]/2, screen_max[1]/2-310, 0, 0, 0)
            text('Tudo que você está jogando aqui é apenas uma recriação', screen_max[0]/2, screen_max[1]/2-260, 0, 0, 0)
            text('Todas as músicas, sprites, efeitos, fontes pertencem ao Studio MDHR', screen_max[0]/2, screen_max[1]/2-210, 0, 0, 0)
            text('Essa é apenas uma recriação feita por um fã', screen_max[0]/2, screen_max[1]/2-160, 0, 0, 0)
            text('Então quando puder compre o original', screen_max[0]/2, screen_max[1]/2-110, 0, 0, 0)
            text('Você não irá se arrepender.', screen_max[0]/2, screen_max[1]/2-60, 0, 0, 0)
        
        if lingua == linguas[0]:
            text('Advertencia', screen_max[0]/2, 45, 0, 0, 0)
            text('Todo el material de este juego pertenece al estudio MDHR', screen_max[0]/2, screen_max[1]/2-310, 0, 0, 0)
            text('Todo lo que estás jugando aquí es solo una recreación', screen_max[0]/2, screen_max[1]/2-260, 0, 0, 0)
            text('Todas las canciones, sprites, efectos, fuentes pertenecen a MDHR', screen_max[0]/2, screen_max[1]/2-210, 0, 0, 0)
            text('Esta es solo una recreación hecha por fanáticos', screen_max[0]/2, screen_max[1]/2-160, 0, 0, 0)
            text('Así que cuando puedas comprar el original', screen_max[0]/2, screen_max[1]/2-110, 0, 0, 0)
            text('No te arrepentirás.', screen_max[0]/2, screen_max[1]/2-60, 0, 0, 0)
            
        if lingua == linguas[1]:
            text('Warning', screen_max[0]/2, 45, 0, 0, 0)
            text('All stuff in this game belongs to MDHR studio', screen_max[0]/2, screen_max[1]/2-310, 0, 0, 0)
            text("Everything you're playing here is just a recreation", screen_max[0]/2, screen_max[1]/2-260, 0, 0, 0)
            text('All songs, sprites, effects, fonts belong to Studio MDHR', screen_max[0]/2, screen_max[1]/2-210, 0, 0, 0)
            text('This is just a fan-made recreation', screen_max[0]/2, screen_max[1]/2-160, 0, 0, 0)
            text('So when you can buy the original', screen_max[0]/2, screen_max[1]/2-110, 0, 0, 0)
            text('You will not regret.', screen_max[0]/2, screen_max[1]/2-60, 0, 0, 0)
        
        if click:
            Warn_Screen = False
            
    screen.blit(ftr, (0, 0))
    pygame.display.flip()
    clock.tick(FPS)
  • 2
    The text is static, so instead of rendering the text every frame, draw the text on a surface before the application loop and [`blit`](https://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit) that surface every frame. – Rabbid76 May 29 '22 at 17:28
  • i'm a little new to pygame so i had a hard time getting this to a surface could you help me? – Samuel Ezequias Plays Jun 03 '22 at 21:54

1 Answers1

0

The text is static, so instead of rendering the text every frame, draw the text on a surface before the application loop and blit that surface every frame.

Write a function that draws a text with multiple lines on a transparent pygame.Surface.A transparent surface can be created with the flag SRCAPHA:

def renderText(font, color, textList):
    fontHeihgt = font.get_height()
    textSurfaceList = [font.render(t, True, color) for t in textList]
    width = max([s.get_width() for s in textSurfaceList])
    height = len(textSurfaceList) * fontHeihgt
    textSurface = pygame.Surface((width, height), pygame.SRCALPHA)
    [textSurface.blit(s, (0, fontHeihgt*i)) for i, s in enumerate(textSurfaceList)]
    return textSurface

See also Rendering text with multiple lines in pygame.

Use this function and render the text before the application loop and blit the text inside the loop.

Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

def renderText(font, color, textList):
    fontHeihgt = font.get_height()
    textSurfaceList = [font.render(t, True, color) for t in textList]
    width = max([s.get_width() for s in textSurfaceList])
    height = len(textSurfaceList) * fontHeihgt
    textSurface = pygame.Surface((width, height), pygame.SRCALPHA)
    [textSurface.blit(s, (0, fontHeihgt*i)) for i, s in enumerate(textSurfaceList)]
    return textSurface

font = pygame.font.SysFont(None, 50)
text = renderText(font, (255, 255, 0), ["This is a", "multi-line text.", ";-)"])

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    window_center = window.get_rect().center
    window.fill(0)
    window.blit(text, text.get_rect(center = window_center))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174