层的数值不一样,显示的优先级就不一样,数字越大,越显示上层。
所以,可以把背景层设置成 -1层,很少用的界面,设置成 -2,先隐藏起来。
切换界面时,只要设置层的数值机可以了。 代码如下:
import pygame
import pygame
import os
import random
vect = pygame.math.Vector2
#define sprite groups. Do this before creating sprites
textgroup = pygame.sprite.Group()
backgroundgroup = pygame.sprite.Group()
menugroup =pygame.sprite.Group()
# only the allgroup draws the sprite, so i use LayeredUpdates() instead Group()
# more sophisticated, can draw sprites in layers
allgroup = pygame.sprite.LayeredUpdates()
SCENEWIDTH = 480
SCENEHEIGHT = 850
class Menu(pygame.sprite.Sprite):
""" display a menu background"""
def __init__(self):
self.groups = allgroup, menugroup
self._layer = -2
pygame.sprite.Sprite.__init__(self, self.groups)
self.image = pygame.image.load('images/bg1.png'