如何在pygame中加快启动时间?

时间:2019-06-22 02:52:55

标签: python pygame

我开始写游戏,但是每当我运行我的代码时,它都需要2分钟才能启动,甚至某些方法也无法正常工作。不起作用的主要方法是退出pygame和drawGameScene()。

我的代码是:

import  os, random
from pygame import *

init()

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)

scalefactor = 2

FPS = 60

screenWidth = round(224 * scalefactor)
screenHeight = round(298 * scalefactor)

size = screenWidth, screenHeight

screen = display.set_mode(size)

button = 0

RED = (255, 0, 0)
BLUE = (0,0,255)

STATEGAME = 1
STATEQUIT = 3

curState = STATEGAME

titleFont = font.SysFont("Times New Roman",45)

def drawText(words, screen,position, color, font):
    text = font.render(words, False, color)
    textSize = text.get_size()

    position[0] = position[0] - textSize[0]//2
    position[1] = position[1] - textSize[1]//2

    #centers the text
    screen.blit(text,position)

def gameRun():
    while curState != STATEQUIT:
        if curState == STATEGAME:
            drawGameScene()
            eventCheck()
            updater()

def eventCheck():
    for evnt in event.get():
        if evnt.type == QUIT:
            curState == STATEQUIT

def updater():
    pass

def drawGameScene():
    draw.rect(screen,RED,(0,0,screenWidth,screenHeight))
    drawText("High Score", screen, [0,0], BLUE, titleFont)
    display.update

gameRun()

display.flip()

未给出错误消息 请帮助,它用于项目

1 个答案:

答案 0 :(得分:0)

对于退出的pygame:

您应使用以下代码:

for events in event.get():

    if events.type == QUIT:

        pygame.quit()
        exit() #this is from sys module

这样,您的pygame首先就退出了。因此,您不需要任何有关curstate的信息。

另外,您需要使用while True语句来重复blitting过程。

完整代码:

import os, random
from pygame import *
from sys import exit
init()
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
scalefactor = 2
FPS = 60
screenWidth = round(224 * scalefactor)
screenHeight = round(298 * scalefactor)
size = screenWidth, screenHeight
screen = display.set_mode(size)
button = 0
RED = (255, 0, 0)
BLUE = (0,0,255)
titleFont = font.SysFont("Times New Roman",45)

def drawText(words,screen,position,color,font):
    text = font.render(words, False, color)
    textSize = text.get_size()
    position[0] = position[0] - textSize[0]//2
    position[1] = position[1] - textSize[1]//2
    #centers the text
    screen.blit(text,position)

def gameRun():
    drawGameScene()
    eventCheck()
    updater()

def eventCheck():
    for events in event.get():
        if events.type == QUIT:
            quit()
            exit()

def updater():
    pass

def drawGameScene():
    draw.rect(screen,RED,(0,0,screenWidth,screenHeight))
    drawText("High Score", screen, [0,0], BLUE, titleFont)
    #display.update()

while True:
    gameRun()
    display.flip()