Python,pygame程序在尝试打开时立即关闭

时间:2015-07-17 20:37:12

标签: python pygame

我正在制作一个简单的pygame程序,它一直运行到现在。当我打开它立即关闭。我已经发现只有当我尝试执行“菜单”功能时才会发生这种情况。其他功能工作正常。对不起我的英文,这是代码:

import pygame
from pygame.locals import *
pygame.init()

'''VARIABLES'''
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

'''GUI AND FPS'''
w = 1000
h = 600
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
FPS = 60
exit = False

'''TEXT FUNCTION'''
def text(msg,color,size,x,y):
    font = pygame.font.SysFont("Arial",size)
    render = font.render(str(msg),True,color)
    screen.blit(render,(x,y))

def gui():
    #ROWS
    screen.fill(white)
    pygame.draw.rect(screen,black,(0,0,10,600))
    pygame.draw.rect(screen,black,(190,0,10,600))
    pygame.draw.rect(screen,black,(390,0,10,600))
    pygame.draw.rect(screen,black,(590,0,10,600))
    pygame.draw.rect(screen,black,(790,0,10,600))
    pygame.draw.rect(screen,black,(990,0,10,600))
    #COLS
    pygame.draw.rect(screen,black,(0,0,1000,10))
    pygame.draw.rect(screen,black,(0,590,1000,10))
    pygame.draw.rect(screen,black,(0,100,1000,10))

    text("Maandag",red,35,30,30)
    text("Dinsdag",red,35,230,30)
    text("Woensdag",red,35,410,30)
    text("Donderdag",red,35,610,30)
    text("Vrijdag",red,35,830,30)

def prompt():
    maand = input("Welke maand?")   

def menu():
    global exit
    while not exit:
        screen.fill(red)
        text("Voor welke maand",white,80,300,20)
        screen.fill(red)
        text("Voor welke maand",white,80,300,20)
        september = pygame.draw.rect(screen,white,(100,100,200,100))
        oktober = pygame.draw.rect(screen,white,(400,100,200,100))
        november = pygame.draw.rect(screen,white,(700,100,200,100))

        december = pygame.draw.rect(screen,white,(100,250,200,100))
        januari = pygame.draw.rect(screen,white,(400,250,200,100))
        februari = pygame.draw.rect(screen,white,(700,250,200,100))

        maart = pygame.draw.rect(screen,white,(100,400,200,100))
        april = pygame.draw.rect(screen,white,(400,400,200,100))
        mei = pygame.draw.rect(screen,white,(700,400,200,100))

        juni = pygame.draw.rect(screen,white,(100,100,200,100))
        exit = pygame.draw.rect(screen,white,(100,100,200,100))

        for event in pygame.event.get():
            if event.type == QUIT:
                exit = True
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit = True
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
    quit()

def main():
    global exit
    while not exit:
        gui()
        for event in pygame.event.get():
            if event.type == QUIT:
                exit = True
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    exit = True
                elif event.key ==K_SPACE:
                    prompt()
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
    quit()
menu()

1 个答案:

答案 0 :(得分:0)

menu函数中绘制看起来像日历月的内容时,最后一个绘图线是

exit = pygame.draw.rect(screen,white,(100,100,200,100))

然后设置exit并结束while循环。您可能只需要在上个月使用不同的名称。