簡體   English   中英

如何在pygame中創建一個新窗口?

[英]How do I make a new window in pygame?

我當前的代碼如下,我想發生的是單擊按鈕后,我希望它轉到空白頁,中間印有“ Hello”。 代碼如下:

#!/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()

您可以創建帶有標簽“ Hello world”的新類,例如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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM