Browse Source

Merge branch 'JSON-implementation'

Conflicts:
	Run.py
pull/19/head 0.3.3.1-alpha.1
Max Lange 7 years ago
parent
commit
ae5f17ba9e
  1. 3
      .gitignore
  2. 2
      Run.py
  3. 11
      saves/Debug.ini
  4. BIN
      saves/Debug/1/background.tga
  5. 65
      saves/Debug/1/world.json
  6. BIN
      saves/Debug/2/background.tga
  7. 65
      saves/Debug/2/world.json
  8. BIN
      saves/Debug/3/background.tga
  9. 65
      saves/Debug/3/world.json
  10. BIN
      saves/Debug/4/background.tga
  11. 65
      saves/Debug/4/world.json
  12. BIN
      saves/Debug/5/background.tga
  13. 65
      saves/Debug/5/world.json
  14. BIN
      saves/Debug/6/background.tga
  15. 65
      saves/Debug/6/world.json
  16. BIN
      saves/Debug/7/background.tga
  17. 65
      saves/Debug/7/world.json
  18. BIN
      saves/Debug/8/background.tga
  19. 65
      saves/Debug/8/world.json
  20. 22
      saves/Debug/Data.json
  21. 11
      saves/default.ini
  22. 101
      src/game_data.py
  23. 12
      src/menu.py
  24. 25
      src/objects.py
  25. 11
      src/player.py
  26. 122
      src/settings.py
  27. 14
      src/tests/json_saving.py
  28. 1
      src/tests/test1.json

3
.gitignore vendored

@ -12,3 +12,6 @@
#empty folders
screenshots/*
assets/sprites/player/*
#symlinks in testing folder
src/tests/settings.py

2
Run.py

@ -25,7 +25,7 @@ try:
break
if int(required[index]) > int(version[index]):
raise SystemExit("Outdated Python version: " + ".".join(version))
if sys.version[:5] >= "3.0.0":
if sys.version[:1] >= "3":
raise SystemExit("No support for Python3")
# Run the game

11
saves/Debug.ini

@ -1,11 +0,0 @@
[main]
fullscreen = False
screenx_current = 400
screeny_current = 300
debug = True
skip = True
posx = 0.949870958743
posy = 0.92354835789
volume = 0.0
highscore = 100000000

BIN
saves/Debug/1/background.tga

Binary file not shown.

65
saves/Debug/1/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.5962254846146301,
"anchorx": 11,
"x_pos": 0.006538285402270971,
"anchory": 529
},
"targets": [
{
"pos_x": 192.74511131686182,
"pos_y": 407.3450649917651,
"pos_xper": 0.10198154037929197,
"chooser": true,
"kill_entity": false,
"timer": 14911,
"pos_yper": 0.38794768094453824,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1738.1894050130202,
"pos_y": 14.265979294817999,
"pos_xper": 0.9196769338693228,
"chooser": true,
"kill_entity": false,
"timer": 15312,
"pos_yper": 0.013586646947445713,
"inscreen": false,
"gothit": false
},
{
"pos_x": 1645.4088153398757,
"pos_y": 995.0051259388391,
"pos_xper": 0.870586674783003,
"chooser": true,
"kill_entity": false,
"timer": 15428,
"pos_yper": 0.9476239294655611,
"inscreen": false,
"gothit": false
},
{
"pos_x": 859.2797757716456,
"pos_y": 41.383825279029026,
"pos_xper": 0.4546453840061617,
"chooser": true,
"kill_entity": false,
"timer": 14805,
"pos_yper": 0.039413166932408594,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1819.875314880344,
"pos_y": 762.3059975367354,
"pos_xper": 0.962896992000182,
"chooser": true,
"kill_entity": false,
"timer": 15499,
"pos_yper": 0.7260057119397479,
"inscreen": false,
"gothit": false
}
]
}

BIN
saves/Debug/2/background.tga

Binary file not shown.

65
saves/Debug/2/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.8627741670853752,
"anchorx": 746,
"x_pos": 0.4322113114160422,
"anchory": 766
},
"targets": [
{
"pos_x": 1259.9748124126409,
"pos_y": 226.10202156313136,
"pos_xper": 0.6666533399008682,
"chooser": false,
"kill_entity": false,
"timer": 5045,
"pos_yper": 0.21533525863155367,
"inscreen": true,
"gothit": false
},
{
"pos_x": 602.0126361333652,
"pos_y": 389.6558162913776,
"pos_xper": 0.31852520430336784,
"chooser": false,
"kill_entity": false,
"timer": 4574,
"pos_yper": 0.37110077742035963,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1208.361751848981,
"pos_y": 306.0158401188983,
"pos_xper": 0.639344842248138,
"chooser": false,
"kill_entity": false,
"timer": 4630,
"pos_yper": 0.29144365725609367,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1500.5480610012135,
"pos_y": 887.4977389881259,
"pos_xper": 0.7939407730165151,
"chooser": false,
"kill_entity": false,
"timer": 4719,
"pos_yper": 0.8452359418934532,
"inscreen": true,
"gothit": false
},
{
"pos_x": 564.6367778268188,
"pos_y": 431.7535796072261,
"pos_xper": 0.29874961789778776,
"chooser": false,
"kill_entity": false,
"timer": 5099,
"pos_yper": 0.4111938853402153,
"inscreen": true,
"gothit": false
}
]
}

BIN
saves/Debug/3/background.tga

Binary file not shown.

65
saves/Debug/3/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.2952987968515459,
"anchorx": 533,
"x_pos": 0.3086092946001434,
"anchory": 262
},
"targets": [
{
"pos_x": 840.6045525273637,
"pos_y": 12.057419901778498,
"pos_xper": 0.44476431350654166,
"chooser": false,
"kill_entity": false,
"timer": 4959,
"pos_yper": 0.011483257049312856,
"inscreen": true,
"gothit": false
},
{
"pos_x": 536.6140365890209,
"pos_y": 95.99947693935353,
"pos_xper": 0.28392277068202165,
"chooser": false,
"kill_entity": false,
"timer": 4647,
"pos_yper": 0.09142807327557478,
"inscreen": true,
"gothit": false
},
{
"pos_x": 541.9550874678257,
"pos_y": 146.14800723754666,
"pos_xper": 0.2867487235279501,
"chooser": false,
"kill_entity": false,
"timer": 5023,
"pos_yper": 0.13918857832147302,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1272.576935766192,
"pos_y": 945.4241725790133,
"pos_xper": 0.6733211300350223,
"chooser": false,
"kill_entity": false,
"timer": 5422,
"pos_yper": 0.9004039738847746,
"inscreen": true,
"gothit": false
},
{
"pos_x": 108.3811500600481,
"pos_y": 342.4810167313304,
"pos_xper": 0.05734452384129529,
"chooser": false,
"kill_entity": false,
"timer": 4845,
"pos_yper": 0.3261723968869813,
"inscreen": true,
"gothit": false
}
]
}

BIN
saves/Debug/4/background.tga

Binary file not shown.

65
saves/Debug/4/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.361199561712627,
"anchorx": 1652,
"x_pos": 0.956154072281788,
"anchory": 320
},
"targets": [
{
"pos_x": 1447.9339766840014,
"pos_y": 288.9959132420225,
"pos_xper": 0.7661026331661384,
"chooser": false,
"kill_entity": false,
"timer": 5722,
"pos_yper": 0.27523420308764046,
"inscreen": true,
"gothit": false
},
{
"pos_x": 225.9615263071484,
"pos_y": 320.1086937542959,
"pos_xper": 0.11955636312547535,
"chooser": false,
"kill_entity": false,
"timer": 5160,
"pos_yper": 0.304865422623139,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1590.4848829568587,
"pos_y": 1003.5391978646514,
"pos_xper": 0.841526393098867,
"chooser": false,
"kill_entity": false,
"timer": 5030,
"pos_yper": 0.9557516170139537,
"inscreen": true,
"gothit": false
},
{
"pos_x": 983.3067910946158,
"pos_y": 995.804242316038,
"pos_xper": 0.5202681434363047,
"chooser": false,
"kill_entity": false,
"timer": 5283,
"pos_yper": 0.9483849926819409,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1606.1446524754251,
"pos_y": 785.4828029960273,
"pos_xper": 0.8498119854367329,
"chooser": false,
"kill_entity": false,
"timer": 5662,
"pos_yper": 0.7480788599962165,
"inscreen": true,
"gothit": false
}
]
}

BIN
saves/Debug/5/background.tga

Binary file not shown.

65
saves/Debug/5/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.5116669149099174,
"anchorx": 1371,
"x_pos": 0.7937797066355374,
"anchory": 454
},
"targets": [
{
"pos_x": 514.8885009269158,
"pos_y": 312.3226966005269,
"pos_xper": 0.2724277782682094,
"chooser": false,
"kill_entity": false,
"timer": 5764,
"pos_yper": 0.29745018723859706,
"inscreen": true,
"gothit": false
},
{
"pos_x": 604.51501858859,
"pos_y": 627.0156772463722,
"pos_xper": 0.3198492161844392,
"chooser": false,
"kill_entity": false,
"timer": 5730,
"pos_yper": 0.5971577878536878,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1175.3960878813232,
"pos_y": 386.5673368281188,
"pos_xper": 0.6219026920007001,
"chooser": false,
"kill_entity": false,
"timer": 5584,
"pos_yper": 0.3681593684077322,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1641.0001263435406,
"pos_y": 132.69347242285403,
"pos_xper": 0.8682540351024024,
"chooser": false,
"kill_entity": false,
"timer": 5939,
"pos_yper": 0.12637473564081336,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1498.9513532837843,
"pos_y": 808.8019959349243,
"pos_xper": 0.7930959541184044,
"chooser": false,
"kill_entity": false,
"timer": 5961,
"pos_yper": 0.7702876151761184,
"inscreen": true,
"gothit": false
}
]
}

BIN
saves/Debug/6/background.tga

Binary file not shown.

65
saves/Debug/6/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.3967364832899276,
"anchorx": 369,
"x_pos": 0.21363821939184624,
"anchory": 352
},
"targets": [
{
"pos_x": 159.0656166298992,
"pos_y": 879.5347783082478,
"pos_xper": 0.08416170192058159,
"chooser": false,
"kill_entity": false,
"timer": 5927,
"pos_yper": 0.8376521698173789,
"inscreen": true,
"gothit": false
},
{
"pos_x": 757.3136827424116,
"pos_y": 195.6357823642085,
"pos_xper": 0.40069507023408024,
"chooser": false,
"kill_entity": false,
"timer": 5767,
"pos_yper": 0.18631979272781762,
"inscreen": true,
"gothit": false
},
{
"pos_x": 580.7327590366116,
"pos_y": 76.09238571139083,
"pos_xper": 0.30726601007228127,
"chooser": false,
"kill_entity": false,
"timer": 5543,
"pos_yper": 0.07246893877275318,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1212.031097336783,
"pos_y": 979.8536230270848,
"pos_xper": 0.6412862948871868,
"chooser": false,
"kill_entity": false,
"timer": 5948,
"pos_yper": 0.9331939266924617,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1096.5099778715178,
"pos_y": 580.2104399968944,
"pos_xper": 0.5801640094558295,
"chooser": false,
"kill_entity": false,
"timer": 5945,
"pos_yper": 0.5525813714256137,
"inscreen": true,
"gothit": false
}
]
}

BIN
saves/Debug/7/background.tga

Binary file not shown.

65
saves/Debug/7/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.40654746791753915,
"anchorx": 94,
"x_pos": 0.0549293458595046,
"anchory": 361
},
"targets": [
{
"pos_x": 927.0818722279864,
"pos_y": 779.9144870262267,
"pos_xper": 0.4905195091153367,
"chooser": false,
"kill_entity": false,
"timer": 6011,
"pos_yper": 0.7427757019297396,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1206.2244352228277,
"pos_y": 774.6113945729289,
"pos_xper": 0.638213986890385,
"chooser": false,
"kill_entity": false,
"timer": 5632,
"pos_yper": 0.7377251376885038,
"inscreen": true,
"gothit": false
},
{
"pos_x": 188.9393402412415,
"pos_y": 789.2248537266288,
"pos_xper": 0.09996790488954577,
"chooser": false,
"kill_entity": false,
"timer": 6493,
"pos_yper": 0.7516427178348846,
"inscreen": true,
"gothit": false
},
{
"pos_x": 631.1595833395236,
"pos_y": 664.9189251597509,
"pos_xper": 0.3339468694918114,
"chooser": false,
"kill_entity": false,
"timer": 6006,
"pos_yper": 0.6332561191997628,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1212.1081119771238,
"pos_y": 442.29525215227346,
"pos_xper": 0.6413270433741396,
"chooser": false,
"kill_entity": false,
"timer": 6097,
"pos_yper": 0.42123357347835566,
"inscreen": true,
"gothit": false
}
]
}

BIN
saves/Debug/8/background.tga

Binary file not shown.

65
saves/Debug/8/world.json

@ -0,0 +1,65 @@
{
"station": {
"y_pos": 0.16604465850019,
"anchorx": 379,
"x_pos": 0.21964365720257617,
"anchory": 147
},
"targets": [
{
"pos_x": 1231.70200638317,
"pos_y": 56.951469933928635,
"pos_xper": 0.6516941832715185,
"chooser": false,
"kill_entity": false,
"timer": 6546,
"pos_yper": 0.05423949517517013,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1317.4750649732946,
"pos_y": 218.8232681610952,
"pos_xper": 0.6970767539541242,
"chooser": false,
"kill_entity": false,
"timer": 6171,
"pos_yper": 0.20840311253437638,
"inscreen": true,
"gothit": false
},
{
"pos_x": 737.9918577243556,
"pos_y": 251.4583990277367,
"pos_xper": 0.3904718823938389,
"chooser": false,
"kill_entity": false,
"timer": 6284,
"pos_yper": 0.23948418955022543,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1265.658474323737,
"pos_y": 278.5373064695809,
"pos_xper": 0.6696605684252577,
"chooser": false,
"kill_entity": false,
"timer": 5945,
"pos_yper": 0.2652736252091247,
"inscreen": true,
"gothit": false
},
{
"pos_x": 1630.903697420273,
"pos_y": 149.5596426406981,
"pos_xper": 0.8629120092170756,
"chooser": false,
"kill_entity": false,
"timer": 6218,
"pos_yper": 0.14243775489590293,
"inscreen": true,
"gothit": false
}
]
}

22
saves/Debug/Data.json

@ -0,0 +1,22 @@
{
"world.name": "1",
"worlds": [
"1",
"3",
"2",
"5",
"4",
"7",
"6",
"8"
],
"player.rel_y": 0.07938996323793225,
"fullscreen": false,
"debugscreen": false,
"screeny_current": 500,
"player.rel_x": 0.004415121093473239,
"screenx_current": 500,
"player.timeplay": 6887,
"debugmode": true,
"sounds.music.volume": 0.0
}

11
saves/default.ini

@ -1,11 +0,0 @@
[main]
fullscreen = False
screenx_current = 960
screeny_current = 540
debug = True
skip = True
posx = 283
posy = 497
volume = 0.5
debugscreen = False
debugmode = False

101
src/game_data.py

@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
import json
import os
import pygame
import sys
import traceback
from . import settings
from . import sounds
from . import objects
def save(name):
name = name.encode("utf-8")
# removes invalid characters
if "/" in name:
name = name.replace("/", "\\")
if "%" in name:
name = name.replace("%", "")
if not os.path.isdir("./saves/%s/" % name):
os.makedirs("./saves/%s/" % name)
all_world_data = {}
world_image = {}
for world_name in settings.localmap:
if not os.path.isdir("./saves/%s/%s" % (name, world_name)):
os.makedirs("./saves/%s/%s" % (name, world_name))
all_world_data[world_name] = {}
all_world_data[world_name]["targets"] = list()
all_world_data[world_name]["station"] = dict()
for target in settings.localmap[world_name].targets:
all_world_data[world_name]["targets"].append(target.unique_relevant_data())
station_data = settings.localmap[world_name].warp1.unique_relevant_data()
all_world_data[world_name]["station"] = station_data
world_image[world_name] = settings.localmap[world_name].background
data = {"fullscreen": settings.fullscreen,
"screenx_current": settings.screenx_current,
"screeny_current": settings.screeny_current,
"debugmode": settings.debugmode,
"debugscreen": settings.debugscreen,
"player.rel_x": settings.player.rel_x,
"player.rel_y": settings.player.rel_y,
"sounds.music.volume": sounds.music.volume,
"player.timeplay": settings.player.timeplay,
"world.name": settings.world.name,
"worlds": list(settings.localmap.keys())
}
settings_file = open("./saves/" + name + "/Data.json", "w")
for world_name in settings.localmap:
world_file = open("./saves/%s/%s/world.json" % (name, world_name), "w")
pygame.image.save(world_image[world_name],
"./saves/%s/%s/background.tga" % (name, world_name))
json.dump(all_world_data[world_name], world_file, indent=12)
json.dump(data, settings_file, indent=12)
def load(name):
"""Load savegame"""
try:
data = json.load(open("./saves/" + unicode(name) + "/Data.json", "r"))
settings.fullscreen = data["fullscreen"]
settings.screenx_current = data["screenx_current"]
settings.screeny_current = data["screeny_current"]
settings.debugmode = data["debugmode"]
settings.debugscreen = data["debugscreen"]
settings.player.rel_x = data["player.rel_x"]
settings.player.rel_y = data["player.rel_y"]
sounds.music.volume = data["sounds.music.volume"]
settings.player.timeplay = data["player.timeplay"]
world_names = data["worlds"]
from . import worlds
localmap = {}
for world_name in world_names:
background_dir = "./saves/%s/%s/background.tga" % (name, world_name)
background = pygame.image.load(background_dir)
world_data = json.load(open("./saves/%s/%s/world.json"
% (name, world_name), "r"))
localmap[world_name] = worlds.world(world_name)
localmap[world_name].generate(background, settings.dstars, 0)
for target_data in world_data["targets"]:
tmp_target = objects.target()
tmp_target.pos_xper = target_data["pos_xper"]
tmp_target.pos_yper = target_data["pos_yper"]
tmp_target.timer = target_data["timer"]
tmp_target.update()
localmap[world_name].targets.append(tmp_target)
tmp_station = objects.warp_station()
tmp_station.x_pos = world_data["station"]["x_pos"]
tmp_station.y_pos = world_data["station"]["y_pos"]
tmp_station.update()
localmap[world_name].warp1 = tmp_station
settings.localmap = localmap
settings.world = localmap[data["world.name"]]
except Exception:
print(("Unexpected error:", sys.exc_info()[0]))
print((traceback.format_exc()))

12
src/menu.py

@ -3,6 +3,7 @@ from . import settings
from . import namings
from . import sounds
from . import missions
from . import game_data
from libs.pyganim import pyganim
import pygame
from libs import menu
@ -209,7 +210,8 @@ def main():
if event == "Load Game":
savegame = savegames()
if savegame is not None:
settings.load(savegame)
game_data.load(savegame)
settings.player.move_rel()
sounds.music.play("next", 0)
settings.upd("get_saves")
settings.upd("adjust_screen")
@ -252,12 +254,12 @@ def pause():
settings.screeny_current / 2,
"Save Game")
if savename != "Exit":
settings.save(savename)
game_data.save(savename)
settings.upd("get_saves")
if event == "Load Game":
savegame = savegames()
if savegame is not None:
settings.load(savegame)
game_data.load(savegame)
sounds.music.play("unpause")
settings.upd("get_saves")
settings.upd("adjust_screen")
@ -402,6 +404,10 @@ def savegames():
D_saves = len(list_of_saves)
currently_selected = 0
if len(list_of_saves) == 0:
print(("No current savegames"))
return None
# Defines Menu
settings_menu = menu_template("load", 0, 255, 255,
{"savename": list_of_saves[currently_selected]},

25
src/objects.py

@ -3,7 +3,6 @@ import pygame
import random
import math
from . import settings
from . import menu
from pygame.locals import *
"""Classes for creating objects"""
@ -173,6 +172,8 @@ class target():
self.pos.size = self.explosion.getRect().size
self.explosion.play()
self.gothit = True
while self.explosion.state in ["stopped", "paused"]:
self.explosion.play()
def blit(self):
"""Blits target and explosion"""
@ -193,6 +194,13 @@ class target():
return True
return False
def unique_relevant_data(self):
data = {}
data["timer"] = self.timer
data["pos_yper"] = self.pos_yper
data["pos_xper"] = self.pos_xper
return data
class warp_station():
@ -239,6 +247,7 @@ class warp_station():
return test
if test_collide():
# Warps to the selected world and gets a bit pushed off the station
from . import menu
selected_num = menu.choose_world()
if selected_num >= 0:
settings.world = settings.localmap[selected_num]
@ -253,14 +262,20 @@ class warp_station():
settings.right = False
while test_collide():
if settings.player.pos.center[0] < self.pos.center[0]:
settings.player.move_ip(-20, 0)
settings.player.move_abs(-20, 0)
else:
settings.player.move_ip(20, 0)
settings.player.move_abs(20, 0)
if settings.player.pos.center[1] < self.pos.center[1]:
settings.player.move_ip(0, -20)
settings.player.move_abs(0, -20)
else:
settings.player.move_ip(0, 20)
settings.player.move_abs(0, 20)
playerpos = settings.player.pos
def blit(self):
self.screen.blit(self.img, self.pos)
def unique_relevant_data(self):
data = {}
data["y_pos"] = self.y_pos
data["x_pos"] = self.x_pos
return data

11
src/player.py

@ -72,7 +72,7 @@ class player():
if self.rotation == 315:
self.img = self.playeruple
def move_ip(self, addx, addy):
def move_abs(self, addx, addy):
#lint:disable
self.rel_x = (self.pos.x + addx) / float(settings.screenx_current)
self.rel_y = (self.pos.y + addy) / float(settings.screeny_current)
@ -80,6 +80,15 @@ class player():
self.pos.left = int(self.rel_x * settings.screenx_current)
#lint:enable
def move_rel(self, *args):
if len(args):
self.rel_x = args[0]
self.rel_y = args[1]
#lint:disable
self.pos.top = int(self.rel_y * settings.screeny_current)
self.pos.left = int(self.rel_x * settings.screenx_current)
#lint:enable
def move(self):
"""Handle the movement and collisions"""
#lint:disable

122
src/settings.py

@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
from ConfigParser import SafeConfigParser
from libs.pyganim import pyganim
import os
import shutil
import sys
import traceback
import random
@ -48,7 +46,6 @@ def init():
global musics # the list of music titles assoziated wih the music files
global saves # all savegames
global psycomode # if psycomode is turned on
global current_game # the current savegame and default when no game is saved
global explosions # list of surfs of explosions
global explosions_disp # list of showing explosions
global run # boolean for main loop
@ -101,7 +98,7 @@ def init():
typeface = "monospace"
stdfont = pygame.font.SysFont(typeface, 15)
version = "0.3.3"
version = "0.3.3.1 dev"
up = False
down = False
left = False
@ -120,7 +117,6 @@ def init():
fullscreenold = False
fake_size = 8 / 7.0
psycomode = False
current_game = "default"
run = True
dtargets = 5
morevents = []
@ -160,11 +156,7 @@ def init():
explosion_attr = [(anim_file, 40) for anim_file in explosion_files]
explosions.append(pyganim.PygAnimation(explosion_attr, loop=False))
saves = []
for filename in os.listdir("./saves"):
if filename.endswith(".ini"):
filename = filename[:-4].decode("utf-8")
saves.append(filename)
upd("get_saves")
if fullscreen:
screen = pygame.display.set_mode(
@ -237,25 +229,22 @@ def upd(level):
if level == "get_saves":
global saves
saves = []
for filename in os.listdir("./saves"):
if filename.endswith(".ini"):
filename = filename[:-4]
if filename not in ("default"):
saves.append(filename)
for elem in os.listdir("./saves/"):
if os.path.isdir("./saves/" + elem):
saves.append(elem)
return
if level == "adjust_screen":
global background
global background_pos
global konstspeed
#FIXME: fullscreenold is redundant
global fullscreenold
global fullscreen
if fullscreenold != fullscreen:
if fullscreen:
pygame.display.set_mode((screenx, screeny), pygame.FULLSCREEN)
if not fullscreen:
pygame.display.set_mode((screenx / 2, screeny / 2))
fullscreenold = fullscreen
if fullscreen:
pygame.display.set_mode((screenx, screeny), pygame.FULLSCREEN)
if not fullscreen:
pygame.display.set_mode((screenx_current, screeny_current))
upd("screenvalues")
@ -290,97 +279,6 @@ def toggle(var, option1, option2):
return var
class save():
def __init__(self, name):
"""create a new savegame"""
global saves
global current_game
name = name.encode("utf-8")
self.name = name
upd("get_saves")
if len(saves) >= 50:
return False
# removes invalid characters
if "/" in name:
name = name.replace("/", "\\")
if "%" in name:
name = name.replace("%", "")
current_game = name
# handles the configparser object
self.config = SafeConfigParser()
self.config.read("./saves/" + name + ".ini")
if not os.path.isfile("./saves/" + name + ".ini"):
self.config.add_section("main")
# sets values
self.config.set("main", "fullscreen", str(fullscreen))
self.config.set("main", "screenx_current", str(screenx_current))
self.config.set("main", "screeny_current", str(screeny_current))
self.config.set("main", "debugscreen", str(debugscreen))
self.config.set("main", "debugmode", str(debugmode))
self.config.set("main", "skip", "True")
self.config.set("main", "posx", str(player.pos.x))
self.config.set("main", "posy", str(player.pos.y))
self.config.set("main", "volume", str(volume))
# and writes them
with open("./saves/" + name + ".ini", "w") as tmp:
self.config.write(tmp)
def load(name):
"""Load savegame"""
global fullscreen
global screenx_current
global screeny_current
global debugscreen
global debugmode
global config
global skip
global pos_x
global pos_y
global volume
global saves
global screen
upd("get_saves")
config = SafeConfigParser()
for a in saves:
if a == name.encode("utf-8"):
config.read("./saves/" + a + ".ini")
if not (saves == []):
# tries to load and returns values in terminal that couldnt be loaded
import ConfigParser
try:
from . import sounds
#lint:disable
fullscreen = config.getboolean("main", "fullscreen")
screenx_current = int(config.getfloat("main", "screenx_current"))
screeny_current = int(config.getfloat("main", "screeny_current"))
debugscreen = config.getboolean("main", "debugscreen")
debugmode = config.getboolean("main", "debugmode")
skip = config.getboolean("main", "skip")
pos_x = config.getfloat("main", "posy")
pos_y = config.getfloat("main", "posx")
sounds.music.volume = config.getfloat("main", "volume")
#lint:enable
except ConfigParser.NoOptionError as test:
print(("Saved game couldn't be loaded completly: " + str(test)))
except Exception:
print(("Unexpected error:", sys.exc_info()[0]))
print((traceback.format_exc()))
screen = pygame.display.set_mode((screenx_current, screeny_current))
def quit():
"""Routine for exiting"""
from . import midi_in

14
src/tests/json_saving.py

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
import json
data = {"Fullscreen": True, "pos_x": 0.3462345254}
data_json = json.dumps(data, indent=12)
print((data_json))
file_obj = open("test1.json", "rw+")
json.dump(data, file_obj)
file_obj.close()
file_obj = open("test1.json", "r")
read_data = json.load(file_obj)
print read_data

1
src/tests/test1.json

@ -0,0 +1 @@
{"Fullscreen": true, "pos_x": 0.3462345254}
Loading…
Cancel
Save