开发者社区 问答 正文

如何在pygame中以正确的方式使用blit()?

我试图制作一个纹理游戏,但是它没有错误。我将显示代码。

import time
import pygame
from pygame.locals import \*        
def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:\...\OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    time.sleep(1)
    screen.blit(intro_1, (140, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_2, (340, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_3, (540, 30)) #This isn't working.

Main()

我认为代码screen.blit将与while循环一起使用。所以我尝试了。

import time
import pygame
from pygame.locals import \*        
def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:\...\OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    while True: #This isn't working.
        time.sleep(1)
        screen.blit(intro_1, (140, 30)) #This isn't working.
        time.sleep(1)
        screen.blit(intro_2, (340, 30)) #This isn't working.
        time.sleep(1)
        screen.blit(intro_3, (540, 30)) #This isn't working.

Main()

然后我启动了程序。但是pygame选项卡没有响应。所以我尝试了定义中的screen.blit。

import time
import pygame
from pygame.locals import \*        
def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:\...\OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    while True: #This isn't working.
        screen_blit(screen, intro_1, intro_2, intro_3) #This isn't working.

def screen_blit(screen, intro_1, intro_2, intro_3): #This isn't working.
    time.sleep(1)
    screen.blit(intro_1, (340, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_2, (340, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_3, (340, 30)) #This isn't working.

Main()

再次,没有回应。我怎样才能解决这个问题???

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 19:01:01 1340 分享 版权
1 条回答
写回答
取消 提交回答
  • 窗口没有响应,因为您不处理事件。您不会在窗口中看到任何内容,因为您没有更新显示内容。 最小的典型PyGame应用程序

    • needs a game loop

    • has to handle the events, by either ` pygame.event.pump() ` or ` pygame.event.get() ` .

    • has to update the display ` Surface ` , by either ` pygame.display.flip() ` or ` pygame.display.update() ` .

    例如:

    def Main():
        # [...]
    
        while True: #This isn't working.
    
            # handle the events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.quit()
    
            # draw the scene
            screen.blit(intro_1, (340, 30))
    
            # update the display
            pygame.display.flip()
    

    回答来源:stackoverflow

    2020-03-24 19:01:11
    赞同 展开评论
问答分类:
问答地址: