2014-05-25 5 views
1

파이 게임 프로그램을 쓰려고하는데 파이 게임 형식의 텍스트를 타사 모듈이없는 단락으로 사용하는 방법을 알아낼 수 없습니다. 예를 들어, 내가 원하는 :pygame의 텍스트가 단락으로 포맷 되었습니까?

"Hello World, how are you doing today? I'm fine actually, thank you." 

가의 라인을 따라 더 될 :

def instructions(text): 
    pressKey, pressRect = makeText('Last Block is a survival game. Every ten lines that you clear will shorten the screen, clear as many lines as possible before you run out of space!', smallFont, bgColor) 
    pressRect.center = (int(windowWidth/ 2), int(windowHeight/2) + 100) 
    displaySurf.blit(pressKey, pressRect) 
    while press() == None: 
     pygame.display.update() 

makeText 기능 :

def makeText(text, font, color): 
    surf = font.render(text, True, color) 
    return surf, surf.get_rect() 
다음

"Hello World, how are 
you doing today? I'm 
fine actually, thank you." 

내 코드입니다

파이 게임에 긴 줄의 텍스트를 각각 x 단어로 나눈 다음 단락처럼 보이게 형식을 지정하십시오. 지금은 지루한 것처럼 보이게하기 위해 수많은 서페이스 개체와 블릿을 사용하고 있습니다. 그 효과를 얻기위한 다른 방법은 없습니까? 당신이 textwrap.fill에서 제공하는 기능을 찾고있는 것처럼

+0

"외부 모듈"이란 제 3 자 모듈 또는 '파이 게임'이 아닌 모든 모듈을 의미합니까? 즉, 기본 제공 모듈을 사용해도됩니까? 그렇다면 기본 제공 ['textwrap'] (https://docs.python.org/2/library/textwrap.html) 모듈에 관심이있을 수 있습니다. – iCodez

+0

타사 = 외부. 가능하다면,'textwrap' 모듈을 빨리 설명 할 수 있습니까? – Anonmly

답변

1

그것은 소리 :

from textwrap import fill 
mystr = "Hello World, how are you doing today? I'm fine actually, thank you." 
print(fill(mystr, 20)) 
print() 
print(fill(mystr, 40)) 
print() 
print(fill(mystr, 10)) 

출력 :

 
Hello World, how are 
you doing today? I'm 
fine actually, thank 
you. 

Hello World, how are you doing today? 
I'm fine actually, thank you. 

Hello 
World, how 
are you 
doing 
today? I'm 
fine 
actually, 
thank you. 

textwrap.fill의 첫 번째 인수는 당신이 휴식 할 문자열입니다. 두 번째 줄의 최대 길이 (문자)입니다.

+0

안녕하세요, 나는'pygame' 텍스트 객체를 만드는 함수 내에서 이것을 시도했습니다. 선들을 나누는 대신,'채우기'는 그들 사이에 상자를 만들었습니다. 내 질문을 편집하여 전체 기능을 포함시킵니다. 나는 파이 게임 내에서 문자열을 사용하고 있다고 믿지 않으며 대신 함수에 다른 매개 변수로 전달됩니다. – Anonmly

2

정확하게 이해하면 일부 코드가 여러 줄의 단락에 텍스트를 표시해야합니다. 다시 게시

def render_textrect(string, font, rect, text_color, background_color, justification=0): 
    """Returns a surface containing the passed text string, reformatted 
    to fit within the given rect, word-wrapping as necessary. The text 
    will be anti-aliased. 

    Takes the following arguments: 

    string - the text you wish to render. \n begins a new line. 
    font - a Font object 
    rect - a rectstyle giving the size of the surface requested. 
    text_color - a three-byte tuple of the rgb value of the 
       text color. ex (0, 0, 0) = BLACK 
    background_color - a three-byte tuple of the rgb value of the surface. 
    justification - 0 (default) left-justified 
        1 horizontally centered 
        2 right-justified 

    Returns the following values: 

    Success - a surface object with the text rendered onto it. 
    Failure - raises a TextRectException if the text won't fit onto the surface. 
    """ 

죄송합니다 - 실수로 커뮤니티 위키를 클릭 :

http://www.pygame.org/pcr/text_rect/index.php은 추가 modules.The 방법은 다음과 같은 매개 변수를 필요로하지 않는다 : 그렇다면, 나는 여기에 코드를 사용했다.

+0

'recttype' 매개 변수 란 무엇입니까? – Anonmly

+0

'recttype'을 어디에서 가져올 지 모르겠습니다. 나는 당신이 어떤 논증을 통과해야하는지 보여주는 대답을 업데이트했다. 'rect'를 언급하고 있다면 이것은 pygame.Rect 객체 (http://www.pygame.org/docs/ref/rect.html)입니다. 기본적으로 텍스트를 원할 영역의 크기입니다 에서. – elParaguayo

관련 문제