栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Pygame运行缓慢

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pygame运行缓慢

游戏运行缓慢,因为您

START_Image
在每一帧中都加载。
pygame.image.load
这是非常昂贵的操作,因为它必须从数据存储中读取图像。
START_Image
在启动时加载一次

surface  = pygame.display.set_mode((width,height))clock = pygame.time.Clock()#for fps#captionpygame.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)

此外,在应用程序循环中绘制场景,而不是事件循环中。

pygame.display.update()
绘制整个场景后单做1个就足够了。

按钮单击条件错误。它一定要是:

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

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

无论如何,我建议使用

pygame.Rect
collidepoint

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

参见示例:

#packagesimport pygameimport sysfrom sys import exit#initializationpygame.init()#display surfwidth = 600height = 400surface  = pygame.display.set_mode((width,height))clock = pygame.time.Clock()#for fps#captionpygame.display.set_caption('Survival Island')START_Image = pygame.image.load('START_Image.png').convert()#variablesmousex = 0mousey = 0#booleansplay = True #entered playmodecanQuitonStart = True     #game can be quitted on startdrawStartScreen = True #start screen drawedrunning = True # game is running#definitionsdef 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 = Falsedef 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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647885.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号