Pygame 运行缓慢 [英] Pygame is running slow

查看:127
本文介绍了Pygame 运行缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个名为生存岛"的游戏,并且刚刚创建了开始屏幕.pygame 在执行一个事件后滞后太多(需要时间来响应).

I am creating a game called "Survival Island" and have just created the start screen. The pygame lags too much after doing an event (takes time to respond).

这是我的源代码:

#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')

After running, The pygame window displays the image. It takes several tries to close the window after running(the 'X' on the bottom right corner)

我是一个新手程序员,所以..我想要一些 pygame 课程(请推荐一些).

I am a newbie programmer so.. I want some pygame courses to do (please suggest some).

谢谢!!!

推荐答案

游戏运行缓慢,因为您在每一帧中都加载了 START_Image.pygame.image.load 是一项非常昂贵的操作,因为它必须从数据存储中读取图像.启动时加载一次 START_Image

The game is running slow because you load the START_Image in every frame. pygame.image.load is a very expensive operation, because it has to read the images from the data store. Load START_Image once at startup

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())并将事件列表传递给函数:

Do not call pygame.display.update() more than once in the main application loop. pygame.display.update() removes the events from the queue, thus you'll get each event just once. Get the list of events once (events = pygame.event.get()) and pass the list of events to the functions:

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

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

Further more, draw the scene in the application loop, rather the event loop. It is sufficient to do 1 single pygame.display.update() after drawing the entire scene.

按钮点击条件错误.必须是:

The button click condition is wrong. It has to be:

if mousex >550 和老鼠 >350 和 mousex <590 和 mousey <390:

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

无论如何我建议使用 pygame.Rectcollidepoint:

Anyway I recommend to use pygame.Rect and collidepoint:

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()

这篇关于Pygame 运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆