File: settings.py

package info (click to toggle)
snowballz 0.9.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 10,888 kB
  • ctags: 1,242
  • sloc: python: 7,511; makefile: 53; sh: 2
file content (82 lines) | stat: -rw-r--r-- 2,021 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pygame
import os
from ConfigParser import ConfigParser, NoOptionError

config = ConfigParser()
pygame.init()

def toggle(option, options):
    options = list(options)
    qm = config.get('display',option)
    i = options.index(qm)
    if i == len(options)-1:
        i = 0
    else:
        i += 1

    config.set('display', option, options[i])
    save_config()
    return options[i]

def get_res():
    try:
        qm = config.get('display','resolution')
    except NoOptionError:
        config.set('display', 'resolution', '0')
        save_config()
        qm = 0
    return get_res_available()[int(qm)]

def get_option(option):
    try:
        qm = config.get('display',option)
    except NoOptionError:
        config.set('display', option, '1')
        save_config()
        qm = "1"
    return qm

def get_res_available():
    res_available = pygame.display.list_modes()
    res_available.reverse()
    res_available_new = []
    for r in res_available:
        if r[0] >= 640 and r[1] >= 480:
            res_available_new.append(r)
    return res_available_new


def get_config_path():
    pathname = ""
    try:
        pathname = os.path.join(os.environ["HOME"], ".snowballz")
    except:
        try:
            pathname = os.path.join(os.environ["APPDATA"], "SnowballZ")
        except:
            print "Couldn't get environment variable for home directory"
            pathname = "."
    return pathname+".ini"


def load_config():
    path = get_config_path()
    r = config.read(path)
    if not r:
        # Setup settings.
        config.add_section('display')
        config.add_section('player')
        res_available = get_res_available()
        config.set('display', 'resolution', str(len(res_available)-1))
        config.set('display', 'snowballdetail', '3')
        config.set('player', 'name', 'Snowballer')
        file = open(path, 'w')
        config.write(file)


def save_config():
    path = get_config_path()
    file = open(path, 'w')
    config.write(file)

load_config()