如何加快pygame的开始时间?

2024-10-03 13:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始写一个游戏,但每当我运行我的代码,需要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()

未给出错误消息 请帮忙,这是为了一个项目


Tags: textsizedefdisplaypositionscreenfontscreenheight
1条回答
网友
1楼 · 发布于 2024-10-03 13:31:05

对于退出游戏:

您应该使用以下代码:

for events in event.get():

    if events.type == QUIT:

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

这样,你的游戏一开始就退出了。所以,你不需要任何关于草书之类的东西

此外,还需要使用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()

相关问题 更多 >