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