Pages

vineri, 21 august 2026

PyGame : using shaders with fonts.

The program shows three text effects using pygame. Each effect is created by slicing the rendered text into thin vertical strips and moving or recoloring those strips. This method works well in Pydroid 3 because it does not require real GPU shaders. The glow effect draws the same text several times with small offsets and low transparency, creating a soft neon halo that pulses over time. The wave effect shifts each vertical slice up or down using a sine function, making the text look like it moves smoothly in a wave pattern. The fire effect applies warm colors and gentle vertical motion to each slice, producing a stable flame‑like animation. The main loop updates a time variable, clears the screen, draws all three effects, and refreshes the display at sixty frames per second. This keeps the animation smooth and consistent.
Let's see the source code:
import pygame
import sys
import math
import random

pygame.init()

WIDTH, HEIGHT = 900, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Optimized Text Effects")

# Larger fonts
font_glow = pygame.font.SysFont("arial", 110)
font_wave = pygame.font.SysFont("comicsansms", 100)
font_fire = pygame.font.SysFont("couriernew", 95)

clock = pygame.time.Clock()
t = 0.0

# Soft pulsing glow effect
def draw_glow(surface, text, font, x, y, color, t):
    pulse = (math.sin(t * 1.5) + 1) * 0.5
    intensity = int(6 + pulse * 8)

    for i in range(1, intensity):
        glow = font.render(text, True, color)
        glow.set_alpha(max(10, 60 - i * 6))
        surface.blit(glow, (x - i, y - i))
        surface.blit(glow, (x + i, y + i))

    main = font.render(text, True, color)
    surface.blit(main, (x, y))

# Smooth wave deformation
def draw_wave(surface, text, font, x, y, t, color):
    base = font.render(text, True, color)
    w, h = base.get_size()

    for i in range(w):
        slice_rect = pygame.Rect(i, 0, 1, h)
        slice_img = base.subsurface(slice_rect)
        offset = int(12 * math.sin(i * 0.04 + t * 1.2))
        surface.blit(slice_img, (x + i, y + offset))

# Stable fire effect
def draw_fire(surface, text, font, x, y, t):
    base = font.render(text, True, (255, 120, 0))
    w, h = base.get_size()

    for i in range(w):
        slice_rect = pygame.Rect(i, 0, 1, h)
        slice_img = base.subsurface(slice_rect)

        offset = int(8 * math.sin(t * 1.5 + i * 0.03))

        r = 255
        g = 100 + int(50 * math.sin(t + i * 0.01))
        b = 20

        slice_img.fill((r, g, b), special_flags=pygame.BLEND_MULT)
        surface.blit(slice_img, (x + i, y + offset))

# Main loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    t += 0.08
    screen.fill((15, 15, 25))

    draw_glow(screen, "NEON GLOW", font_glow, 50, 40, (0, 180, 255), t)
    draw_wave(screen, "WAVE EFFECT", font_wave, 50, 220, t, (255, 255, 255))
    draw_fire(screen, "FIRE TEXT", font_fire, 50, 380, t)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()