转换Pygame图像会出错

时间:2018-04-10 13:28:21

标签: python image pygame blit

我正在通过制作简单的游戏来学习Pygame。但是我的游戏非常迟钝,我知道这可能是因为pygame每次碰到屏幕都会转换.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,如果这可能有帮助。

1 个答案:

答案 0 :(得分:2)

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

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

您可以在实例化时将screen变量传递给Game对象,然后将其分配给self.screen