为什么没有标题屏幕显示任何内容?

时间:2016-11-10 23:05:03

标签: python pygame

import pygame,sys
pygame.init()

size = (425,550)
screen = pygame.display.set_mode(size)

lightcyan = (191,255,255)
black = (0,0,0)

titlefont = pygame.font.SysFont("Calibri",36)
subtitlefont = pygame.font.SysFont("Calibri",24)

def title():
    titlescreenAlpha = 0

    centeredText = pygame.display.set_mode(size)
    centeredText = centeredText.convert()
    centeredText.set_alpha(0)

    titlescreen = pygame.display.set_mode(size).convert()
    titlescreen.set_alpha(titlescreenAlpha)
    nowiseeText = titlefont.render("NOW I SEE",1,black)
    centeredText.blit(nowiseeText,(0,0))
    nowiseeTextRect = nowiseeText.get_rect()
    nowiseeTextRect.center = (size[0]/2,225)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        if titlescreenAlpha < 255:
            titlescreenAlpha += 1
            titlescreen.set_alpha(titlescreenAlpha)
        print(titlescreenAlpha)

        screen.fill(lightcyan)
        titlescreen.blit(nowiseeText,nowiseeTextRect)
        pygame.display.flip()
        pygame.time.wait(10)

title()

我试图制作关于光学的电子传播的标题序列。这就是我到目前为止所做的一切。

每当我运行它时,titlescreenAlpha会增加1直到达到255,但titlescreen.set_alpha(titlescreenAlpha)似乎无法正常工作。

为什么我只能在屏幕上看到内容?

1 个答案:

答案 0 :(得分:1)

你的代码有些混乱。您使用set_mode三次,但它用于创建窗口,而不是曲面。 Pygame有pygame.Surface()来创建表面。

主要问题是alpha

PyGame有两种类型的alpha:

  • 每个像素alpha - 每个像素都有自己的alpha值(即带有透明背景的图像)
  • 全表面alpha - pygame对所有像素使用相同的alpha(即平滑显示/隐藏图像)

PyGame不能同时在一个表面使用 - 请参阅set_alpha

您必须使用背景创建文字,然后才能使用set_alpha

import pygame
import sys

# --- constants --- (UPPER_CASE names)

LIGHTCYAN = (191, 255, 255)
BLACK     = (  0,   0,   0)
WHITE     = (255, 255, 255)

SIZE = (425, 550)

# --- classes --- (CamelCase names)

# empty

# --- functions --- (lower_case names)

def title():

    # init
    pygame.init()

    # create window
    screen = pygame.display.set_mode(SIZE)
    screen_rect = screen.get_rect()

    # create fonts
    title_font = pygame.font.SysFont("Calibri", 36)
    subtitle_font = pygame.font.SysFont("Calibri", 24)

    # render surface with text (and background) and center on screen
    text = title_font.render("NOW I SEE", True, BLACK, LIGHTCYAN) # WHITE)
    text = text.convert()
    text_rect = text.get_rect(center=screen_rect.center)

    text_alpha = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()

        # change "full surface alpha"
        if text_alpha < 255:
            text_alpha += 1
            text.set_alpha(text_alpha)
        print(text_alpha)

        # draw everything on `screen`
        screen.fill(LIGHTCYAN)
        screen.blit(text, text_rect)
        pygame.display.flip()

        pygame.time.wait(10)

# --- main ---

title()

编辑:更复杂的版本:)

import pygame
import sys

# --- constants --- (UPPER_CASE names)

LIGHTCYAN = (191, 255, 255)
BLACK     = (  0,   0,   0)
WHITE     = (255, 255, 255)

SIZE = (425, 550)

# --- classes --- (CamelCase names)

# empty

# --- functions --- (lower_case names)

def title(message, screen, scree_rect):

    # create fonts
    title_font = pygame.font.SysFont("Calibri", 36)
    subtitle_font = pygame.font.SysFont("Calibri", 24)

    # render surface with text (and background) and center on screen
    text = title_font.render(message, True, BLACK, LIGHTCYAN) # WHITE)
    text = text.convert()
    text_rect = text.get_rect(center=screen_rect.center)

    text_alpha = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False # to skip rest of program
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    return True

        # change "full surface alpha"
        if text_alpha < 255:
            text_alpha += 1
            text.set_alpha(text_alpha)
        print(text_alpha)

        # draw everything on `screen`
        screen.fill(LIGHTCYAN)
        screen.blit(text, text_rect)
        pygame.display.flip()

        pygame.time.wait(10)

# --- main ---

# init

pygame.init()

# create window

screen = pygame.display.set_mode(SIZE)
screen_rect = screen.get_rect()

# many scenes

result = title("CLICK MOUSE", screen, screen_rect)

if result: # skip if pygame.QUIT
    result = title("OR PRESS ESC", screen, screen_rect)

if result: # skip if pygame.QUIT
    result = title("NOW I SEE", screen, screen_rect)

if result: # skip if pygame.QUIT
    result = title("HELLO WORLD", screen, screen_rect)

if result: # skip if pygame.QUIT
    result = title("BYE, BYE", screen, screen_rect)

# quit

pygame.quit()
sys.exit()