Pygame загружается медленно

У меня есть простой проект, в котором отображается кнопка с изображением, текстом и цветом фона:

import os os.environ[‘PYGAME_HIDE_SUPPORT_PROMPT’] = «hide» import pygame, sys from pygame.locals import * import requests import io BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) GRAY = (200, 200, 200) class Main_window: def __init__(self): pygame.init() self.screen = pygame.display.set_mode((800, 600)) def draw(self): Btn1 = Button(0,0,200,200,»hello»,color=RED, text=»test»,text_color=BLACK) def mainLoop(self): done = False while not done: eventlist = pygame.event.get() for ev in eventlist: if ev.type == QUIT: done = True if ev.type == MOUSEBUTTONDOWN: Button.getButton(ev) pygame.quit() class Button: Buttons = [] def __init__(self, left, top, w, h,function,color=RED, text=»», text_color = BLACK): self.left = left self.top = top self.w = w self.h = h self.right = self.left+w self.bottom = self.top+h self.function = function surf1 = pygame.Surface((w, h)) surf1.fill(color) rect1 = pygame.Rect(left, top, w, h) main.screen.blit(surf1, rect1) Button.Buttons.append(self) if text != «»: font1 = pygame.font.SysFont(‘chalkduster.ttf’, 72) text1 = font1.render(text, True, text_color) text_rect = text1.get_rect(center=(int(w/2), int(h/2))) main.screen.blit(text1, text_rect) image_url = «https://image.flaticon.com/icons/png/512/31/31990.png» r = requests.get(image_url) img = io.BytesIO(r.content) image = pygame.image.load(img) image = pygame.transform.scale(image, (w, h)) main.screen.blit(image, (0, 0)) pygame.display.flip() def getButton(event): for i in Button.Buttons: x, y = event.pos if x>=i.left and x<=i.right and y<=i.bottom and y>=i.top: eval(i.function+»()») def hello(): print(«hello») main = Main_window() main.draw() main.mainLoop()

это работает нормально, но проблема в том, что когда я запускаю игру, она загружает главное окно, подождите некоторое время (что-то вроде 1 секунды), а затем загрузите кнопку. Я попытался добавить еще кнопку, и она загружала их по одной. не понимаю почему.

Вы загружаете изображение из Интернета. Попробуйте использовать образ на диске.   —  person yonis lacorte    schedule 18.02.2021

… и загружать изображение не для каждого экземпляра кнопки, а один раз при инициализации.   —  person yonis lacorte    schedule 18.02.2021

Источник: ledsshop.ru

Стиль жизни - Здоровье!