Есть ли способ ограничить сканирование заголовка определенной частью экрана? - PullRequest
1 голос
/ 03 марта 2020

Я пытаюсь сделать конечные кредиты, такие как анимация, код выше для сканирования заголовка, я пытаюсь внести в него следующие изменения: - 1) Текст должен начинаться в нижней части экрана в определенном месте, чтобы никакой другой текст из строки должен отображаться ниже этого места на экране. 2) Текст должен останавливаться в определенном месте вверху экрана, так что строка вверху должна быть удалена, как только он достигнет этого места, освобождая место для других строк в строке. Я python newb ie, я просто экспериментирую с вещами, следующий код мне тоже не принадлежит.

import pygame
from pygame.locals import *

pygame.init()
pygame.display.set_caption('Title Crawl')
screen = pygame.display.set_mode((1000, 800))
screen_r = screen.get_rect()
font = pygame.font.SysFont("franklingothicdemibold", 40)
clock = pygame.time.Clock()

def main():

    crawl = ["Star Wars - The Wilds"," ","It is  a dark time for the Galaxy. The evil Dark","Lord, Vitiate is rising to power. Alone, a single", "spec   is  on  a  trip,  a  trip that will ultimately", "rectify  the wrongs of the galaxy. The keepers ", "of  peace are dying out and the  DARK SIDE is", "lurking,   a   conniving   force   determined  to", "become the omniarch."]

    texts = []
    # we render the text once, since it's easier to work with surfaces
    # also, font rendering is a performance killer
    for i, line in enumerate(crawl):
        s = font.render(line, 1, (229, 177, 58))
        # we also create a Rect for each Surface. 
        # whenever you use rects with surfaces, it may be a good idea to use sprites instead
        # we give each rect the correct starting position 
        r = s.get_rect(centerx=screen_r.centerx, y=screen_r.bottom + i * 45)
        texts.append((r, s))

    while True:
        for e in pygame.event.get():
            if e.type == QUIT or e.type == KEYDOWN and e.key == pygame.K_ESCAPE:
                return

        screen.fill((0, 0, 0))

        for r, s in texts:
            # now we just move each rect by one pixel each frame
            r.move_ip(0, -1)
            # and drawing is as simple as this
            screen.blit(s, r)

        # if all rects have left the screen, we exit
        if not screen_r.collidelistall([r for (r, _) in texts]):
            return

        # only call this once so the screen does not flicker
        pygame.display.flip()

        # cap framerate at 60 FPS
        clock.tick(60)

if __name__ == '__main__': 
    main()

1 Ответ

1 голос
/ 03 марта 2020

Используйте set_clip(), чтобы установить область отсечения поверхности дисплея.

например, обрезать 100 строк сверху и снизу:

# set clipping rectangle
clip_rect = (0, 100, screen.get_width(), screen.get_height()-200)
screen.set_clip(clip_rect)

for r, s in texts:
    # now we just move each rect by one pixel each frame
    r.move_ip(0, -1)
    # and drawing is as simple as this
    screen.blit(s, r)

# cancel clipping for further drawing
screen.set_clip(None)
...