【问题标题】:How do I use blit() in the proper way in pygame? [duplicate]如何在 pygame 中以正确的方式使用 blit()? [复制]
【发布时间】:2020-03-14 02:13:55
【问题描述】:

我试图制作一个纹理游戏,但它无法正常工作且没有错误。我会显示代码。

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 tab 没有任何反应。所以我在定义中尝试了 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()

再一次,没有回应。我该如何解决这个问题???

【问题讨论】:

    标签: python python-3.x pygame pygame-surface


    【解决方案1】:

    窗口没有响应,因为您没有处理事件。您在窗口中看不到任何内容,因为您没有更新显示。
    一个最小的典型 PyGame 应用程序

    例如:

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

    【讨论】:

      【解决方案2】:

      您需要在每次绘制/blit 时更新屏幕,否则更改将不会显示。

      无论您是在循环内还是在函数中更改内容都没有关系,重要的是您通过调用pygame.display.flip()(更新整个屏幕)或pygame.display.update()(更新整个屏幕)来更新屏幕屏幕或传递给它的矩形。在这个示例中,我使用了 blitting 返回的矩形来仅更新特定区域,还添加了一个简单的游戏循环来处理事件,因此您的游戏将具有响应性并且您可以退出它:

      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!!!")
          pygame.display.update() # Update screen after filling it
      
          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)
          pygame.display.update(screen.blit(intro_1, (140, 30)))
          time.sleep(1)
          pygame.display.update(screen.blit(intro_2, (340, 30))) 
          time.sleep(1)
          pygame.display.update(screen.blit(intro_3, (540, 30)))
      
          while pygame.event.wait().type != QUIT:
              pass
      
      Main()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 1970-01-01
        • 2020-03-03
        相关资源
        最近更新 更多