Mercurial > rinkhals
comparison gamelib/misc.py @ 336:82a18615a0ab
Ask 'Are you sure?'
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 05 Sep 2009 22:30:17 +0000 |
parents | 1a7000c8211c |
children | 35f09e0ccd16 |
comparison
equal
deleted
inserted
replaced
335:efafe71dd261 | 336:82a18615a0ab |
---|---|
1 # Holder for misc useful classes | 1 # Holder for misc useful classes |
2 | 2 |
3 import random | 3 import random |
4 | |
5 from pygame.locals import KEYDOWN, K_ESCAPE | |
6 from pgu import gui | |
4 | 7 |
5 class Position(object): | 8 class Position(object): |
6 """2D position / vector""" | 9 """2D position / vector""" |
7 | 10 |
8 def __init__(self, x, y): | 11 def __init__(self, x, y): |
45 roll = random.uniform(0, self.total) | 48 roll = random.uniform(0, self.total) |
46 for item, weight in self.weightings: | 49 for item, weight in self.weightings: |
47 if roll < weight: | 50 if roll < weight: |
48 return item | 51 return item |
49 roll -= weight | 52 roll -= weight |
53 | |
54 class CheckDialog(gui.Dialog): | |
55 def __init__(self, **params): | |
56 title = gui.Label('Are You sure') | |
57 self.do_quit = False | |
58 self.running = True | |
59 tbl = gui.Table() | |
60 tbl.tr() | |
61 tbl.td(gui.Label("Do you REALLY want to exit this game?"), colspan=2) | |
62 tbl.tr() | |
63 tbl.td(gui.Spacer(0, 15), colspan=2) | |
64 tbl.tr() | |
65 yes_button = gui.Button('Yes') | |
66 yes_button.connect(gui.CLICK, self.clicked, True) | |
67 no_button = gui.Button('No') | |
68 no_button.connect(gui.CLICK, self.clicked, False) | |
69 tbl.td(no_button, align=-1) | |
70 tbl.td(yes_button, align=1) | |
71 gui.Dialog.__init__(self, title, tbl, **params) | |
72 | |
73 def clicked(self, val): | |
74 self.do_quit = val | |
75 self.running = False | |
76 self.close() | |
77 | |
78 def event(self, e): | |
79 if e.type == KEYDOWN and e.key == K_ESCAPE: | |
80 self.clicked(True) | |
81 return True | |
82 return gui.Dialog.event(self, e) | |
83 | |
84 def check_exit(): | |
85 dialog = CheckDialog() | |
86 dialog.open() | |
87 return dialog | |
88 | |
89 |