如何解决如何使用pickle将图像和变量保存到文件中?
我正在尝试在pygame中保存用户配置。
当用户退出游戏时,它应该使用pickle模块保存一些信息,例如主题(怪异,友好等)和得分。当再次打开它时,以前保存的内容将加载到游戏中。
例如,我们有这堆代码。
background = pygame.image.load('image full path')
score = 530
# Upon quitting
with open('GameSave','wb') as file:
pickle.dump([bakcground,score],file)
# Upon opening
with open('GameSave','rb') as file:
background = pickle.load(file)
score = pickle.load(file)
但是最终,我在转储时得到了这个:
TypeError: can't pickle pygame.Surface objects
所以我认为我必须使用pygame.image.tostring
问题是加载后如何将其转换回图像。
以及如何确保我们仅加载background
变量的背景,并且仅加载score
变量的分数。
解决方法
假设您有合理的必要将背景图像复制到保存文件中,则很容易将图像的“字符串”及其大小进行腌制:
// <client> is a placeholder for the Discord.Client() object
const settings = require('./settings.json')
<client>.on('message',(message) => {
var slowed;
if (slowed) return;
slowed = true
setTimeout(() => {
slowed = false
},settings.slow) // only make slowed falsey after specified amount of time
然后再次取消腌渍时,将其转换回图像:
image_string = pygame.image.tostring( background,'RGB' )
image_size = ( background.width,background.height )
# TODO - pickle
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。