我正在尝试为我的游戏制作自动保存功能,但在某些情况下,加载功能不起作用,我找不到原因。
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)
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)
我添加了(几乎)整个代码,因此不会丢失任何重要信息。文件room2.py与room1.py基本相同,所以我没有添加。
这个问题已经有了答案,但我找不到了。
另外,如果你注意到别的东西我可以做得更好,请随意说出来。