所以我正在制作2D游戏,我想显示一个获胜的文本,每次我到达屏幕底部(这就是我赢得游戏的方式)时,游戏只会冻结并且不显示我的文本。虽然可以重置,但可以像我想要的那样回到开始
#this is where I set up my text and font
# def text_objects(text, font):
# textSurface = font.render(text, True, BLACK)
# return textSurface, textSurface.get_rect()
#def message_dissplay(text):
# largeText = pygame.font.Font("freesansbold.ttf", 115)
# TextSurf, TextRect = text_objects(text, largeText)
# TextRect.center = ((SCREEN_WIDTH/2), (SCREEN_HEIGHT/2))
# gameDisplay.blit(TextSurf, TextRect)
#time.sleep(2)
# main()
#def GameWon():
# message_dissplay("You Won")
#this is the mainloop I use for my game
#def main():
# pygame.init()
# Set the height and width of the screen
# size = [SCREEN_WIDTH, SCREEN_HEIGHT]
# screen = pygame.display.set_mode(size)
# Main Loop
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#I set up my code so if the player reached 750 pixles he gets
#reset to
#the top and
#I want it to display a win text
if player.rect.y > 750:
GameWon()
我真的很困惑如何在遇到获胜条件时向游戏中添加文字
答案 0 :(得分:0)
draw()
blit()
仅发送到缓冲区。您忘记了pygame.screen.update()
或pygame.screen.flip()
将图形从缓冲区发送到显示它的视频卡。
您GameWon()
使用sleep()
,因此游戏无法运行for event
循环并移动/单击或按下鼠标键。它也无法运行pygame.screen.update()
来在屏幕上发送图形-然后看起来像冻结了。
在示例中,我使用state
在主循环中运行不同的代码-简介,游戏,游戏结束等。我还使用计时器而不是sleep
进行控制,因此它不会停止循环。
我使用外部main()
而不是再次运行loop
,因此游戏可以退出旧循环,重置设置并再次运行旧循环。这样,我不必再次运行main()。
def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
def message_dissplay(text):
largeText = pygame.font.Font("freesansbold.ttf", 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((SCREEN_WIDTH/2), (SCREEN_HEIGHT/2))
#TextRect.center = screen.get_rect().center
gameDisplay.blit(TextSurf, TextRect)
def main():
pygame.init()
# Set the height and width of the screen
size = [SCREEN_WIDTH, SCREEN_HEIGHT]
screen = pygame.display.set_mode(size)
# Main Loop
repeate_game = True:
while repeate_game:
reset_game() # set default values again
state = 'game' #'intro'
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
current_time = pygame.time.get_ticks()
if state = 'intro'
# here code for intro
elif state = 'game'
# here code for game
if player.rect.y > 750:
state = 'gameover'
state_end = current_time + 2000 #2000ms = 2s
elif state = 'pause'
# here code for pause
message_dissplay("Pause")
elif state = 'gameover'
# here code for Game Over
message_dissplay("You Won")
if current_time > state_end:
done = True # to exit `while done` after 2000 ms
# send buffer to video card and on screen
pygame.screen.flip() # or pygame.screen.update()