Mercurial > rinkhals
annotate gamelib/misc.py @ 447:f04a2490c35f
The sub-toolbar rewrite, the finally not crashing version
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 19:58:49 +0000 |
parents | 129de5883524 |
children | 96dbf2c8506e |
rev | line source |
---|---|
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
1 # Holder for misc useful classes |
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
2 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
3 import random |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
4 |
336 | 5 from pygame.locals import KEYDOWN, K_ESCAPE |
6 from pgu import gui | |
431
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
7 from pgu.algo import getline |
336 | 8 |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
9 import serializer |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
10 |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
11 class Position(serializer.Simplifiable): |
100 | 12 """2D position / vector""" |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
13 |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
14 SIMPLIFY = ['x', 'y', 'z'] |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
15 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
16 def __init__(self, x, y, z=0): |
100 | 17 self.x = x |
18 self.y = y | |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
19 self.z = z |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
20 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
21 def to_tile_tuple(self): |
100 | 22 return self.x, self.y |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
23 |
100 | 24 def dist(self, b): |
25 """Gives the distance to another position""" | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
26 |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
27 return max(abs(self.x - b.x), abs(self.y - b.y), abs(self.z - b.z)) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
28 |
100 | 29 def __sub__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
30 return Position(self.x - b.x, self.y - b.y, self.z - b.z) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
31 |
100 | 32 def __add__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
33 return Position(self.x + b.x, self.y + b.y, self.z + b.z) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
34 |
100 | 35 def left_of(self, b): |
36 return self.x < b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
37 |
100 | 38 def right_of(self, b): |
39 return self.x > b.x | |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
40 |
100 | 41 def __eq__(self, b): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
347
diff
changeset
|
42 return self.x == b.x and self.y == b.y and self.z == b.z |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
43 |
431
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
44 def intermediate_positions(self, b): |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
45 """Only operates in two dimensions.""" |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
46 if max(abs(self.x - b.x), abs(self.y - b.y)) <= 1: |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
47 # pgu gets this case wrong on occasion. |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
48 return [b] |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
49 start = self.to_tile_tuple() |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
50 end = b.to_tile_tuple() |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
51 points = getline(start, end) |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
52 points.remove(start) # exclude start_pos |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
53 if end not in points: |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
54 # Rounding errors in getline cause this |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
55 points.append(end) |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
56 return [Position(p[0], p[1]) for p in points] |
129de5883524
Moved intermediate position generation to a more suitable location.
Jeremy Thurgood <firxen@gmail.com>
parents:
427
diff
changeset
|
57 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
58 class WeightedSelection(object): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
59 def __init__(self, weightings=None): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
60 self.weightings = [] |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
61 self.total = 0 |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
62 if weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
63 for item, weight in weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
64 self.weightings.append((item, weight)) |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
65 self.total += weight |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
66 |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
67 def choose(self): |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
68 roll = random.uniform(0, self.total) |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
69 for item, weight in self.weightings: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
70 if roll < weight: |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
71 return item |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
72 roll -= weight |
336 | 73 |
74 class CheckDialog(gui.Dialog): | |
75 def __init__(self, **params): | |
347
35f09e0ccd16
Make 'Esc' cancel the 'Are You Sure?' dialog. Fix dialog title
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
76 title = gui.Label('Are You Sure?') |
336 | 77 self.do_quit = False |
78 self.running = True | |
79 tbl = gui.Table() | |
80 tbl.tr() | |
81 tbl.td(gui.Label("Do you REALLY want to exit this game?"), colspan=2) | |
82 tbl.tr() | |
83 tbl.td(gui.Spacer(0, 15), colspan=2) | |
84 tbl.tr() | |
85 yes_button = gui.Button('Yes') | |
86 yes_button.connect(gui.CLICK, self.clicked, True) | |
87 no_button = gui.Button('No') | |
88 no_button.connect(gui.CLICK, self.clicked, False) | |
89 tbl.td(no_button, align=-1) | |
90 tbl.td(yes_button, align=1) | |
91 gui.Dialog.__init__(self, title, tbl, **params) | |
92 | |
93 def clicked(self, val): | |
94 self.do_quit = val | |
95 self.running = False | |
96 self.close() | |
97 | |
98 def event(self, e): | |
99 if e.type == KEYDOWN and e.key == K_ESCAPE: | |
347
35f09e0ccd16
Make 'Esc' cancel the 'Are You Sure?' dialog. Fix dialog title
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
100 self.clicked(False) |
336 | 101 return True |
102 return gui.Dialog.event(self, e) | |
103 | |
104 def check_exit(): | |
105 dialog = CheckDialog() | |
106 dialog.open() | |
107 return dialog | |
108 | |
109 |