gpt4 book ai didi

python - 转换 Pygame 图像出现错误

转载 作者:行者123 更新时间:2023-12-01 02:00:12 25 4
gpt4 key购买 nike

我正在通过用 Pygame 制作简单的游戏来学习 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,如果这有帮助的话。

最佳答案

您必须调用pygame.display.set_mode在调用 convertconvert_alpha 方法之前。所以我只需将此行移至程序顶部:

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

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

关于python - 转换 Pygame 图像出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49754923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com