Pertanyaan Teks Pygame: Daripada warna, biarkan teks menampilkan gambar, atau animasi


Sesuatu yang terlihat seperti ini tetapi saya ingin gambar dan teks dapat diedit.

Something that looks like this but I want the image and text editable

Daripada memiliki sesuatu seperti:

title = menuFont.render("COMPUTER INFORMATION!", 1, BLACK) 
screen.blit(title, Rect(50, 100, 400, 400))

Apakah mungkin warna dalam teks menjadi gambar, atau animasi?

EDIT: Bagi yang penasaran ... ketika saya mengimpor gambar, saya harus mengubah bagian akhir kode sedikit

screen.blit(texture, (50, 50))
screen.fill(BG_COLOR)
screen.blit(text_surface, (50, 170))
pg.display.update()
clock.tick(30)

Layar. Akan muncul setelah tekstur ... hanya kepala :)


5
2018-05-03 23:40


asal


Jawaban:


Untuk tekstur teks Anda, Anda dapat terlebih dahulu membuat teks dalam warna putih, kemudian mengaburkan tekstur ke atasnya dan lulus pygame.BLEND_RGB_MULT sebagai special_flags argumen untuk menggunakan mode campuran banyak. Tekstur hanya akan muncul di bagian buram dari permukaan teks.

Selain itu, pastikan tekstur Anda lebih besar dari permukaan teks, jika tidak, beberapa bagian teks akan tetap tidak terpengaruh.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray32')
FONT = pg.font.Font(None, 50)

# I create a grid texture for demonstration purposes here.
# Just load your image with pygame.image.load instead.
texture = pg.Surface((200, 100))
texture.fill((200, 100, 0))
for x in range(0, 201, 5):
    pg.draw.line(texture, (0, 0, 0), (x, 0), (x, 200))
for y in range(0, 101, 5):
    pg.draw.line(texture, (0, 0, 0), (0, y), (200, y))

# Render the text and use pure white as the color.
text_surface = FONT.render('Hello world!', True, (255, 255, 255))
# Now blit the texture onto the text surface and pass BLEND_RGB_MULT as
# the special_flags argument, so that only the opaque parts are affected.
text_surface.blit(texture, (0, 0), special_flags=pg.BLEND_RGB_MULT)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    screen.fill(BG_COLOR)
    screen.blit(texture, (50, 50))
    screen.blit(text_surface, (50, 170))
    pg.display.flip()
    clock.tick(30)

pg.quit()

Ini versi animasinya. Anda harus memuat frame terpisah dari animasi dan melakukan hal yang sama seperti di atas untuk setiap frame. Masukkan permukaan yang dihasilkan ke dalam daftar dan kemudian putar kembali di loop utama.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray32')
FONT = pg.font.Font(None, 50)

# I create a grid texture for demonstration purposes here.
# Just load your image with pygame.image.load instead.
texture = pg.Surface((200, 100))
texture.fill((200, 100, 0))
for x in range(0, 201, 5):
    pg.draw.line(texture, (0, 0, 0), (x, 0), (x, 200))
for y in range(0, 101, 5):
    pg.draw.line(texture, (0, 0, 0), (0, y), (200, y))

# Render the text and use pure white as the color.
text_surface = FONT.render('Hello world!', True, (255, 255, 255))

frames = []
for i in range(5):
    surf = text_surface.copy()  # We need a fresh copy of the text.
    # Now blit the texture onto the text surface and pass BLEND_RGB_MULT as
    # the special_flags argument, so that only the opaque parts are affected.
    # The y-position is shifted by -1 each iteration.
    surf.blit(texture, (0, -1*i), special_flags=pg.BLEND_RGB_MULT)
    frames.append(surf)

frame_counter = 0
frame_timer = 0
dt = 0

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    frame_timer += dt  # Add the passed time.
    if frame_timer >= 150:  # If 150 milliseconds have passed...
        frame_timer = 0  # Reset the timer.
        frame_counter += 1  # Increment the counter.
        frame_counter %= len(frames)  # Keep it in the correct range.

    screen.fill(BG_COLOR)
    # Now use `frame_counter` as the list index and blit the surface.
    screen.blit(frames[frame_counter], (50, 170))
    pg.display.flip()
    dt = clock.tick(60)  # `dt` is the passed time in milliseconds.

pg.quit()

5
2018-05-04 01:08