微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简单的 Pygame 加载缓慢

如何解决简单的 Pygame 加载缓慢

我第一次尝试使用 Pygame 时制作了一个小井字游戏,但加载需要大约 10 秒。这是正常的吗?如果没有,我怎样才能让它更快?链接到下面的代码,如果有点乱请见谅。

import pygame,sys,time
from pygame.locals import *

moves = []
rects = []

# Initialize program
pygame.init()

# Assign FPS a value
FPS = 30
FramePerSec = pygame.time.Clock()

# Setting up color objects
BLUE  = (0,255)
RED   = (255,0)
GREEN = (0,255,0)
BLACK = (0,0)
WHITE = (255,255)

# Setup a 300x300 pixel display with caption
disPLAYSURF = pygame.display.set_mode((300,400))
disPLAYSURF.fill(WHITE)
pygame.display.set_caption("Tic Tac Toe")

def checkWin(c):
    if moves[0] == c and moves[1] == c and moves[2] == c:
        return True
    elif moves[3] == c and moves[4] == c and moves[5] == c:
        return True
    elif moves[6] == c and moves[7] == c and moves[8] == c:
        return True
    elif moves[0] == c and moves[4] == c and moves[8] == c:
        return True
    elif moves[2] == c and moves[4] == c and moves[6] == c:
        return True
    elif moves[0] == c and moves[3] == c and moves[6] == c:
        return True
    elif moves[1] == c and moves[4] == c and moves[7] == c:
        return True
    elif moves[2] == c and moves[5] == c and moves[8] == c:
        return True

    return False

def drawX(rect):
    pygame.draw.line(disPLAYSURF,RED,(rect.centerx - 25,rect.centery - 25),(rect.centerx + 25,rect.centery + 25),2)

    pygame.draw.line(disPLAYSURF,2)

    moves.pop(rects.index(rect))
    moves.insert(rects.index(rect),"X")

def drawO(rect):
    moves.pop(rects.index(rect))
    moves.insert(rects.index(rect),'O')
    pygame.draw.circle(disPLAYSURF,BLUE,rect.center,25,2)

# define a main function
def main():
    # Draw grid
    for i in range(3):
        for j in range(3):
            pygame.draw.rect(disPLAYSURF,BLACK,(i*100,j*100,100,100),2)
            rects.append(pygame.Rect((i*100,j*100),(98,98)))
            moves.append(' ')

    pygame.draw.rect(disPLAYSURF,(0,300,0)
    myfont = pygame.font.SysFont(None,100) # use default system font,size 100
    mytext = myfont.render("X's Turn",True,WHITE)
    disPLAYSURF.blit(mytext,(25,325))
    pygame.display.update()

    gameOver = False
    xTurn = True
    count = 0

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN and gameOver == False:
                mouse_pos = pygame.mouse.get_pos()
                for rect in rects:
                    if rect.collidepoint(mouse_pos) and moves[rects.index(rect)] == ' ':
                        count += 1
                        if xTurn == True:
                            drawX(rect)
                            if checkWin('X') == False:
                                if count == 9:
                                    mytext = myfont.render("Draw!",WHITE)
                                    gameOver = True
                                else:
                                    xTurn = False
                                    mytext = myfont.render("O's Turn",WHITE)
                            else:
                                mytext = myfont.render("X Wins!",GREEN)
                                gameOver = True
                        elif xTurn == False:
                            drawO(rect)
                            if checkWin('O') == False:
                                if count == 9:
                                    mytext = myfont.render("Draw!",WHITE)
                                    gameOver = True
                                else:
                                    xTurn = True
                                    mytext = myfont.render("X's Turn",WHITE)
                            else:
                                mytext = myfont.render("O Wins!",GREEN)
                                gameOver = True
                        print(moves)
                        pygame.draw.rect(disPLAYSURF,0)
                        disPLAYSURF.blit(mytext,325))
                        pygame.display.update()

        FramePerSec.tick(FPS)

if __name__=="__main__":
    # call the main function
    main()

解决方法

我发现加载时间很长的那一行是pygame.font.SysFont(None,100),我在这里找到了解决这个问题的方法:github.com/pygame/pygame/issues/344>

安装 XQuartz 2.7.11 后,加载速度更快

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。