Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/game.py
blob: c873afcfa266cb8c9b0aa1d5cd1f5484c6cedf27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python

# Copyright (C) 2012 Agustin Zubiaga <aguz@sugarlabs.org>
# Copyright (C) 2012 Cristhofer Travieso <cristhofert97@gmail.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import cairo
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from sugar3.graphics import style

LINE_WIDTH = 12
SIZE = style.zoom(150)
BETWEEN_LINE_SPACE = style.zoom(200)

MODE_CIRCLE = 0
MODE_CROSS = 1

TICTACTOE_COLOR = (1, 1, 1)
CROSS_COLOR = (0, 0.921568, 0.0627450)
CIRCLE_COLOR = (1, 0.556862, 0)
BACKGROUND_COLOR = (0.352941, 0.349019, 0.709803)


class Canvas(Gtk.DrawingArea):

    __gsignals__ = {'cross-added': (GObject.SignalFlags.RUN_FIRST, None, [int]),
                    'circle-added': (GObject.SignalFlags.RUN_FIRST,
                                     None, [int])}

    def __init__(self):
        Gtk.DrawingArea.__init__(self)

        self._mode = MODE_CROSS
        self.circles = []
        self.crosses = []
        self.squares = []
        self.cursor = None

        self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
                        Gdk.EventMask.POINTER_MOTION_MASK)

        self.connect('draw', self._draw_cb)
        self.connect('button-press-event', self._click_cb)
        self.connect('motion-notify-event', self._motion_notify_cb)

        self.show_all()

    def set_mode(self, mode=MODE_CROSS):
        self._mode = mode

    def clear(self):
        self.circles = []
        self.crosses = []
        self.queue_draw()

    def _draw_cb(self, widget, context):
        alloc = self.get_allocation()

        # Background
        context.rectangle(0, 0, alloc.width, alloc.height)
        context.set_source_rgb(*BACKGROUND_COLOR)
        context.fill()

        # Tic-Tac-Toe
        context.set_source_rgb(*TICTACTOE_COLOR)
        context.set_line_width(LINE_WIDTH)
        context.set_line_cap(cairo.LINE_CAP_ROUND)

        x = (alloc.width - BETWEEN_LINE_SPACE * 3) / 2
        y = (alloc.height - BETWEEN_LINE_SPACE * 3) / 2

        y += BETWEEN_LINE_SPACE
        lx = (BETWEEN_LINE_SPACE + (LINE_WIDTH * 8.2)) * 3
        context.move_to(x, y)
        context.line_to(lx, y)

        size = SIZE / 2 + style.zoom(25)

        cx1 = x + size
        cx2 = x + BETWEEN_LINE_SPACE + size
        cx3 = x + (BETWEEN_LINE_SPACE * 2) + size

        x += BETWEEN_LINE_SPACE
        y -= BETWEEN_LINE_SPACE
        screen_width = Gdk.Screen.width()
        infactor = 1.5 + ((screen_width - 1024) / 100.0)
        ly = (BETWEEN_LINE_SPACE + (LINE_WIDTH * infactor)) * 3
        context.move_to(x, y)
        context.line_to(x, ly)

        cy1 = y + size
        cy2 = y + BETWEEN_LINE_SPACE + size
        cy3 = y + (BETWEEN_LINE_SPACE * 2) + size

        y += BETWEEN_LINE_SPACE * 2
        x -= BETWEEN_LINE_SPACE
        context.move_to(x, y)
        context.line_to(lx, y)

        x += BETWEEN_LINE_SPACE * 2
        y -= BETWEEN_LINE_SPACE * 2
        context.move_to(x, y)
        context.line_to(x, ly)
        context.stroke()

        # Save center squares coords
        if not cx1 < 0:
            self.squares = []
            self.squares.extend([(cx1, cy1), (cx2, cy1), (cx3, cy1)])
            self.squares.extend([(cx1, cy2), (cx2, cy2), (cx3, cy2)])
            self.squares.extend([(cx1, cy3), (cx2, cy3), (cx3, cy3)])

        # Draw circles and crosses
        context.set_source_rgb(*CIRCLE_COLOR)
        for circle in self.circles:
            if circle == self.cursor:
                self.cursor = None
            x, y = circle
            context.arc(x, y, SIZE / 2, 0, 360)
            context.stroke()

        context.set_source_rgb(*CROSS_COLOR)
        for cross in self.crosses:
            if cross == self.cursor:
                self.cursor = None
            x, y = cross
            a = SIZE / 2
            context.move_to(x - a, y - a)
            context.line_to(x + a, y + a)

            context.move_to(x + a, y - a)
            context.line_to(x - a, y + a)
            context.stroke()

        # Draw cursor dot
        try:
            context.set_source_rgb(*TICTACTOE_COLOR)
            cx, cy = self.cursor
            context.arc(cx, cy, 9, 0, 360)
            context.fill_preserve()
            context.stroke()
        except:
            pass

    def _motion_notify_cb(self, widget, event):
        self.cursor = self.get_pos(event.x, event.y)
        self.queue_draw()

    def add_cross(self, square, emit_signal=True):
        self.crosses.append(
              square if type(square) == tuple else self.squares[square])
        if emit_signal:
            self.emit('cross-added', self.squares.index(square))
        self.queue_draw()

    def add_circle(self, square, emit_signal=True):
        self.circles.append(
              square if type(square) == tuple else self.squares[square])
        if emit_signal:
            self.emit('circle-added', self.squares.index(square))
        self.queue_draw()

    def _click_cb(self, widget, event):
        pos = self.get_pos(event.x, event.y)

        if self._mode == MODE_CROSS and pos:
            self.add_cross(pos)
        elif self._mode == MODE_CIRCLE and pos:
            self.add_circle(pos)

    def get_pos(self, mx, my):
        try:
            ypos = 0
            xpos = 0
            all_x = [s[0] for s in self.squares]
            all_y = [s[1] for s in self.squares]

            size = SIZE / 2 + style.zoom(25)

            if mx > all_x[0] and not mx > all_x[-1]:
                count = 0
                for x in all_x:
                    if not xpos:
                        count += 1
                        next_x = all_x[count]
                        if mx >= (x - size) and mx <= (next_x - size):
                            xpos = x

            elif mx < all_x[0]:
                xpos = all_x[0]

            elif mx > all_x[-1]:
                xpos = all_x[-1]

            if my > all_y[0] and not my > all_y[-1]:
                count = 0
                for y in all_y:
                    if not ypos:
                        count += 1
                        next_y = all_y[count]
                        if my >= (y - size) and my <= (next_y - size):
                            ypos = y

            elif my < all_y[0]:
                ypos = all_y[0]

            elif my > all_y[-1]:
                ypos = all_y[-1]

            pos = xpos, ypos
            if pos in self.crosses and pos in self.circles:
                pos = None
            return pos

        except IndexError:
            return None

if __name__ == "__main__":
    window = Gtk.Window()
    window.connect('destroy', lambda w: Gtk.main_quit())
    window.add(Canvas())
    window.maximize()
    window.show_all()
    Gtk.main()