Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Quinteti.activity/gui/board.py
blob: a0d9deb575f74738f35d60ccdb27bf538b10cfaa (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
#
# Copyright 2008, 2009 Pablo Moleri, ceibalJAM
# This file is part of Quinteti.
#
# Quinteti 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 3 of the License, or
# (at your option) any later version.
#
# Quinteti 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 Quinteti.  If not, see <http://www.gnu.org/licenses/>.

"""Board represents the game board, and its capable of paint its elements in a given surface."""

import pygame

import os

from logic.game import GameState

from button import Button
from cell import Cell

file_dir = "gui/"

image_fondo = file_dir + "background.png"
image_tablero = file_dir + "tablero.png"
image_null = "nulo.bmp"
image_number = "<N>.png"
image_disabled_number = "<N>selected.png"
image_size = pygame.Rect(0, 0,  97,  97)

new_image_coords = (180,  87)
new_image = "quinteti-new.png"

instructions_coords = (950, 745)
instructions_button = "instructions_button.png"
instructions_image = "instructions.png"

player_win_image = "player_win.png"

score_sound_file = file_dir + "jupeee.ogg"

font_name = "DejaVu Serif"  #"DejaVuLGCSerif.ttf"  # None  to load pygame default font
font_size = 24
user_font_color = (255, 255, 255)

"""Class Board keeps all the grafical elements as well as a reference to the logical game state."""
class Board:
    
    # Center of initial number positions
    number_locations = [
        ([756+138*0,  231+138*0]), 
        ([756+138*1,  231+138*0]), 
        ([756+138*2,  231+138*0]), 
        ([756+138*0,  231+138*1]), 
        ([756+138*1,  231+138*1]), 
        ([756+138*2,  231+138*1]), 
        ([756+138*0,  231+138*2]), 
        ([756+138*1,  231+138*2]), 
        ([756+138*2,  231+138*2])]
    
    screen = None
    # Center of board cells
    locations = [
        ([267, 228]),
        ([404, 228]),
        ([541, 228]),
        ([267, 367]),
        ([404, 367]),
        ([541, 367]),
        ([267, 510]),
        ([404, 510]),
        ([541, 510])]

    players_name_midleft_location = [
                             (200,  667), 
                             (200,  752)]
    
    players_score_center_location = [
                             (581,  667), 
                             (581,  752)]

    players_score_box_location = [
                             (173,  628), 
                             (173,  714)]

    def __init__(self, screen, game = None):
        self.font = None
        if font_name:
            self.font = pygame.font.SysFont(font_name, font_size)

        if not self.font:
            self.font = pygame.font.Font(None, font_size)
        
        self.screen = screen
        self.game = game
        self.showing_instructions = False
        self.score_sound = pygame.mixer.Sound(score_sound_file)
        self.init_board()

    def init_board (self):
        self.new_button = Button(new_image_coords, file_dir + new_image,  self.new_game)
        self.instructions_button = Button(instructions_coords, file_dir + instructions_button,  self._show_instructions)
        self.cells = []
        self.numbers = []
        self.lastSelectedBoardCell = None
        self.lastSelectedNumberCell = None
        
        self.backgroundImage = pygame.image.load(image_fondo)
        
        self._init_cells()
        self._init_numbers()
        
        # Creates a sprite group, with all the board visible elements inside
        self._paint_background()
        self.items = pygame.sprite.Group()
        self.items.add(self.new_button)
        self.items.add(self.instructions_button)
        
        for n in self.numbers:
            self.items.add(n)
            
        self.arrange_gui()  # Arranges the gui according to the game state.

    def new_game(self):
        self.game = GameState("", "")
        self.init_board()
        
    def _init_cells(self):
        i = 1
        for row in range(1, 4):
            for col in range(1, 4):
                if self.game:
                    number = self.game.get_cell(row, col)[0]
                else:
                    number = None
                location = self.locations[i-1]
                self.cells.append( Cell(location, None, None, i, image_size) )
                i += 1
    
    def _init_numbers(self):
        k = 0
        for location in self.number_locations:
            k += 1
            normal_image = self._get_number(k)
            selected_image = self._get_disabled_number(k)
            self.numbers.append( Cell(location, normal_image, selected_image, k, image_size) )

    def set_players(self, name_player1, name_player2):
        self.game = GameState(name_player1, name_player2)
   
    def _paint_background(self):
        rect = self.backgroundImage.get_rect()
        rect.topleft = (0,  0)
        self.screen.blit(self.backgroundImage, rect)

    def _paint_winner(self,  i):
        image = pygame.image.load(file_dir + player_win_image)
        rect = image.get_rect()
        rect.topleft = self.players_score_box_location[i]
        self.screen.blit(image,  rect)
    
    def _paint_players_status(self):
        player1Name = "" 
        player2Name = ""
    
        if (self.game):
            for i in range(1,3):
                if self.game.get_enabled_player():
                    if self.game.get_enabled_player() == i:
                        self.font.set_bold(True)
                    else:
                        self.font.set_bold(False)
                else:
                    if self.game.get_player_score(i) >= self.game.get_player_score(3-i):
                        self._paint_winner(i-1)
                    
                player_name = self.game.get_player_name(i)
                #str_player = 'Jugador %s: %s' % (i, player_name)
                str_player = 'Jugador %s' % (i)
                name_surface = self.font.render(str_player, 1, user_font_color)
                name_rect = name_surface.get_rect()
                name_rect.midleft = self.players_name_midleft_location[i-1]
                self.screen.blit(name_surface, name_rect)
                
                player_score = self.game.get_player_score(i)
                str_player_score = '%s' % (player_score)
                score_surface = self.font.render(str_player_score, 1, user_font_color)
                score_rect = score_surface.get_rect()
                score_rect.center = self.players_score_center_location[i-1]
                self.screen.blit(score_surface, score_rect)

    def paint_board_elements(self):
        # Using an sprite group all the items are painted:
        
        #self.items.clear(self.screen,  self.backgroundImage)   # If only sprites are cleared, players scores remain
        self._paint_background()                                                  # Instead, the whole background is repainted
        self.items.draw(self.screen)
        self._paint_players_status()
        
        if self.showing_instructions:
            self._paint_instructions()

    def _paint_instructions(self):
        image = pygame.image.load(file_dir + instructions_image)
        rect = image.get_rect()
        rect.center = self.screen.get_rect().center
        self.screen.blit(image, rect)

    def arrange_gui(self):
        '''Arranges the numbers according to the game state.'''
        
        # First moves all the numbers to its original positions
        i = 0
        for number in self.numbers:
            number.rect.center = self.number_locations[i]
            i += 1
        
        # Then for each occupied cell, moves the number to that cell
        coords = [ (row,col) for row in range(1,4) for col in range(1,4) ]
        i = 0
        for row, col in coords:
            cell = self.game.get_cell(row, col)
            if cell:
                number, player = cell
                if number > 0:
                    gui_number = self.numbers[number-1]
                    gui_cell = self.cells[i]
                    gui_number.rect.center = gui_cell.rect.center
            i += 1
    
    def processXY(self, x, y):
        # If is showing instructions, it disables them
        if self.showing_instructions:
            self.showing_instructions = False
            return
        else:
            if self.instructions_button.coords_in(x, y):
                self.instructions_button.callback()
        
        # Checks if the selected coordinate is a board cell
        isCell = False
        for c in self.cells:
            if c.coords_in(x, y):
                isCell = True
                self.lastSelectedBoardCell = c
                if self.lastSelectedNumberCell != None:
                    row, col = c.get_pos()
                    player = self.game.get_enabled_player()
                    ok, hits = self.game.make_move(row, col, self.lastSelectedNumberCell.id_cell, player)
                    if ok:
                        # Moves the number to the board
                        self.lastSelectedNumberCell.rect.center = self.lastSelectedBoardCell.rect.center
                        self.lastSelectedNumberCell.set_selected(False)
                        self.lastSelectedNumberCell = None
                        
                        if hits:
                            self.score_sound.play()
                            # Sets a timer to update blinked cells in one second
                            pygame.time.set_timer(pygame.USEREVENT + 1, 1500)
                            for number in self.numbers:
                                if number.id_cell in hits:
                                    number.set_selected(True)

                        player = self.game.get_enabled_player()
                        self.game.auto_play(player)
                        self.arrange_gui()
                break
    
        # Checks if the selected coordinate is a number
        if isCell == False:
            for n in self.numbers:
                if n.coords_in(x,y):
                    if self.lastSelectedNumberCell:
                        self.lastSelectedNumberCell.set_selected(False)
                    self.lastSelectedNumberCell = n
                    n.set_selected(True)
                    
        if self.new_button.coords_in(x, y):
            self.new_button.callback()
        
        return True
    
    def user_event(self, event):
        pygame.time.set_timer(pygame.USEREVENT + 1, 0)
        if event.type == pygame.USEREVENT + 1:
            # Deselect all numbers
            for number in self.numbers:
                number.set_selected(False)
            
            #player = self.game.get_enabled_player()
            #self.game.auto_play(player)
            #self.arrange_gui()
        
    def _show_instructions(self):
        self.showing_instructions = True
    
    def _get_number(self, number):
        if (number == None) or (number == 0):
            return None
        else:
            path = os.path.join(file_dir, image_number.replace("<N>", str(number)))
            return pygame.image.load(path)

    def _get_disabled_number(self, number):
        if (number == None) or (number == 0):
            return None
        else:
            path = os.path.join(file_dir, image_disabled_number.replace("<N>", str(number)))
            return pygame.image.load(path)