annotate gamelib/misc.py @ 427:e89a1afe4e84

Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
author Simon Cross <hodgestar@gmail.com>
date Sat, 21 Nov 2009 16:17:48 +0000
parents 2d0ff46118e2
children 129de5883524
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
5 from pygame.locals import KEYDOWN, K_ESCAPE
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
6 from pgu import gui
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
7
427
e89a1afe4e84 Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents: 395
diff changeset
8 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
9
e89a1afe4e84 Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents: 395
diff changeset
10 class Position(serializer.Simplifiable):
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
11 """2D position / vector"""
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12
427
e89a1afe4e84 Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents: 395
diff changeset
13 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
14
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
15 def __init__(self, x, y, z=0):
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
16 self.x = x
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
17 self.y = y
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
18 self.z = z
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
20 def to_tile_tuple(self):
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
21 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
22
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
23 def dist(self, b):
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
24 """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
25
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
26 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
27
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
28 def __sub__(self, b):
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
29 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
30
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
31 def __add__(self, b):
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
32 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
33
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
34 def left_of(self, b):
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
35 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
36
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
37 def right_of(self, b):
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
38 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
39
100
e90068d1f374 Re-indent to four spaces.
Simon Cross <hodgestar@gmail.com>
parents: 69
diff changeset
40 def __eq__(self, b):
395
2d0ff46118e2 Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents: 347
diff changeset
41 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
42
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
43 class WeightedSelection(object):
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
44 def __init__(self, weightings=None):
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
45 self.weightings = []
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
46 self.total = 0
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
47 if weightings:
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
48 for item, weight in weightings:
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
49 self.weightings.append((item, weight))
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
50 self.total += weight
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
51
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
52 def choose(self):
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
53 roll = random.uniform(0, self.total)
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
54 for item, weight in self.weightings:
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
55 if roll < weight:
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
56 return item
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
57 roll -= weight
336
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
58
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
59 class CheckDialog(gui.Dialog):
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
60 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
61 title = gui.Label('Are You Sure?')
336
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
62 self.do_quit = False
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
63 self.running = True
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
64 tbl = gui.Table()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
65 tbl.tr()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
66 tbl.td(gui.Label("Do you REALLY want to exit this game?"), colspan=2)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
67 tbl.tr()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
68 tbl.td(gui.Spacer(0, 15), colspan=2)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
69 tbl.tr()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
70 yes_button = gui.Button('Yes')
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
71 yes_button.connect(gui.CLICK, self.clicked, True)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
72 no_button = gui.Button('No')
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
73 no_button.connect(gui.CLICK, self.clicked, False)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
74 tbl.td(no_button, align=-1)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
75 tbl.td(yes_button, align=1)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
76 gui.Dialog.__init__(self, title, tbl, **params)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
77
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
78 def clicked(self, val):
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
79 self.do_quit = val
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
80 self.running = False
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
81 self.close()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
82
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
83 def event(self, e):
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
84 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
85 self.clicked(False)
336
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
86 return True
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
87 return gui.Dialog.event(self, e)
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
88
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
89 def check_exit():
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
90 dialog = CheckDialog()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
91 dialog.open()
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
92 return dialog
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
93
82a18615a0ab Ask 'Are you sure?'
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
94