如何在 pygame 中以正确的方式使用 blit()?
How do I use blit() in the proper way in pygame?
我正在尝试制作一个纹理游戏,但没有任何错误。我会显示代码。
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()
同样,没有回应。我该如何解决这个问题???
window 没有响应,因为您不处理事件。您在 window 中看不到任何内容,因为您没有更新显示。
最小的典型 PyGame 应用程序
需要一个游戏循环
必须通过 pygame.event.pump()
or pygame.event.get()
.
来处理事件
必须更新显示 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()
每次 draw/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()
我正在尝试制作一个纹理游戏,但没有任何错误。我会显示代码。
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()
同样,没有回应。我该如何解决这个问题???
window 没有响应,因为您不处理事件。您在 window 中看不到任何内容,因为您没有更新显示。
最小的典型 PyGame 应用程序
需要一个游戏循环
必须通过
pygame.event.pump()
orpygame.event.get()
. 来处理事件
必须更新显示
Surface
, by eitherpygame.display.flip()
orpygame.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()
每次 draw/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()