Pygame 运行缓慢。

4

enter image description here 我正在制作一个名为“生存岛”(Survival Island)的游戏,并刚刚创建了开始界面。 在执行事件后,pygame反应过慢(需要时间来响应)。

这是我的源代码:

#packages
import pygame
import sys
from sys import exit
#initialization
pygame.init()
#display surf
width = 600
height = 400
surface  = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()#for fps
#caption
pygame.display.set_caption('Survival Island')

#variables
mousex = 0
mousey = 0
#booleans
play = True #entered playmode
canQuitOnStart = True     #game can be quitted on start
drawStartScreen = True #start screen drawed
running = True # game is running
#definitions
def quitOnStart():     #quitting the game
    #can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(550,350,40,40))]
    global mousex,mousey,running
    for event in pygame.event.get():
       if event.type == pygame.MOUSEBUTTONDOWN: #quit on pressing x on start screen
            if mousex > 550 and mousey > 350 and mousex <590 and mousey <390:
                print('Exit1')
                running = False

def drawStart():      #drawing start menu
    START_Image = pygame.image.load('START_Image.png').convert()
    surface.blit(START_Image,(0,0))
    pygame.display.update()

def playGame():
    #play on clicking on "play"
    global mousex,mousey,canQuitOnStart,drawStartScreen
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
           if mousex > 415 and mousey >190 and mousex <70 and mousey <30: # can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(415,190,70,30))]
                canQuitOnStart = False
                drawStartScreen = False
                screen.fill((0,0,0))
                pygame.display.update()
if drawStartScreen == True:
        drawStart()

def main():
    if play == True:
        playGame()
    if canQuitOnStart == True:
        quitOnStart()

#main loop
while running:
    #get mouse position
    mousex,mousey = pygame.mouse.get_pos()
    # fps is 60
    clock.tick(120)
    # quit button event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # main function
    if __name__ == '__main__':
        main()

pygame.quit()#quits after event

运行后,pygame窗口会显示图像。关闭窗口需要尝试几次(位于右下角的'X'按钮)。

我是一名新手程序员,所以..我想要一些pygame课程来练习(请给出建议)。

谢谢!!!


我会先尝试找出哪个具体的步骤花费了这么长时间。是立即获取点击事件吗?是screen.fill((0,0,0))这一行代码花费了很长时间吗?还是pygame.display.update() - QuinnFreedman
1个回答

11
游戏运行缓慢是因为你在每一帧都加载了START_Imagepygame.image.load是一项非常昂贵的操作,因为它必须从数据存储中读取图像。应该在启动时只加载一次START_Image
surface  = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()#for fps
#caption
pygame.display.set_caption('Survival Island')

START_Image = pygame.image.load('START_Image.png').convert()

在主应用程序循环中不要多次调用 pygame.display.update()pygame.display.update() 会从队列中删除事件,因此您只能收到每个事件一次。获取事件列表 (events = pygame.event.get()) ,并将其传递给函数:

while running:
    # [...]
    events = pygame.event.get()
    for event in events:
        # [...]
        
    if play == True:
        playGame(events)
    if canQuitOnStart == True:
        quitOnStart(events)

此外,在应用程序循环中绘制场景,而不是事件循环。在绘制整个场景后,仅需要执行1个 pygame.display.update()

按钮点击条件是错误的。它必须是:

if mousex > 550 and mousey > 350 and mousex <590 and mousey <390:

if  415 < mousex < 415+70 and 190 < mousey < 190+30:

总之,我建议使用pygame.Rectcollidepoint

if pygame.Rect(415,190,70,30).collidepoint(mousex, mousey):

看例子:

#packages
import pygame
import sys
from sys import exit
#initialization
pygame.init()
#display surf
width = 600
height = 400
surface  = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()#for fps
#caption
pygame.display.set_caption('Survival Island')

START_Image = pygame.image.load('START_Image.png').convert()
    
#variables
mousex = 0
mousey = 0
#booleans
play = True #entered playmode
canQuitOnStart = True     #game can be quitted on start
drawStartScreen = True #start screen drawed
running = True # game is running
#definitions
def quitOnStart(events):     #quitting the game
    #can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(550,350,40,40))]
    global mousex,mousey,running
    for event in events:
       if event.type == pygame.MOUSEBUTTONDOWN: #quit on pressing x on start screen
            if mousex > 550 and mousey > 350 and mousex <590 and mousey <390:
                print('Exit1')
                running = False

def drawStart():      #drawing start menu
    surface.blit(START_Image,(0,0))

def playGame(events):
    #play on clicking on "play"
    global mousex,mousey,canQuitOnStart,drawStartScreen
    for event in events:
        if event.type == pygame.MOUSEBUTTONDOWN:
           if pygame.Rect(415,190,70,30).collidepoint(mousex, mousey): # can be seen if rect is drawn [pygame.draw.rect(surface,(0,0,255),(415,190,70,30))]
                canQuitOnStart = False
                drawStartScreen = False
    surface.fill((0,0,0))
    if drawStartScreen == True:
        drawStart()
    #pygame.draw.rect(surface, (255, 0, 0), (415,190,70,30))
    pygame.display.update()

def main():
    global canQuitOnStart, play, running, mousex, mousey
    #main loop
    while running:
        #get mouse position
        mousex,mousey = pygame.mouse.get_pos()
        # fps is 60
        clock.tick(120)
        # quit button event
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                running = False
        # main function
        if play == True:
            playGame(events)
        if canQuitOnStart == True:
            quitOnStart(events)

if __name__ == '__main__':
    main()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接