我目前的代码如下,我想要发生的是点击按钮后我希望它转到一个空白页面,中间印有“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()
答案 0 :(得分:1)
您可以使用标签创建新类,例如HelloWorld:" Hello world"并在您发布类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类。