File: gamesetup.py

package info (click to toggle)
solarwolf 1.5%2Bdfsg1-3
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,592 kB
  • sloc: python: 5,349; ansic: 159; makefile: 100; pascal: 50; sh: 27
file content (369 lines) | stat: -rw-r--r-- 12,696 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""Gamename input setup handler, part of SOLARWOLF."""
# Copyright (C) 2002 Aaron "APS" Schlaegel, LGPL, see lgpl.txt
import string, math
import pygame
from pygame.locals import *
import game
import gfx, snd, txt
import input
import score
import gameplay


images = []
delimage = None
addimage = None
allimage = None
namefont = None
namefontheight = None
textfont = None
textfontheight = None

# ship direction constants
SHIPUP    = 1
SHIPDOWN  = 2
SHIPRIGHT = 3
SHIPLEFT  = 4

DONE     = 0
BUTTONS  = 1
DELETING = 2
ADDING   = 3

def load_game_resources():
    global images, namefont, namefontheight, textfont, textfontheight, delimage, addimage, allimage
    img = pygame.transform.rotate(gfx.load('ship-up.png'), -90)
    images.append((img, img.get_rect()))

    bgd = 0, 0, 0
    font = txt.Font(None, 50)
    t = font.text((220, 210, 180), 'Setup Controls', (gfx.rect.centerx, 30))
    images.append(t)
    t = txt.Font('sans', 12).text((180, 220, 180), '(You can Pause the game with the PAUSE or P buttons)', (400, 590))
    images.append(t)

    namefontheight = 46
    namefont = txt.Font(None, 46)
    textfontheight = 26
    textfont = txt.Font(None, textfontheight)
    smallfont = txt.Font('sans', 12)

    snd.preload('select_choose', 'select_move', 'incorrect', 'delete')

    delimage = gfx.load('btn-delete.gif')
    addimage = gfx.load('btn-add.gif')
    #allimage = gfx.load('btn-all.gif')


class GameSetup:
    def __init__(self, prevhandler):
        self.prevhandler = prevhandler
        self.images = images
        self.inputstate = BUTTONS
        self.buttonlist = []
        self.buildbuttonlist()
        self.controlrectlist = []
        self.buildcontrolrectlist()
        self.actionlist = []
        self.clearactionlist()
        self.buildactionlist()
        self.currentaction = 0
        self.currentbutton = 0
        self.shipmovex = 40
        self.shipmovey = 16
        self.shipdir = SHIPRIGHT
        self.status = '...'
        self.statusimage = None
        self.buildstatus()

        self.moveto(self.targetbutton())

    def moveto(self, pos):
        if self.shipdir == SHIPUP:
            self.shippos = pos[0] - (self.images[0][1].width // 2), pos[1]
        elif self.shipdir == SHIPDOWN:
            self.shippos = pos[0] - (self.images[0][1].width // 2), pos[1] - self.images[0][1].height
        elif self.shipdir == SHIPRIGHT:
            self.shippos = pos[0] - self.images[0][1].width, pos[1] - (self.images[0][1].height // 2)
        else:
            self.shippos == pos[0], pos[1] - (self.images[0][1].height // 2)

    def displayevent(self, i):
        if i.normalized != None:
            msg = input.input_text(i.type, i.normalized)
            if msg not in (self.status, None):
                self.status = msg
                self.clearstatus()
                self.buildstatus()
                self.drawstatus()


    def quit(self):
        self.inputstate = DONE
        self.clearactionlist()
        self.clearstatus()
        snd.play('select_choose')

    def add(self, i):
        if not i.release:
            if i.all:
                snd.play('select_choose')
                self.clearactionlist()
                self.display[input.actions_order[self.currentaction]].append((i.type, i.normalized))
                input.setdisplay(self.display)
                self.buildactionlist()
                self.drawactionlist()
            else:
                snd.play('incorrect')
            self.inputstate = BUTTONS
            self.moveto(self.targetbutton())

    def delete(self):
        snd.play('delete')
        self.clearactionlist()
        del self.display[input.actions_order[self.currentaction]][self.currentcontrol]
        input.setdisplay(self.display)
        self.buildactionlist()
        self.drawactionlist()
        self.inputstate = BUTTONS
        self.moveto(self.targetbutton())

    def selectdelete(self):
        def ignoreall(x):
            return x[0] != NOEVENT
        mutable = len(filter(ignoreall, self.display[input.actions_order[self.currentaction]]))
        if mutable > 1:
            snd.play('select_choose')
            self.inputstate = DELETING
            self.currentcontrol = 0
            self.moveto(self.targetcontrol())
        else:
            snd.play('incorrect')

    def selectadd(self):
        if len(self.display[input.actions_order[self.currentaction]]) <= 12:
            snd.play('select_choose')
            self.inputstate = ADDING
            self.currentcontrol = len(self.display[input.actions_order[self.currentaction]])
            self.moveto(self.targetcontrol())
        else:
            snd.play('incorrect')

    def selectall(self):
        snd.play('select_choose')
        self.clearactionlist()
        if NOEVENT not in input.translations:
            input.translations[NOEVENT] = {}
        input.translations[NOEVENT][KEYDOWN] = input.actions_order[self.currentaction]
        input.translations[NOEVENT][JOYBUTTONDOWN] = input.actions_order[self.currentaction]
        self.display = input.getdisplay()
        self.buildactionlist()
        self.drawactionlist()
        self.inputstate = BUTTONS
        self.moveto(self.targetbutton())


    def input(self, i):
        if i.release:
            return
        if self.inputstate == DONE:
            return
        self.displayevent(i)

        #APS switch done to the inputstate
        if self.inputstate == BUTTONS:
            if i.translated == input.ABORT:
                self.quit()
            if i.translated == input.PRESS:
                if self.currentbutton == 0:
                    self.selectadd()
                #elif self.currentbutton == 1:
                #    self.selectall()
                else:
                    self.selectdelete()
            if i.translated in ( input.DOWN, input.UP, input.LEFT, input.RIGHT):
                if i.translated == input.DOWN:
                    self.currentaction = (self.currentaction + 1) % len(self.actionlist)
                elif i.translated == input.UP:
                    self.currentaction = (self.currentaction - 1) % len(self.actionlist)
                elif i.translated == input.LEFT:
                    self.currentbutton = (self.currentbutton - 1) % len(self.buttonlist)
                elif i.translated == input.RIGHT:
                    self.currentbutton = (self.currentbutton + 1) % len(self.buttonlist)
                snd.play('select_move')
                self.moveto(self.targetbutton())
        elif self.inputstate == DELETING:
            if i.translated == input.ABORT:
                snd.play('select_choose')
                self.inputstate = BUTTONS
                self.moveto(self.targetbutton())
            if i.translated == input.PRESS:
                self.delete()
            if i.translated in ( input.DOWN, input.UP, input.LEFT, input.RIGHT):
                if i.translated == input.DOWN or i.translated == input.UP:
                    currentcontrol = (self.currentcontrol + 6) % 12
                    if currentcontrol < len(self.display[input.actions_order[self.currentaction]]):
                        self.currentcontrol = currentcontrol
                elif i.translated == input.LEFT:
                    self.currentcontrol = (self.currentcontrol - 1) % 6 + 6 * (self.currentcontrol // 6)
                    if self.currentcontrol >= len(self.display[input.actions_order[self.currentaction]]):
                        self.currentcontrol = len(self.display[input.actions_order[self.currentaction]]) - 1
                elif i.translated == input.RIGHT:
                    self.currentcontrol = (self.currentcontrol + 1) % 6 + 6 * (self.currentcontrol // 6)
                    if self.currentcontrol >= len(self.display[input.actions_order[self.currentaction]]):
                        self.currentcontrol = 6 * (self.currentcontrol // 6)
                snd.play('select_move')
                self.moveto(self.targetcontrol())
            pass
        elif self.inputstate == ADDING:
            self.add(i)

    def targetbutton(self):
        x = self.buttonlist[self.currentbutton][1].left
        y = self.actionlist[self.currentaction][1].top + self.buttonlist[self.currentbutton][1].centery
        return (x,y)

    def targetcontrol(self):
        x = self.controlrectlist[self.currentcontrol].left
        y = self.actionlist[self.currentaction][1].top + self.controlrectlist[self.currentcontrol].centery
        return (x,y)

    def event(self, e):
        pass

    def run(self):
        r = self.background(self.images[0][1])
        gfx.dirty(r)

        self.moveship()
        gfx.updatestars(self.background, gfx)

        if self.inputstate != DONE:
            self.drawactionlist()
            self.drawstatus()
            for img in self.images:
                r = gfx.surface.blit(img[0], img[1])
                gfx.dirty(r)
        else:
            self.clearactionlist()
            self.clearstatus()
            game.handler = self.prevhandler

            for img in self.images[1:]:
                r = self.background(img[1])
                gfx.dirty(r)

    def buildcontrolrectlist(self):
        global textfontheight
        for l in range(16):
            x = 90 + 100 * (l % 6)
            y = 36 + 22 * (l // 6)
            w = 100
            h = textfontheight
            r = pygame.Rect(x, y, w, h)
            self.controlrectlist.append(r)

    def buildbuttonlist(self):
        i = 0
        #for img in (addimage, allimage, delimage):
        for img in (addimage, delimage):
            rect = img.get_rect().move(300 + 250 * i, 10)
            self.buttonlist.append((img, rect))
            i += 1


    def buildactionlist(self):
        clr = 160, 200, 250
        clr2 = 200, 200, 200
        offsety = 90
        sizey = 75
        sizex = 800
        self.actionlist = []
        self.display = input.getdisplay()
        for a in input.actions_order:
            if gfx.surface.get_bitsize() > 8:
                img = pygame.Surface((sizex, sizey))
            else:
                img = pygame.Surface((sizex, sizey), 0, 32)

            subimgs = []

            subimgs.append((namefont.render(input.actions_text[a], 1, clr), (80, 0)))

            for l in range(len(self.display[a])):
                text = input.input_text(self.display[a][l][0],self.display[a][l][1])
                subimg = textfont.render(text, 1, clr2)
                r = subimg.get_rect()
                r.topleft = self.controlrectlist[l].topleft
                subimgs.append((subimg, r))

            for b in self.buttonlist:
                subimgs.append(b)

            bgd = 0, 0, 0
            img.fill(bgd)
            for sub, pos in subimgs:
                img.blit(sub, pos)
            img.set_colorkey(bgd, RLEACCEL)
            #img = img.convert()
            rect = img.get_rect().move(0, offsety)

            self.actionlist.append((img, rect))
            offsety += sizey


    def clearactionlist(self):
        for g in self.actionlist:
            self.background(g[1])
            gfx.dirty(g[1])

    def drawactionlist(self):
        for g in self.actionlist:
            r = gfx.surface.blit(g[0], g[1])
            gfx.dirty(r)

    def buildstatus(self):
        if self.status != '...':
            statustext = "(Latest Input Event: %s)" % self.status
            self.statusimage = textfont.text((255, 250, 160), statustext, (gfx.rect.centerx, 560))
        else:
            self.statusimage = None

    def drawstatus(self):
        if self.statusimage:
            r = gfx.surface.blit(self.statusimage[0], self.statusimage[1])
            gfx.dirty(r)

    def clearstatus(self):
        if self.statusimage:
            r = self.background(self.statusimage[1])
            gfx.dirty(r)

    def background(self, area):
        return gfx.surface.fill((0, 0, 0), area)

    def moveship(self):
        pos = list(self.images[0][1].topleft)
        if pos[0] + self.shipmovex < self.shippos[0]:
            pos[0] += self.shipmovex
        elif pos[0] < self.shippos[0]:
            pos[0] = self.shippos[0]

        if pos[0] - self.shipmovex > self.shippos[0]:
            pos[0] -= self.shipmovex
        elif pos[0] > self.shippos[0]:
            pos[0] = self.shippos[0]

        if pos[1] + self.shipmovey < self.shippos[1]:
            pos[1] += self.shipmovey
        elif pos[1] < self.shippos[1]:
            pos[1] = self.shippos[1]

        if pos[1] - self.shipmovey > self.shippos[1]:
            pos[1] -= self.shipmovey
        elif pos[1] > self.shippos[1]:
            pos[1] = self.shippos[1]

        self.images[0][1].topleft = pos