有没有办法限制标题爬行到屏幕的某些部分? [英] Is there a way to restrict the title crawl to certain portion of screen?

查看:36
本文介绍了有没有办法限制标题爬行到屏幕的某些部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像动画一样结束字幕爬行,我正在尝试对其进行以下更改:- 1) 文本应从屏幕底部的某个位置开始,以便没有其他文本从字符串应显示在屏幕上该位置的下方.2) 文本应停在屏幕顶部的某个位置,以便在顶部的行到达该位置时应立即删除,为字符串中的其他行腾出空间.我是python新手,我只是在做实验,下面的代码也不属于我.

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

推荐答案

使用 set_clip() 设置显示面的裁剪区域.

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

例如在顶部和底部剪辑 100 行:

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)

这篇关于有没有办法限制标题爬行到屏幕的某些部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆