Is there a way to restrict the title crawl to certain portion of screen?

做~自己de王妃 提交于 2021-02-02 09:33:33

问题


I am trying do end credits like animation the code above for Title crawl, I am trying to make the following changes to it:- 1) The text should begin at the bottom of screen at certain location, such that no other text from the string should be displayed below that location on the screen. 2) The text should stop at certain location on top of screen such that the line at the top should be deleted as soon as it reaches that location making room for other lines in the string. I am a python newbie, I am just experimenting with things, the following code doesn't belong to me either.

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:


Use set_clip() to set the clipping region of the display surface.

e.g. Clip 100 rows at the top and the bottom:

# 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)


来源:https://stackoverflow.com/questions/60508313/is-there-a-way-to-restrict-the-title-crawl-to-certain-portion-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!