2015-06-05 56 views
0

我現在的代碼是在下面,我想要發生的是點擊按鈕後,我希望它進入一個空白頁,在它的中間打印「你好」。代碼如下:如何在pygame中創建一個新窗口?

#!/usr/local/bin/python3.4 
import pygame, sys 
from pygame.locals import * 
windowHeight = 600 
windowWidth = 900 

def main(): 
    pygame.init() 
    screen = pygame.display.set_mode((windowWidth, windowHeight)) 
    pygame.display.set_caption('Test') 
    background = pygame.image.load('background.jpg') 
    screen.blit(background, (0,0)) 
    face = pygame.image.load('text.gif') 
    b = screen.blit(face, (300, 300)) 

    while 1: 
     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      if event.type == pygame.MOUSEMOTION: 
       x, y = event.pos 
       if b.collidepoint(x, y): 
        face = pygame.image.load('background.jpg') 
        b = screen.blit(face, (300, 300)) 
       else: 
        face = face = pygame.image.load('text.gif') 
        b = screen.blit(face, (300, 300)) 
      if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 
       x, y = event.pos 
       if b.collidepoint(x, y): 
        print("button has been clicked") 

     pygame.display.update() 

if __name__ == '__main__': 
    main() 
+0

我不確定你想在這裏做什麼,你的'while'循環會在'main()'之前運行,並導致NameError-s,因爲你還沒有定義屏幕。如果你只想製作一個屏幕,你有正確的想法,只需在'main'中包含'while'循環。如果你想製作幾個屏幕,你不能在pygame中[源代碼](http://stackoverflow.com/questions/7376479/creating-multiple-screens-in-pygame) – Matthew

回答

1

您可以創建與標籤案例的HelloWorld類新:「世界,你好」,創造功能,你空空類的HelloWorld。 對於示例(Python 2.7):

if __name__ == "__main__": 
    def _creator():   
     screen2 = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),0,32) 
     hello = HelloWorld(screen2) 
     hello.run() 
    MENU_ITEMS = ("Welcome", "Exit") 
    SCREEN = pygame.display.set_mode((800, 600), 0, 32) 
    FUNCS = {"Welcome": _creator, "Exit": sys.exit} 
    GM = Main(SCREEN, FUNCS.keys(), FUNCS) 
    GM.run() 

當您單擊 「歡迎」 你啓動類的HelloWorld。

相關問題