如何在一定时间后 blit 一个新的背景图像?
How to blit a new background image after a certain amount of time?
我目前在游戏中有一张背景图片,我想在 30 秒后更改图片。执行此操作的最佳方法是什么,一个功能?一个循环?或 pygame 函数,例如 pygame 等待函数?
def main():
#Loading the music for the title screen of the game and playing it infintely.
sound = pygame.mixer.Sound('C:\Users\Joseph Molina\Desktop\CST\KeyGen.ogg')
sound.play(loops = -1)
while True:
global total_frames
process(bug,FPS, total_frames, SCREENHEIGHT)
#LOGIC
bug.motion(SCREENWIDTH, SCREENHEIGHT)
Enemy.movement(SCREENWIDTH)
BugProjectile.movement()
total_frames += 1
#Bliting the image of the background into the screen at the given coordinates
screen.blit(backgroundImg, (0,0))
#Draws all the sprites to the screen
BaseClass.allsprites.draw(screen)
i = 0
if i < 255:
i += 5
if i > 255:
secondImg = pygame.image.load('C:\Users\Joseph Molina\Desktop\CST\anything.jpg')
screen.blit(secondImg, (0,0))
#pygame.tick(1000)
#Makes sure that everything is being drawn on the screen
pygame.display.flip()
#How many frames are going to be in a second
clock.tick(FPS)
使用 pygame.time.set_timer()
创建事件:
pygame.time.set_timer()
在事件队列中重复创建事件
set_timer(eventid, milliseconds) -> None
Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed.
Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.
To disable the timer for an event, set the milliseconds argument to 0.
当事件发生时,只需将 backgroundImg
更改为另一个 Surface
。
def main():
CHANGEEVENT = pygame.USEREVENT+1
pygame.time.set_timer(MOVEEVENT, 30000)
#Loading the music for the title screen of the game and playing it infintely.
sound = pygame.mixer.Sound('C:\Users\Joseph Molina\Desktop\CST\KeyGen.ogg')
sound.play(loops = -1)
while True:
if pygame.event.get(CHANGEEVENT ):
backgroundImg = get_another_image()
...
我目前在游戏中有一张背景图片,我想在 30 秒后更改图片。执行此操作的最佳方法是什么,一个功能?一个循环?或 pygame 函数,例如 pygame 等待函数?
def main():
#Loading the music for the title screen of the game and playing it infintely.
sound = pygame.mixer.Sound('C:\Users\Joseph Molina\Desktop\CST\KeyGen.ogg')
sound.play(loops = -1)
while True:
global total_frames
process(bug,FPS, total_frames, SCREENHEIGHT)
#LOGIC
bug.motion(SCREENWIDTH, SCREENHEIGHT)
Enemy.movement(SCREENWIDTH)
BugProjectile.movement()
total_frames += 1
#Bliting the image of the background into the screen at the given coordinates
screen.blit(backgroundImg, (0,0))
#Draws all the sprites to the screen
BaseClass.allsprites.draw(screen)
i = 0
if i < 255:
i += 5
if i > 255:
secondImg = pygame.image.load('C:\Users\Joseph Molina\Desktop\CST\anything.jpg')
screen.blit(secondImg, (0,0))
#pygame.tick(1000)
#Makes sure that everything is being drawn on the screen
pygame.display.flip()
#How many frames are going to be in a second
clock.tick(FPS)
使用 pygame.time.set_timer()
创建事件:
pygame.time.set_timer()
在事件队列中重复创建事件
set_timer(eventid, milliseconds) -> None
Set an event type to appear on the event queue every given number of milliseconds. The first event will not appear until the amount of time has passed.
Every event type can have a separate timer attached to it. It is best to use the value between pygame.USEREVENT and pygame.NUMEVENTS.
To disable the timer for an event, set the milliseconds argument to 0.
当事件发生时,只需将 backgroundImg
更改为另一个 Surface
。
def main():
CHANGEEVENT = pygame.USEREVENT+1
pygame.time.set_timer(MOVEEVENT, 30000)
#Loading the music for the title screen of the game and playing it infintely.
sound = pygame.mixer.Sound('C:\Users\Joseph Molina\Desktop\CST\KeyGen.ogg')
sound.play(loops = -1)
while True:
if pygame.event.get(CHANGEEVENT ):
backgroundImg = get_another_image()
...