erkennt jetzt ob das spiel fertig ist
[darcs-mirror-ozeanien.git] / ozeanien.py
1 #!/usr/bin/python
2 # encoding:utf8
3
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8 import sys
9
10 from engine import * 
11 from seatheme import SeaFabric
12 from grasstheme import GrassFabric
13
14
15 DEBUG = 1
16
17 def dbg(txt):
18     if DEBUG:
19         print txt
20
21
22 class Game:
23     def destroy(self, widget, data=None):
24         gtk.main_quit()
25
26     def __init__(self):
27         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
28         self.window.set_title("Ozeanien")
29         self.window.set_border_width(2)
30         self.window.set_resizable(False)
31         self.window.connect("destroy", self.destroy)
32         self.window.set_icon(gtk.gdk.pixbuf_new_from_file('icon.png'))
33
34         self.reset()
35
36         self.window.show_all()
37
38     def reset(self):
39         if self.window.get_child():
40             w = self.window.get_child()
41             self.window.remove(self.window.get_child())
42             w.destroy()
43
44         self.board = Board()
45         self.drag_from = None
46
47         if len(sys.argv) > 1 and sys.argv[1] == "sea": 
48             self.boarddraw = SeaFabric().get_widget(self.board)
49         else:
50             self.boarddraw = GrassFabric().get_widget(self.board)
51
52         self.boarddraw.connect("button-press-event",   self.press)
53         self.boarddraw.connect("button-release-event", self.release)
54         self.boarddraw.connect("quit-clicked", lambda w: self.window.destroy())
55         self.boarddraw.connect("reset-clicked", lambda w: self.reset())
56         self.boarddraw.connect("card-flipped", lambda w: self.card_flip())
57
58         self.boarddraw.show()
59
60         self.window.add(self.boarddraw)
61
62     def card_flip(self):
63         if self.board.get_state() == SHIPPING and self.board.pile.can_flip_card():
64                 self.board.pile.flip_card()
65
66
67     def press(self, widget, event):
68         assert not self.drag_from
69
70         # Doppelclick ignorieren
71         if event.type != gtk.gdk.BUTTON_PRESS:
72             return
73
74         slot = self.boarddraw.event2slot(event)
75
76         if self.board.get_state() == SHIPPING:
77             if slot:
78                 if slot.ship:
79                     self.drag_from = slot
80                     self.board.hand.move_ship_from(slot)
81
82         elif self.board.get_state() == PLACING:
83             if slot:
84                 if slot.card and not slot.card.placed:
85                     self.drag_from = slot
86                     self.board.hand.set_card(slot.card)
87                     slot.card = None
88
89         self.boarddraw.queue_draw()
90
91     def release(self, widget, event):
92         slot = self.boarddraw.event2slot(event)
93         if self.board.hand.ship:
94             if slot and slot.can_drop_ship():
95                 slot.move_ship_from(self.board.hand)
96                 self.drag_from = None
97             else:
98                 self.drag_from.move_ship_from(self.board.hand)
99                 self.drag_from = None
100
101         if self.board.hand.card:
102             if slot and slot.can_drop_card(self.board.hand.card):
103                 slot.set_card(self.board.hand.card)
104                 self.board.hand.card = None
105                 self.drag_from = None
106             else:
107                 self.drag_from.set_card(self.board.hand.card)
108                 self.board.hand.card = None
109                 self.drag_from = None
110
111         self.boarddraw.queue_draw()
112         self.board.calc_points()
113
114     def main(self):
115         gtk.main()
116
117 if __name__ == "__main__":
118     game = Game()
119     game.main()
120
121
122 # vim:ts=4:sw=4:sts=4:et