Python 酸洗负载在某些情况下不工作

Python 酸洗负载在某些情况下不工作,python,save,pygame,pickle,autosave,Python,Save,Pygame,Pickle,Autosave,我正在尝试为我的游戏制作一个自动保存功能,但是在某些情况下加载功能不起作用,我找不到原因 game.py: import pygame, sys, os, pickle pygame.init() pygame.mouse.set_visible(False) def save(): os.remove('savefile.dat') with open('savefile.dat', 'wb') as f: pickle.dump([in_room.in_room1

我正在尝试为我的游戏制作一个自动保存功能,但是在某些情况下加载功能不起作用,我找不到原因

game.py:

import pygame, sys, os, pickle
pygame.init()

pygame.mouse.set_visible(False)

def save():
    os.remove('savefile.dat')
    with open('savefile.dat', 'wb') as f:
    pickle.dump([in_room.in_room1, in_room.in_room2, var.room_x, var.room_y], f, protocol = 2)

def load():
    with open('savefile.dat', 'rb') as f:
        in_room.in_room1, in_room.in_room2, var.room_x, var.room_y = 
pickle.load(f)

class in_room():
    import menu
    in_menu = True
    in_room1 = False
    in_room2 = False

class settings():
    res_x = 1280
    res_y = 720
    FPS = 30

class win_setup():
    window = pygame.display.set_mode((settings.res_x, settings.res_y), pygame.FULLSCREEN)
    pygame.display.set_caption('The Old House 0.1')

class var():
    location = os.path.dirname(os.path.realpath(__file__))
    black = (0, 0, 0)
    d_gray = (75, 75, 75)
    l_gray = (125, 125, 125)
    white = (255, 255, 255)
    myfont = pygame.font.SysFont('monospace', 20)
    room_x = 515
    room_y = 185
    room_x_def = 515
    room_y_def = 185
    fpsClock = pygame.time.Clock()

class spr:
    logo = pygame.image.load(os.path.join(var.location, 'graphics/frostic_logo.png'))
    player = pygame.image.load(os.path.join(var.location, 'graphics/player_shadows.png'))
    player = pygame.transform.scale(player, (settings.res_x, settings.res_x)).convert_alpha()
    cursor = pygame.image.load(os.path.join(var.location, 'graphics/cursor.png')).convert_alpha()
    cursor = pygame.transform.scale2x(cursor)
    menu_bg = pygame.image.load(os.path.join(var.location, 'graphics/menu_bg.png'))
    menu_bg = pygame.transform.scale2x(menu_bg)
    newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame1.png'))
    continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue1.png'))
    exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit1.png'))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT()
            sys.exit
menu.py:

import pygame, imp, sys, os
import game
from game import in_room, settings, win_setup, var, spr
pygame.init()

sel_item = 0

while in_room.in_menu:

    # Selection:

    if sel_item == 0:
        spr.newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame2.png'))
        spr.continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue1.png'))
        spr.exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit1.png'))
    elif sel_item == 1:
        spr.newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame1.png'))
        spr.continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue2.png'))
        spr.exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit1.png'))
    elif sel_item == 2:
        spr.newgame_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_newgame1.png'))
        spr.continue_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_continue1.png'))
        spr.exit_texture = pygame.image.load(os.path.join(var.location, 'graphics/button_exit2.png'))

    newgame_button = pygame.transform.scale2x(spr.newgame_texture)
    newgame_rect = newgame_button.get_rect(center = (640, 321))
    continue_button = pygame.transform.scale2x(spr.continue_texture)
    continue_rect = continue_button.get_rect(center = (640, 429))
    exit_button = pygame.transform.scale2x(spr.exit_texture)
    exit_rect = exit_button.get_rect(center = (640, 537))

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w and sel_item > 0:
                sel_item -= 1
            elif event.key == pygame.K_s and sel_item < 2:
                sel_item += 1
            elif event.key == pygame.K_RETURN:
                if sel_item == 0:
                    in_room.in_room1 = True
                    in_room.in_menu = False
                    var.room_x = var.room_x_def
                    var.room_y = var.room_y_def
                    import room1
                    imp.reload(room1)
                elif sel_item == 1:
                    game.load()
                    if in_room.in_room1 == True:
                        import room1
                        imp.reload(room1)
                    elif in_room.in_room2 == True:
                        import room2
                        imp.reload(room2)
                elif sel_item == 2:
                    pygame.quit()
                    sys.exit()
        elif event.type == pygame.QUIT:
            pygame.QUIT()
            sys.exit()

    win_setup.window.fill(var.black)
    win_setup.window.blit(spr.menu_bg, (0, 0))
    win_setup.window.blit(newgame_button, newgame_rect)
    win_setup.window.blit(continue_button, continue_rect)
    win_setup.window.blit(exit_button, exit_rect)
    pygame.display.update()
    var.fpsClock.tick(settings.FPS)
导入pygame、imp、sys、os
进口游戏
从游戏导入到游戏室、设置、win\u设置、var、spr
pygame.init()
选择项=0
在房间中时。在菜单中:
#选择:
如果选择项==0:
spr.newgame_texture=pygame.image.load(os.path.join(var.location,'graphics/button_newgame2.png'))
spr.continue\u texture=pygame.image.load(os.path.join(var.location,'graphics/button\u continue1.png'))
spr.exit_texture=pygame.image.load(os.path.join(var.location,'graphics/button_exit1.png'))
elif sel_项==1:
spr.newgame\u texture=pygame.image.load(os.path.join(var.location,'graphics/button\u newgame1.png'))
spr.continue\u texture=pygame.image.load(os.path.join(var.location,'graphics/button\u continue2.png'))
spr.exit_texture=pygame.image.load(os.path.join(var.location,'graphics/button_exit1.png'))
elif sel_项==2:
spr.newgame\u texture=pygame.image.load(os.path.join(var.location,'graphics/button\u newgame1.png'))
spr.continue\u texture=pygame.image.load(os.path.join(var.location,'graphics/button\u continue1.png'))
spr.exit_texture=pygame.image.load(os.path.join(var.location,'graphics/button_exit2.png'))
newgame_button=pygame.transform.scale2x(spr.newgame_纹理)
newgame_rect=newgame_按钮。get_rect(中心=(640321))
continue_button=pygame.transform.scale2x(spr.continue_纹理)
continue\u rect=继续按钮。get\u rect(中心=(640429))
exit_button=pygame.transform.scale2x(spr.exit_纹理)
exit_rect=退出按钮。get_rect(中心=(640537))
对于pygame.event.get()中的事件:
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_w且选择项>0:
选择项-=1
elif event.key==pygame.K_s和sel_项<2:
选择项+=1
elif event.key==pygame.K_返回:
如果选择项==0:
in_room.in_room 1=真
in_room.in_菜单=错误
var.room_x=var.room_x_def
var.room_y=var.room_y_def
导入室1
imp.reload(房间1)
elif sel_项==1:
game.load()
如果in_room.in_room1==真:
导入室1
imp.reload(房间1)
elif in_room.in_room2==真:
导入室2
imp.reload(2号房间)
elif sel_项==2:
pygame.quit()
sys.exit()
elif event.type==pygame.QUIT:
pygame.QUIT()
sys.exit()
win_设置窗口填充(黑色变量)
win_setup.window.blit(spr.menu_bg,(0,0))
win_setup.window.blit(新游戏按钮,新游戏提示)
win_setup.window.blit(继续按钮,继续)
win_setup.window.blit(退出按钮,退出按钮)
pygame.display.update()
变量fpscall.tick(settings.FPS)
room1.py:

import pygame, imp, sys, os, math, pickle
import game
from game import in_room, settings, win_setup, var, spr
pygame.init()

last_saved = pygame.time.get_ticks()

def pos_x(room_x):
    display_pos = var.myfont.render('X: ' + str(var.room_x), 1, var.white)
    win_setup.window.blit(display_pos, (0, 0))

def pos_y(room_y):
    display_pos = var.myfont.render('Y: ' + str(var.room_y), 1, var.white)
    win_setup.window.blit(display_pos, (0, 20))

def is_room1(in_room1):
    display_room = var.myfont.render('Is in room1: ' + str(in_room.in_room1), 1, var.white)
    win_setup.window.blit(display_room, (0, 40))

def is_room2(in_room2):
    display_room = var.myfont.render('Is in room2: ' + str(in_room.in_room2), 1, var.white)
    win_setup.window.blit(display_room, (0, 60))

while in_room.in_room1:

    room = pygame.image.load(os.path.join(var.location, 'graphics/room1_bg.jpg'))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT()
            sys.exit

    # Rotation:
    mouse_pos = pygame.mouse.get_pos()
    angle = 270 - math.atan2(mouse_pos[1] - 360, mouse_pos[0] - 640) * 180 / math.pi
    player = pygame.transform.rotate(spr.player, angle).convert_alpha()
    rect = player.get_rect(center = (640, 360))

    # Movement:
    if pygame.key.get_pressed()[pygame.K_w]:
        var.room_y += 5
    elif pygame.key.get_pressed()[pygame.K_s]:
        var.room_y -= 5
    elif pygame.key.get_pressed()[pygame.K_d]:
        var.room_x -= 5
    elif pygame.key.get_pressed()[pygame.K_a]:
        var.room_x += 5
    elif pygame.key.get_pressed()[pygame.K_ESCAPE]:
        in_room.in_menu = True
        in_room.in_room1 = False
        import menu
        imp.reload(menu)

    # Collision
    if var.room_y < 125:
        var.room_y += 5
    elif var.room_y > 335 and var.room_x > 530:
        var.room_y -= 5
    elif var.room_y > 335 and var.room_x < 495:
        var.room_y -= 5
    elif var.room_x < 405:
        var.room_x += 5
    elif var.room_x > 625:
        var.room_x -= 5
    elif var.room_y < 125:
        var.room_y += 5
    elif var.room_y > 335 and var.room_x > 530:
        var.room_y -= 5
    elif var.room_y > 335 and var.room_x < 495:
        var.room_y -= 5
    elif var.room_x < 405:
        var.room_x += 5
    elif var.room_x > 625:
        var.room_x -= 5

    # Door to next room:
    if var.room_y > 345 and var.room_x < 530 and var.room_x > 495:
        in_room.in_room2 = True
        in_room.in_room1 = False
        var.room_y = 130
        import room2
        imp.reload(room2)

    # Autosave:
    now = pygame.time.get_ticks()

    if now - last_saved >= 1000:
        game.save()
        last_saved = now

    win_setup.window.fill(var.black)
    win_setup.window.blit(room, (var.room_x, var.room_y))
    win_setup.window.blit(player, rect)
    pos_x(var.room_x)
    pos_y(var.room_y)
    is_room1(in_room.in_room1)
    is_room2(in_room.in_room2)
    win_setup.window.blit(spr.cursor, mouse_pos)
    pygame.display.update()
    var.fpsClock.tick(settings.FPS)
导入pygame、imp、sys、os、math、pickle
进口游戏
从游戏导入到游戏室、设置、win\u设置、var、spr
pygame.init()
上次保存=pygame.time.get\u ticks()
def位置x(房间x):
display_pos=var.myfont.render('X:'+str(var.room_X),1,var.white)
win_setup.window.blit(显示位置,(0,0))
def位置y(房间y):
display_pos=var.myfont.render('Y:'+str(var.room_Y),1,var.white)
win_setup.window.blit(显示位置,(0,20))
def在房间1(在房间1中):
display_room=var.myfont.render('在房间1中:'+str(在房间1中),1,var.white)
win_setup.window.blit(显示室,(0,40))
def在2号房间(2号房间内):
display_room=var.myfont.render('Is in room2:')+str(in_room.in_room2),1,var.white)
win_setup.window.blit(显示室,(0,60))
在房间时。在房间1:
room=pygame.image.load(os.path.join(var.location,'graphics/room1_bg.jpg'))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.QUIT()
系统出口
#轮换:
mouse\u pos=pygame.mouse.get\u pos()
角度=270-数学值atan2(鼠标位置[1]-360,鼠标位置[0]-640)*180/数学值pi
player=pygame.transform.rotate(spr.player,angle.convert_alpha()
rect=player.get_rect(中心=(640360))
#运动:
如果pygame.key.get_按下()[pygame.K_w]:
变量室_y+=5
elif pygame.key.get_pressed()[pygame.K_s]:
变量室y-=5
elif pygame.key.get_pressed()[pygame.K_d]:
变量室_x-=5
elif pygame.key.get_pressed()[pygame.K_a]:
变量室_x+=5
elif pygame.key.get_pressed()[pygame.K_ESCAPE]:
in_room.in_菜单=真
in_room.in_room 1=错误
导入菜单
导入重新加载(菜单)
#碰撞
如果变量室y<125:
变量室_y+=5
elif变量室y>335和变量室x>530:
变量室y-=5
elif变量室y>335和变量室x<495:
变量室y-=5
elif var.room_x<405:
变量室_x+=5
elif var.room_x>625:
变量室_x-=5
elif变量室y<125:
变量室_y+=5
elif变量室y>335和变量室x>530:
变量室y-=5
elif变量室y>335和变量室x<495:
变量室y-=5
elif var.room_x<405:
变量室_x+=5
elif var.room_x>625:
变量室_x-=5
#隔壁房间的门:
如果变量室y>345,变量室x<530,变量室x>495:
in_room.in_room2=真
in_room.in_room 1=错误
变量室y=130
导入室2
imp.reload(2号房间)
#自动保存:
now=pygame.time.get_ticks()
如果现在-上次保存>=1000:
game.save()
上次保存=现在
win_设置窗口填充(黑色变量)
win_setup.windo