私はStackOverflowと、一般的にpygameとpythonの両方に慣れていないので、非常に単純な間違いをした場合はご容赦ください。pygame画面に一度に1文字ずつテキストを表示しようとしています。私の関数は正常に動作し、ランダムにフリーズし、キャプション領域に「応答なし」と一貫性のない時間に表示されることを除いて、希望どおりにレンダリングします(たとえば、10文字のレンダリング後にフリーズする場合や、28文字後にフリーズする場合など)。これは、コンピューターを再起動した後でも発生します。私の質問は:これは私に起こっているだけなのか、それとも私のコードに何か問題があるのか、そしてそれが私のコードに問題がある場合は、私がそれを修正するのを手伝ってください。これが私のコードです。よろしくお願いします。
import pygame, time
from pygame.locals import *
width = 800
height = 800
pygame.init()
scrn = pygame.display.set_mode((width, height))
font = pygame.font.SysFont(None, 22)
def render_text(string, bg = None, text_color = (0, 0, 0), surf = scrn, width = width, height = height):
text = ''
for i in range(len(string)):
if bg == None:
surf.fill((255, 255, 255))
else:
surf.blit(bg, (0, 0))
text += string[i]
text_surface = font.render(text, True, text_color)
text_rect = text_surface.get_rect()
text_rect.center = (width/2, height/2)
surf.blit(text_surface, text_rect)
pygame.display.update()
time.sleep(0.05)
def intro():
while True: #just used as an example, it will freeze up usually sometime during the first or second iteration
render_text("It was a dark and stormy night.")
time.sleep(2)
intro()
イベントキューを使用しないPygameプログラムは、代わりに反復ごとにpygame.event.pumpを呼び出す必要があります。これにより、凍結が発生しなくなります。intro
関数を次のように変更すると、機能するはずです。
def intro():
while True:
pygame.event.pump()
render_text("It was a dark and stormy night.")
time.sleep(2)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加