Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/run.py
blob: 868a6310148dad9a318cb2edf671494891cfb9b6 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
#! /usr/bin/env python2.5
############################################################################
#    Copyright (C) 2007 by kawk                                            #
#    kawk@theprogrammingsite.com                                           #
#                                                                          #
#    This file is the main menu for the FiftyTwo card game set.            #
#    FiftyTwo is a set of card games for the OLPC XO laptop.               #
#                                                                          #
#    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.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import gtk
import pygame
from pygame import camera

import crazyeights

import gui



#CARDBACK_IMAGE = 'back.regular.png'
#DECK_IMAGE = 'cards.regular.png'

MMCOL = (192, 64, 255)

class deckinfoclass():
    def __init__(self):
        self.deck = 'regular'

deck = deckinfoclass()

class Game():

    def __init__(self, parent):
        self.parent = parent
        pass

    def loop(self):
        pygame.init()

        self.screen = pygame.display.get_surface()
        if self.screen:
            self.screensize = self.screen.get_size()
        else:
            info = pygame.display.Info()
            self.screensize = (info.current_w, info.current_h)
            self.screen = pygame.display.set_mode(self.screensize)

        screensize = self.screensize

        decksel = False
        c8sheader = gui.label((100, 300), (200, 75), 'Crazy Eights', (0, 128, 0))
        c8s2p = gui.button((100, 400), (200, 35), "Two players")
        c8s3p = gui.button((100, 450), (200, 35), "Three players")
        c8s4p = gui.button((100, 500), (200, 35), "Four players")
        
        c8sframe = gui.frame((c8sheader, c8s2p, c8s3p, c8s4p), (75, 275), (250, 275))
        
        mainquit = gui.button((50, screensize[1]-75), (100, 35), "Quit")
        maindeck = gui.button((screensize[0]-150, screensize[1]-75), (100, 35), "Deck")
        mainphoto = gui.button(((screensize[0]/2)-75, (screensize[1]-75)), (150, 35), "Photo deck")
        mainheader = gui.image((50, 25), (screensize[0]-100, 200), 'fiftytwo.png', -1)
        
        mainframe = gui.frame((mainheader, mainquit, maindeck, mainphoto), (25, screensize[1]-100), (screensize[0]-50, 75))

        deckselexit = gui.button((screensize[0]-215, screensize[1]-145), (115, 35), "Cancel")
        deckfromjournal = gui.button((screensize[0]-445, screensize[1]-145), (200, 35), "from Journal")
        deckselregular = gui.image((115, 115), (crazyeights.card_width(), crazyeights.card_height()), 'back.regular.png')
        deckselgnome = gui.image((300, 115), (crazyeights.card_width(), crazyeights.card_height()), 'back.gnome.png')
        deckselparis = gui.image((485, 115), (crazyeights.card_width(), crazyeights.card_height()), 'back.paris.png')
        deckselshapes = gui.image((670, 115), (crazyeights.card_width(), crazyeights.card_height()), 'back.shapes.png')
        self.deckseluser = gui.image((855, 115), (crazyeights.card_width(), crazyeights.card_height()), 'back.user.tga')

        deckselpopup = gui.popup((deckselexit, deckfromjournal, deckselregular, deckselgnome, deckselparis, deckselshapes, self.deckseluser), (100, 100), (screensize[0]-200, screensize[1]-200))
        
        mouseposition = [0, 0, 0]

        self.screen.fill((192, 64, 255))
        
        fpslimiter = pygame.time.Clock()

        while True:
            fpslimiter.tick(20)

            while gtk.events_pending():
                gtk.main_iteration()

            for event in pygame.event.get():

                if event.type == pygame.MOUSEMOTION:
                    mouseposition[0] = event.pos[0]
                    mouseposition[1] = event.pos[1]
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouseposition[2] = 1
                elif event.type == pygame.MOUSEBUTTONUP:
                    mouseposition[2] = 0
                    if not decksel and c8s2p.detect_click(event.pos):
                        crazyeights.main(('aThe XO', 'lYou'), screensize)
                        self.screen.fill(MMCOL)
                        pygame.event.set_allowed(pygame.MOUSEMOTION)
                    elif not decksel and  c8s3p.detect_click(event.pos):
                        crazyeights.main(('aXO player #1', 'aXO player #2', 'lYou'), screensize)
                        self.screen.fill(MMCOL)
                        pygame.event.set_allowed(pygame.MOUSEMOTION)
                    elif not decksel and  c8s4p.detect_click(event.pos):
                        crazyeights.main(('aXO player #1', 'aXO player #2', 'lYou', 'aXO player #3'), screensize)
                        self.screen.fill(MMCOL)
                        pygame.event.set_allowed(pygame.MOUSEMOTION)
                    elif not decksel and maindeck.detect_click(event.pos):
                        decksel = True
                    elif not decksel and mainphoto.detect_click(event.pos):
                        self.photo()
                        self.screen.fill(MMCOL)
                    elif mainquit.detect_click(event.pos):
                        return
                    elif decksel and deckselexit.detect_click(event.pos):
                        decksel = False
                        self.screen.fill(MMCOL)
                    elif decksel and deckfromjournal.detect_click(event.pos):
                        picture = self.parent.load_image_from_journal()
                        if picture:
                            self.update_user_deck(picture.file_path)
                        self.screen.fill(MMCOL)
                    elif decksel and deckselgnome.detect_click(event.pos):
                        deck.deck = 'gnome'
                        decksel = False
                        self.screen.fill(MMCOL)
                    elif decksel and deckselregular.detect_click(event.pos):
                        deck.deck = 'regular'
                        decksel = False
                        self.screen.fill(MMCOL)
                    elif decksel and deckselparis.detect_click(event.pos):
                        deck.deck = 'paris'
                        decksel = False
                        self.screen.fill(MMCOL)
                    elif decksel and deckselshapes.detect_click(event.pos):
                        deck.deck = 'shapes'
                        decksel = False
                        self.screen.fill(MMCOL)
                    elif decksel and self.deckseluser.detect_click(event.pos):
                        deck.deck = 'user'
                        decksel = False
                        self.screen.fill(MMCOL)

        
            if not decksel:
                c8sframe.update()
                c8sframe.draw(self.screen, mouseposition)
            
                mainframe.update()
                mainframe.draw(self.screen, mouseposition)
            else:
                deckselpopup.update()
                deckselpopup.draw(self.screen, mouseposition)

            pygame.display.flip()
        
    def photo(self):
        screensize = self.screensize
        mouseposition = [0, 0, 0]
        self.screen = pygame.display.get_surface()
        fpslimiter = pygame.time.Clock()
        photodynamic = gui.image((125, 125), (640, 480), 'back.regular.png')
        photoreturn = gui.button((screensize[0]-300, screensize[1]-175), (150, 35), "Cancel")
        photopopup = gui.popup((photodynamic, photoreturn), (100, 100), (screensize[0]-200, screensize[1]-200))

        pygame.camera.init()
        cams = pygame.camera.list_cameras()
        photocamera = pygame.camera.Camera(cams[0], (640, 480), 'RGB')
        photocamera.start()
        photocamera.set_controls(True, False)
        camera_size = photocamera.get_size()
        capture = pygame.surface.Surface(camera_size, 0, self.screen)
        photodynamic.image = photocamera.get_image(capture)

        running = True
        
        while running:
            fpslimiter.tick(20)

            while gtk.events_pending():
                gtk.main_iteration()

            for event in pygame.event.get():
                
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        photodynamic.image = photocamera.get_image(capture)
                elif event.type == pygame.MOUSEMOTION:
                    mouseposition[0] = event.pos[0]
                    mouseposition[1] = event.pos[1]
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    mouseposition[2] = 1
                elif event.type == pygame.MOUSEBUTTONUP:
                    mouseposition[2] = 0
                    if photoreturn.detect_click(event.pos):
                        running = False
                    elif photodynamic.detect_click(event.pos):
                        r = photodynamic.image.get_rect()
                        r.x = mouseposition[0]-125
                        r.y = mouseposition[1]-125
                        r.w = 156
                        r.h = 244
                        sub = photodynamic.image.subsurface(r)
                        pygame.image.save(sub, 'data/back.user.tga')
                        deck.deck = 'user'
                        running = False
            
            photopopup.update()
            photopopup.draw(self.screen, mouseposition)
            r = self.screen.get_rect()
            r.x = 125
            r.y = 125
            r.w = 640
            r.h = 480
            self.screen.set_clip(r)
            
            r.x = mouseposition[0]
            r.y = mouseposition[1]
            r.w = 156
            r.h = 244
            
            pygame.draw.rect(self.screen, (255, 0, 0), r, 4)
            self.screen.set_clip()
            
            pygame.display.flip()

        photocamera.stop()

    def update_user_deck(self, path):
        try:
            raw_deck = pygame.image.load(path)
            deck = pygame.transform.scale(raw_deck, (156, 244))
            pygame.image.save(deck, 'data/back.user.tga')
            self.deckseluser.image = deck
        except:
            pass