转换 Pygame 图片报错

Converting Pygame image gives an error

我正在通过使用它制作简单的游戏来学习 Pygame。但是我的游戏非常卡顿,我了解到这可能是因为 pygame 每次 blit 到屏幕时都会转换 .png 图像。

所以我尝试使用convert() 来加载图像。但它给了我一个错误。进行了一些研究,但即使我已经测试了一些,也没有答案可以帮助我。

我的代码:

import pygame
import os, random


class Item:
    def __init__(self):

        self.image = Context.load_image("item.png")

        self.X = random.randint(0, Context.resolution[1] - 200) 
        self.Y = 0

        self.dY = 10


class Basket:
    def __init__(self):
        self.image = Context.load_image("catcher.png")

        self.X = Context.resolution[0] / 2
        self.Y = Context.resolution[1] - 200    

        self.vel = 30.
        self.dX  = self.dY = 0

def action(self, event):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_a:
            self.dX = -self.vel
        elif event.key == pygame.K_d:
            self.dX = self.vel

    elif event.type == pygame.KEYUP:
        self.dX = 0


class Context:
    resolution      = (1600, 900)

    dir_resources   = "resources/"

    def __init__(self):
        self.fps = 60
        self.background_theme = self.load_image("background.png")

    @staticmethod
    def load_image(image_name):
        path = Context.dir_resources + image_name
        # ERROR HAPPENS HERE! If I take ".convert()" off, the game runs but laggy
        return pygame.image.load(os.path.join(path)).convert() 


class Game:
    score = 0

def __init__(self):
    self.game_on = True

    self.context = Context()
    self.basket  = Basket()

    self.screen = pygame.display.set_mode(self.context.resolution, pygame.HWSURFACE | pygame.DOUBLEBUF)
    self.fps    = self.context.fps
    self.clock  = pygame.time.Clock()

    self.tshirt = Item()

def event_loop(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            self.game_on = False

        self.basket.action(event)

def update(self):   
    self.basket.X = self.basket.X + self.basket.dX
    self.tshirt.Y = self.tshirt.Y + self.tshirt.dY

def draw(self):
    self.screen.blit(self.context.background_theme, (0, 0))

    self.screen.blit(self.basket.image, (self.basket.X, self.basket.Y))
    self.screen.blit(self.tshirt.image, (self.tshirt.X, self.tshirt.Y))

    pygame.display.flip()

def run(self):
    while self.game_on:
        self.event_loop()
        self.update()
        self.draw()

        pygame.display.update()
        self.clock.tick(self.fps)

def main():
    pygame.init()

    game = Game()
    game.run()

    pygame.quit()

if __name__ == '__main__':
    main()

这里是错误:

Traceback (most recent call last):
  File "/me/tmvaz/PycharmProjects/Game/__main__.py", line 105, in <module>
    main()
  File "/home/me/PycharmProjects/Game/__main__.py", line 98, in main
    game = Game()
  File "/home/me/PycharmProjects/Game/__main__.py", line 57, in __init__
    self.context = Context()
  File "/home/me/PycharmProjects/Game/__main__.py", line 42, in __init__
    self.background_theme = self.load_image("background.png")
  File "/home/me/PycharmProjects/Game/__main__.py", line 47, in load_image
    return pygame.image.load(os.path.join(path)).convert()
pygame.error: No video mode has been set

Process finished with exit code 1

我正在使用 Linux,Lubuntu,如果有帮助的话。

您必须在调用 convertconvert_alpha 方法之前调用 pygame.display.set_mode。所以我只是将这一行移到程序的顶部:

pygame.init()
screen = pygame.display.set_mode(resolution, pygame.HWSURFACE | pygame.DOUBLEBUF)

可以在实例化Game对象时将screen变量传递给它,然后赋值给self.screen