创建菜单时出现黑屏
我遇到一个问题。我用Python做了一个游戏的菜单(其实更像是开始界面)。但是,当我运行代码时,看到一个标题合适的窗口,但屏幕却是黑色的。我哪里做错了呢?
#importing the libraries
import pygame
import sys
import os
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
#colour R G B
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
DARKGREEN = ( 0, 155, 0)
DARKGREY = ( 40, 40, 40)
BGCOLOR = BLACK
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.init()
#Drawing the message
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render("Press a key to play...", True, DARKGREY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
#Showing the start screen
def showStartScreen():
titleFont = pygame.font.Font(None, 100)
titleMain = titleFont.render('Badger Defense', True, WHITE, BGCOLOR)
titleSecond = titleFont.render("Don't get your family killed!", True, GREEN)
while True:
drawPressKeyMsg()
#Main function
def main():
global DISPLAYSURF, BASICFONT
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
BASICFONT = pygame.font.Font(None, 18)
pygame.display.set_caption('Badger Defense - Aplha(0.0.1)')
showStartScreen()
#Drawing the screen
DISPLAYSURF.fill(BGCOLOR)
pygame.display.update()
#Reaction to the message
def checkForKeyPress():
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
os.system('python game.py')
if __name__ == "__main__":
main()
我在使用Sublime编辑器,并且运行的是Ubuntu 12.04系统。我把游戏和所有资源都放在和菜单同一个文件夹里,并且那里还有一个__init__
.py文件。