Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Quinteti.activity/gui/board.py
blob: 5cf579290e9b8fdb4d5f28edec9c0e6175d7d14d (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
import pygame
from pygame.locals import *
from logic.GameState import GameState

from Button import Button
from Cell import Cell
from Player import Player

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"

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

class BoardUI:
    
    # 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.initBoard()

    def initBoard (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.initCells()
        self.initNumbers()
        
        # Creates a sprite group, with all the board visible elements inside
        self.paintBackground()
        self.items = pygame.sprite.Group()
        self.items.add(self.new_button)
        self.items.add(self.instructions_button)
        for c in self.cells:
            if c.image:
                self.items.add( c )
            
        for n in self.numbers:
            if n.image:
                self.items.add( n )

    def new_game(self):
        self.game = GameState("", "")
        self.initBoard()
        
    def initCells(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, self.get_number_name(number), i,  image_size) )
                i += 1
    
    def initNumbers(self):
        k = 0
        for location in self.number_locations:
            k += 1
            self.numbers.append(Cell(location, self.get_number_name(k), k,  image_size)) 

    def setPlayers (self, namePlayer1, namePlayer2):
        self.game = GameState(namePlayer1, namePlayer2)
   
    def paintBackground(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 paintPlayersStatus(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 paintBoardElements(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.paintBackground()                                                  # Instead, the whole background is repainted
        self.items.draw(self.screen)
        self.paintPlayersStatus()
        
        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 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.coordsIn(x, y):
                self.instructions_button.callback()
        
        # Checks if the selected coordinate is a board cell
        isCell = False
        for c in self.cells:
            if c.coordsIn(x,y):
                isCell = True
                self.lastSelectedBoardCell = c
                if self.lastSelectedNumberCell != None:
                    row, col = c.get_pos()
                    player = self.game.get_enabled_player()
                    ok = self.game.make_move(row, col, self.lastSelectedNumberCell.idxCell, player)
                    if ok:
                        self.lastSelectedBoardCell.setImage( self.get_number_name(self.lastSelectedNumberCell.idxCell) )
                        self.items.add(self.lastSelectedBoardCell)
                        self.items.remove(self.lastSelectedNumberCell)
                        self.lastSelectedNumberCell.setImage(None)
                        self.lastSelectedNumberCell = None
                break
    
        # Checks if the selected coordinate is a number
        if isCell == False:
            for n in self.numbers:
                if n.coordsIn(x,y):
                    if self.lastSelectedNumberCell:
                        self.lastSelectedNumberCell.setImage( self.get_number_name(self.lastSelectedNumberCell.idxCell) )
                    self.lastSelectedNumberCell = n
                    n.setImage( self.get_disabled_number_name(n.idxCell) )
                    
        if self.new_button.coordsIn(x, y):
            self.new_button.callback()
    
    def show_instructions(self):
        self.showing_instructions = True
    
    def get_number_name(self, number):
        if (number == None) or (number == 0):
            return None
        else:
            return file_dir +  image_number.replace("<N>", str(number))

    def get_disabled_number_name(self, number):
        if (number == None) or (number == 0):
            return None
        else:
            return file_dir +  image_disabled_number.replace("<N>", str(number))