File: levels.py

package info (click to toggle)
solarwolf 1.5%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,656 kB
  • sloc: python: 5,353; ansic: 159; makefile: 102; pascal: 50; sh: 27
file content (113 lines) | stat: -rw-r--r-- 3,255 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import objbox, game
import pygame


Levels = []
initialized = 0

def init():
    global Levels, initialized
    curlev = []
    f = game.get_resource('levels.txt')
    f = open(f, 'r')
    curtitle = curtitle2 = ''
    while 1:
        l = f.readline()
        if l and l[0] == '>':
            curtitle = l[1:].strip()
            continue
        if l and l[0] == '<':
            curtitle2 = l[1:].strip()
            continue
        if curlev and (not l or l[0] == '!'):
            for i in range(7-len(curlev)):
                curlev.append('         ')
            curlev.insert(0, curtitle2)
            curlev.insert(0, curtitle)
            Levels.append(curlev)
            curtitle = curtitle2 = ''
            curlev = []
        if not l: break
        if l[0] == '!' or l[0] == ';':
            continue
        if len(curlev) < 7:
            l = (l.rstrip() + '          ')[:9]
            curlev.append(l)
    initialized = 1


def makelevel(level):
    "returns (list, startcenter) level number"
    if not initialized: init()
    lev = Levels[level%len(Levels)]
    touches = level//len(Levels) + 1
    passes = (level>len(Levels) and 2) or 1
    boxlist = []
    size = 58, 58
    corner = 106, 106
    startpos = corner[0]+236, corner[1]+182
    pos = [corner[0], corner[1]]
    numboxes = level//2
    for row in lev[2:]:
        cells = list(row)
        if touches == 2:
            cells.reverse()
        for cell in cells:
            if cell == '#':
                boxlist.append(objbox.Box(pos, touches))
                numboxes += touches
            elif cell == '*':
                boxlist.append(objbox.Box(pos, touches+1))
                numboxes += touches + 1
            elif cell == 's':
                startpos = pos[0] , pos[1]
            pos[0] = pos[0] + size[0]
        pos[0] = corner[0]
        pos[1] = pos[1] + size[1]
    msg = ''
    msg = lev[touches-1]
    if level == maxlevels()-1: msg = 'Final Level'
    return boxlist, startpos, msg, numboxes


def preview(level):
    "returns (list, startcenter) level number"
    if not initialized: init()
    lev = Levels[level%len(Levels)]
    touches = level//len(Levels) + 1
    passes = (level>len(Levels) and 2) or 1
    boxlist = []
    size = 5, 5
    corner = 5, 5
    startpos = corner[0]+236, corner[1]+182
    pos = [corner[0], corner[1]]
    numboxes = level//2
    img = pygame.Surface((52, 42))
    img.fill((20, 20, 30))
    pygame.draw.rect(img, (255, 255, 255), (0,0,51,41), 2)
    colors = (150,150,150), (60, 255, 60), (255, 255, 60), (255, 60, 60)
    for row in lev[2:]:
        cells = list(row)
        if touches == 2:
            cells.reverse()
        for cell in cells:
            if cell == '#':
                img.fill(colors[touches], (pos, (2, 2)))
            elif cell == '*':
                img.fill(colors[touches+1], (pos, (2, 2)))
            elif cell == 's':
                img.fill(colors[0], (pos, (2, 2)))
            pos[0] = pos[0] + size[0]
        pos[0] = corner[0]
        pos[1] = pos[1] + size[1]
    return img

def maxlevels():
    return len(Levels) * 2


def numrocks(level):
    if level >= maxlevels():
        return 18
    percent = float(level) / maxlevels()
    return int(percent * 12)