~ubuntu-branches/ubuntu/vivid/solarwolf/vivid

« back to all changes in this revision

Viewing changes to code/gamesetup.py

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2004-02-17 20:18:53 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040217201853-26zrwncpwu3pobf0
Tags: 1.5-1
* New upstream release.
* Remove patch from solarwolf.py, it is integrated upstream.
* Use icons from upstream, remove build-dependency on sng.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Gamename input setup handler, part of SOLARWOLF."""
 
2
# Copyright (C) 2002 Aaron "APS" Schlaegel, LGPL, see lgpl.txt
 
3
import string, math
 
4
import pygame
 
5
from pygame.locals import *
 
6
import game
 
7
import gfx, snd, txt
 
8
import input
 
9
import score
 
10
import gameplay
 
11
 
 
12
 
 
13
images = []
 
14
delimage = None
 
15
addimage = None
 
16
allimage = None
 
17
namefont = None
 
18
namefontheight = None
 
19
textfont = None
 
20
textfontheight = None
 
21
 
 
22
# ship direction constants
 
23
SHIPUP    = 1
 
24
SHIPDOWN  = 2
 
25
SHIPRIGHT = 3
 
26
SHIPLEFT  = 4
 
27
 
 
28
DONE     = 0
 
29
BUTTONS  = 1
 
30
DELETING = 2
 
31
ADDING   = 3
 
32
 
 
33
def load_game_resources():
 
34
    global images, namefont, namefontheight, textfont, textfontheight, delimage, addimage, allimage
 
35
    img = pygame.transform.rotate(gfx.load('ship-up.png'), -90)
 
36
    images.append((img, img.get_rect()))
 
37
 
 
38
    bgd = 0, 0, 0
 
39
    font = txt.Font(None, 50)
 
40
    t = font.text((220, 210, 180), 'Setup Controls', (gfx.rect.centerx, 30))
 
41
    images.append(t)
 
42
    t = txt.Font('sans', 12).text((180, 220, 180), '(You can Pause the game with the PAUSE or P buttons)', (400, 590))
 
43
    images.append(t)
 
44
 
 
45
    namefontheight = 46
 
46
    namefont = txt.Font(None, 46)
 
47
    textfontheight = 26
 
48
    textfont = txt.Font(None, textfontheight)
 
49
    smallfont = txt.Font('sans', 12)
 
50
 
 
51
    snd.preload('select_choose', 'select_move', 'incorrect', 'delete')
 
52
 
 
53
    delimage = gfx.load('btn-delete.gif')
 
54
    addimage = gfx.load('btn-add.gif')
 
55
    #allimage = gfx.load('btn-all.gif')
 
56
 
 
57
 
 
58
class GameSetup:
 
59
    def __init__(self, prevhandler):
 
60
        self.prevhandler = prevhandler
 
61
        self.images = images
 
62
        self.inputstate = BUTTONS
 
63
        self.buttonlist = []
 
64
        self.buildbuttonlist()
 
65
        self.controlrectlist = []
 
66
        self.buildcontrolrectlist()
 
67
        self.actionlist = []
 
68
        self.clearactionlist()
 
69
        self.buildactionlist()
 
70
        self.currentaction = 0
 
71
        self.currentbutton = 0
 
72
        self.shipmovex = 40
 
73
        self.shipmovey = 16
 
74
        self.shipdir = SHIPRIGHT
 
75
        self.status = '...'
 
76
        self.statusimage = None
 
77
        self.buildstatus()
 
78
 
 
79
        self.moveto(self.targetbutton())
 
80
 
 
81
    def moveto(self, pos):
 
82
        if self.shipdir == SHIPUP:
 
83
            self.shippos = pos[0] - (self.images[0][1].width / 2), pos[1]
 
84
        elif self.shipdir == SHIPDOWN:
 
85
            self.shippos = pos[0] - (self.images[0][1].width / 2), pos[1] - self.images[0][1].height
 
86
        elif self.shipdir == SHIPRIGHT:
 
87
            self.shippos = pos[0] - self.images[0][1].width, pos[1] - (self.images[0][1].height / 2)
 
88
        else:
 
89
            self.shippos == pos[0], pos[1] - (self.images[0][1].height / 2)
 
90
 
 
91
    def displayevent(self, i):
 
92
        if i.normalized != None:
 
93
            msg = input.input_text(i.type, i.normalized)
 
94
            if msg not in (self.status, None):
 
95
                self.status = msg
 
96
                self.clearstatus()
 
97
                self.buildstatus()
 
98
                self.drawstatus()
 
99
 
 
100
 
 
101
    def quit(self):
 
102
        self.inputstate = DONE
 
103
        self.clearactionlist()
 
104
        self.clearstatus()
 
105
        snd.play('select_choose')
 
106
 
 
107
    def add(self, i):
 
108
        if not i.release:
 
109
            if i.all:
 
110
                snd.play('select_choose')
 
111
                self.clearactionlist()
 
112
                self.display[input.actions_order[self.currentaction]].append((i.type, i.normalized))
 
113
                input.setdisplay(self.display)
 
114
                self.buildactionlist()
 
115
                self.drawactionlist()
 
116
            else:
 
117
                snd.play('incorrect')
 
118
            self.inputstate = BUTTONS
 
119
            self.moveto(self.targetbutton())
 
120
 
 
121
    def delete(self):
 
122
        snd.play('delete')
 
123
        self.clearactionlist()
 
124
        del self.display[input.actions_order[self.currentaction]][self.currentcontrol]
 
125
        input.setdisplay(self.display)
 
126
        self.buildactionlist()
 
127
        self.drawactionlist()
 
128
        self.inputstate = BUTTONS
 
129
        self.moveto(self.targetbutton())
 
130
 
 
131
    def selectdelete(self):
 
132
        def ignoreall(x):
 
133
            return x[0] != NOEVENT
 
134
        mutable = len(filter(ignoreall, self.display[input.actions_order[self.currentaction]]))
 
135
        if mutable > 1:
 
136
            snd.play('select_choose')
 
137
            self.inputstate = DELETING
 
138
            self.currentcontrol = 0
 
139
            self.moveto(self.targetcontrol())
 
140
        else:
 
141
            snd.play('incorrect')
 
142
 
 
143
    def selectadd(self):
 
144
        if len(self.display[input.actions_order[self.currentaction]]) <= 12:
 
145
            snd.play('select_choose')
 
146
            self.inputstate = ADDING
 
147
            self.currentcontrol = len(self.display[input.actions_order[self.currentaction]])
 
148
            self.moveto(self.targetcontrol())
 
149
        else:
 
150
            snd.play('incorrect')
 
151
 
 
152
    def selectall(self):
 
153
        snd.play('select_choose')
 
154
        self.clearactionlist()
 
155
        if not input.translations.has_key(NOEVENT):
 
156
            input.translations[NOEVENT] = {}
 
157
        input.translations[NOEVENT][KEYDOWN] = input.actions_order[self.currentaction]
 
158
        input.translations[NOEVENT][JOYBUTTONDOWN] = input.actions_order[self.currentaction]
 
159
        self.display = input.getdisplay()
 
160
        self.buildactionlist()
 
161
        self.drawactionlist()
 
162
        self.inputstate = BUTTONS
 
163
        self.moveto(self.targetbutton())
 
164
 
 
165
 
 
166
    def input(self, i):
 
167
        if i.release:
 
168
            return
 
169
        if self.inputstate == DONE:
 
170
            return
 
171
        self.displayevent(i)
 
172
 
 
173
        #APS switch done to the inputstate
 
174
        if self.inputstate == BUTTONS:
 
175
            if i.translated == input.ABORT:
 
176
                self.quit()
 
177
            if i.translated == input.PRESS:
 
178
                if self.currentbutton == 0:
 
179
                    self.selectadd()
 
180
                #elif self.currentbutton == 1:
 
181
                #    self.selectall()
 
182
                else:
 
183
                    self.selectdelete()
 
184
            if i.translated in ( input.DOWN, input.UP, input.LEFT, input.RIGHT):
 
185
                if i.translated == input.DOWN:
 
186
                    self.currentaction = (self.currentaction + 1) % len(self.actionlist)
 
187
                elif i.translated == input.UP:
 
188
                    self.currentaction = (self.currentaction - 1) % len(self.actionlist)
 
189
                elif i.translated == input.LEFT:
 
190
                    self.currentbutton = (self.currentbutton - 1) % len(self.buttonlist)
 
191
                elif i.translated == input.RIGHT:
 
192
                    self.currentbutton = (self.currentbutton + 1) % len(self.buttonlist)
 
193
                snd.play('select_move')
 
194
                self.moveto(self.targetbutton())
 
195
        elif self.inputstate == DELETING:
 
196
            if i.translated == input.ABORT:
 
197
                snd.play('select_choose')
 
198
                self.inputstate = BUTTONS
 
199
                self.moveto(self.targetbutton())
 
200
            if i.translated == input.PRESS:
 
201
                self.delete()
 
202
            if i.translated in ( input.DOWN, input.UP, input.LEFT, input.RIGHT):
 
203
                if i.translated == input.DOWN or i.translated == input.UP:
 
204
                    currentcontrol = (self.currentcontrol + 6) % 12
 
205
                    if currentcontrol < len(self.display[input.actions_order[self.currentaction]]):
 
206
                        self.currentcontrol = currentcontrol
 
207
                elif i.translated == input.LEFT:
 
208
                    self.currentcontrol = (self.currentcontrol - 1) % 6 + 6 * (self.currentcontrol / 6)
 
209
                    if self.currentcontrol >= len(self.display[input.actions_order[self.currentaction]]):
 
210
                        self.currentcontrol = len(self.display[input.actions_order[self.currentaction]]) - 1
 
211
                elif i.translated == input.RIGHT:
 
212
                    self.currentcontrol = (self.currentcontrol + 1) % 6 + 6 * (self.currentcontrol / 6)
 
213
                    if self.currentcontrol >= len(self.display[input.actions_order[self.currentaction]]):
 
214
                        self.currentcontrol = 6 * (self.currentcontrol / 6)
 
215
                snd.play('select_move')
 
216
                self.moveto(self.targetcontrol())
 
217
            pass
 
218
        elif self.inputstate == ADDING:
 
219
            self.add(i)
 
220
 
 
221
    def targetbutton(self):
 
222
        x = self.buttonlist[self.currentbutton][1].left
 
223
        y = self.actionlist[self.currentaction][1].top + self.buttonlist[self.currentbutton][1].centery
 
224
        return (x,y)
 
225
 
 
226
    def targetcontrol(self):
 
227
        x = self.controlrectlist[self.currentcontrol].left
 
228
        y = self.actionlist[self.currentaction][1].top + self.controlrectlist[self.currentcontrol].centery
 
229
        return (x,y)
 
230
 
 
231
    def event(self, e):
 
232
        pass
 
233
 
 
234
    def run(self):
 
235
        r = self.background(self.images[0][1])
 
236
        gfx.dirty(r)
 
237
 
 
238
        self.moveship()
 
239
        gfx.updatestars(self.background, gfx)
 
240
 
 
241
        if self.inputstate != DONE:
 
242
            self.drawactionlist()
 
243
            self.drawstatus()
 
244
            for img in self.images:
 
245
                r = gfx.surface.blit(img[0], img[1])
 
246
                gfx.dirty(r)
 
247
        else:
 
248
            self.clearactionlist()
 
249
            self.clearstatus()
 
250
            game.handler = self.prevhandler
 
251
 
 
252
            for img in self.images[1:]:
 
253
                r = self.background(img[1])
 
254
                gfx.dirty(r)
 
255
 
 
256
    def buildcontrolrectlist(self):
 
257
        global textfontheight
 
258
        for l in range(16):
 
259
            x = 90 + 100 * (l % 6)
 
260
            y = 36 + 22 * (l / 6)
 
261
            w = 100
 
262
            h = textfontheight
 
263
            r = pygame.Rect(x, y, w, h)
 
264
            self.controlrectlist.append(r)
 
265
 
 
266
    def buildbuttonlist(self):
 
267
        i = 0
 
268
        #for img in (addimage, allimage, delimage):
 
269
        for img in (addimage, delimage):
 
270
            rect = img.get_rect().move(300 + 250 * i, 10)
 
271
            self.buttonlist.append((img, rect))
 
272
            i += 1
 
273
 
 
274
 
 
275
    def buildactionlist(self):
 
276
        clr = 160, 200, 250
 
277
        clr2 = 200, 200, 200
 
278
        offsety = 90
 
279
        sizey = 75
 
280
        sizex = 800
 
281
        self.actionlist = []
 
282
        self.display = input.getdisplay()
 
283
        for a in input.actions_order:
 
284
            if gfx.surface.get_bitsize() > 8:
 
285
                img = pygame.Surface((sizex, sizey))
 
286
            else:
 
287
                img = pygame.Surface((sizex, sizey), 0, 32)
 
288
 
 
289
            subimgs = []
 
290
 
 
291
            subimgs.append((namefont.render(input.actions_text[a], 1, clr), (80, 0)))
 
292
 
 
293
            for l in range(len(self.display[a])):
 
294
                text = input.input_text(self.display[a][l][0],self.display[a][l][1])
 
295
                subimg = textfont.render(text, 1, clr2)
 
296
                r = subimg.get_rect()
 
297
                r.topleft = self.controlrectlist[l].topleft
 
298
                subimgs.append((subimg, r))
 
299
 
 
300
            for b in self.buttonlist:
 
301
                subimgs.append(b)
 
302
 
 
303
            bgd = 0, 0, 0
 
304
            img.fill(bgd)
 
305
            for sub, pos in subimgs:
 
306
                img.blit(sub, pos)
 
307
            img.set_colorkey(bgd, RLEACCEL)
 
308
            #img = img.convert()
 
309
            rect = img.get_rect().move(0, offsety)
 
310
 
 
311
            self.actionlist.append((img, rect))
 
312
            offsety += sizey
 
313
 
 
314
 
 
315
    def clearactionlist(self):
 
316
        for g in self.actionlist:
 
317
            self.background(g[1])
 
318
            gfx.dirty(g[1])
 
319
 
 
320
    def drawactionlist(self):
 
321
        for g in self.actionlist:
 
322
            r = gfx.surface.blit(g[0], g[1])
 
323
            gfx.dirty(r)
 
324
 
 
325
    def buildstatus(self):
 
326
        if self.status != '...':
 
327
            statustext = "(Latest Input Event: %s)" % self.status
 
328
            self.statusimage = textfont.text((255, 250, 160), statustext, (gfx.rect.centerx, 560))
 
329
        else:
 
330
            self.statusimage = None
 
331
 
 
332
    def drawstatus(self):
 
333
        if self.statusimage:
 
334
            r = gfx.surface.blit(self.statusimage[0], self.statusimage[1])
 
335
            gfx.dirty(r)
 
336
 
 
337
    def clearstatus(self):
 
338
        if self.statusimage:
 
339
            r = self.background(self.statusimage[1])
 
340
            gfx.dirty(r)
 
341
 
 
342
    def background(self, area):
 
343
        return gfx.surface.fill((0, 0, 0), area)
 
344
 
 
345
    def moveship(self):
 
346
        pos = list(self.images[0][1].topleft)
 
347
        if pos[0] + self.shipmovex < self.shippos[0]:
 
348
            pos[0] += self.shipmovex
 
349
        elif pos[0] < self.shippos[0]:
 
350
            pos[0] = self.shippos[0]
 
351
 
 
352
        if pos[0] - self.shipmovex > self.shippos[0]:
 
353
            pos[0] -= self.shipmovex
 
354
        elif pos[0] > self.shippos[0]:
 
355
            pos[0] = self.shippos[0]
 
356
 
 
357
        if pos[1] + self.shipmovey < self.shippos[1]:
 
358
            pos[1] += self.shipmovey
 
359
        elif pos[1] < self.shippos[1]:
 
360
            pos[1] = self.shippos[1]
 
361
 
 
362
        if pos[1] - self.shipmovey > self.shippos[1]:
 
363
            pos[1] -= self.shipmovey
 
364
        elif pos[1] > self.shippos[1]:
 
365
            pos[1] = self.shippos[1]
 
366
 
 
367
        self.images[0][1].topleft = pos
 
368
 
 
369