diff options
author | Alan Aguiar <alanjas@hotmail.com> | 2013-11-30 17:34:33 (GMT) |
---|---|---|
committer | Alan Aguiar <alanjas@hotmail.com> | 2013-11-30 17:34:33 (GMT) |
commit | cd74f2263a5e5f0b8aa13b7460d982eae2f05c54 (patch) | |
tree | df81afab9b4994b3fd1c097cd41d8da8cf481171 | |
parent | f4933f665844f0a09b4879cc84b3c423ce47ab01 (diff) |
add game end message
-rwxr-xr-x | main.py | 106 |
1 files changed, 84 insertions, 22 deletions
@@ -4,10 +4,13 @@ import pygame import gtk +from gettext import gettext as _ LINE_SIZE = 10 T = 15 COLOR_OWNER = (255, 0, 0) +PLAYER_A = 0 +PLAYER_B = 0 class box: @@ -29,13 +32,25 @@ class box: self.left = 0 self.right = 0 self.down = 0 - #self.owner = 0 + self.owner = None def check(self): return ((self.up + self.left + self.right + self.down) == 4) - def showText(self, texto): - text = self.fuente.render(texto, 1, COLOR_OWNER) + def setOwner(self, owner): + global PLAYER_A + global PLAYER_B + if owner == 'A': + self.owner = 'A' + PLAYER_A = PLAYER_A + 1 + else: + self.owner = 'B' + PLAYER_B = PLAYER_B + 1 + self.showOwner() + print PLAYER_A, PLAYER_B + + def showOwner(self): + text = self.fuente.render(self.owner, 1, COLOR_OWNER) textrect = text.get_rect() textrect.center = (self.pos_x, self.pos_y) self.screen.blit(text, textrect) @@ -45,7 +60,8 @@ class Game: def __init__(self, parent=None): self.parent = parent self.current = 'A' - self.grid_size = (8, 6) + self.grid_size = (4, 4) + self.calc_grid_cant() self.box_size = (50, 50) self.x_offset = 100 self.y_offset = 100 @@ -56,10 +72,41 @@ class Game: def draw_line(self, r1, r2): pygame.draw.line(self.screen, self.line_color, r1, r2, LINE_SIZE) + def draw_game_end(self): + s = self.screen.get_size() + alphasurface = pygame.Surface(s) + alphasurface.convert() + alphasurface.fill((100,100,100)) + alphasurface.set_alpha(200) + self.screen.blit(alphasurface, (0, 0)) + winner = None + if PLAYER_A > PLAYER_B: + winner = 'A' + elif PLAYER_B > PLAYER_A: + winner = 'B' + if winner == None: + msg = _('Empate') + else: + msg = _('Player %s win!') % winner + text = self.fuente2.render(msg, 1, COLOR_OWNER) + textrect = text.get_rect() + s = self.screen.get_size() + textrect.center = (int(s[0] / 2), int(s[1] / 2)) + self.screen.blit(text, textrect) + text = self.fuente.render(_('Click to start a new game'), 1, COLOR_OWNER) + textrect = text.get_rect() + s = self.screen.get_size() + textrect.center = (int(s[0] / 2), int(s[1] / 2) + 60) + self.screen.blit(text, textrect) + def set_board_size(self, size): self.grid_size = size + self.calc_grid_cant() self.draw_grid() + def calc_grid_cant(self): + self.grid_cant = (self.grid_size[0] - 1) * (self.grid_size[1] - 1) + def set_point_color(self, color): self.point_color = color self.draw_grid() @@ -78,13 +125,16 @@ class Game: self.draw_grid() def draw_grid(self): + global PLAYER_A + global PLAYER_B + PLAYER_A = 0 + PLAYER_B = 0 w = self.grid_size[0] h = self.grid_size[1] s_w = self.screen.get_width() s_h = self.screen.get_height() if s_w < (w * (self.box_size[0] + 1)): - print 'pasa x' value = int(s_w / (self.box_size[0] + 1.0)) - 1 self.parent.h_spin_set_max(value) return @@ -93,7 +143,6 @@ class Game: self.x_offset = int((s_w - xx) / 2.0) + LINE_SIZE * 2 if s_h < (h * (self.box_size[1] + 1)): - print 'pasa y' value = int(s_h / (self.box_size[1] + 1.0)) - 1 self.parent.v_spin_set_max(value) return @@ -146,7 +195,6 @@ class Game: x, y = pos r1 = self.where_x(x) r2 = self.where_y(y) - #print r1, r2 if not(r1[0] == False): if not(r2[0] == False): if x < (r1[0] + T): @@ -158,13 +206,13 @@ class Game: con = False b.left = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True if x_b > 0: b2 = self.boxes[x_b - 1][y_b] b2.right = 1 if b2.check(): - b2.showText(self.current) + b2.setOwner(self.current) con = True self.draw_line((r1[0],r2[0]), (r1[0],r2[1])) return con @@ -177,14 +225,13 @@ class Game: con = False b.right = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True - #print 'aca', x_b, y_b, self.grid_size[0] if x_b < (self.grid_size[0] - 2): b2 = self.boxes[x_b + 1][y_b] b2.left = 1 if b2.check(): - b2.showText(self.current) + b2.setOwner(self.current) con = True self.draw_line((r1[1],r2[0]), (r1[1],r2[1])) return con @@ -199,7 +246,7 @@ class Game: con = False b.left = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True self.draw_line((self.x_offset,r2[0]), (self.x_offset,r2[1])) return con @@ -213,7 +260,7 @@ class Game: con = False b.right = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True self.draw_line((self.x_end,r2[0]), (self.x_end,r2[1])) return con @@ -229,13 +276,13 @@ class Game: con = False b.up = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True if y_b > 0: b2 = self.boxes[x_b][y_b-1] b2.down = 1 if b2.check(): - b2.showText(self.current) + b2.setOwner(self.current) con = True self.draw_line((r1[0],r2[0]), (r1[1],r2[0])) return con @@ -248,13 +295,13 @@ class Game: con = False b.down = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True if y_b < self.grid_size[1] - 2: b2 = self.boxes[x_b][y_b+1] b2.up = 1 if b2.check(): - b2.showText(self.current) + b2.setOwner(self.current) con = True self.draw_line((r1[0],r2[1]), (r1[1],r2[1])) return con @@ -269,7 +316,7 @@ class Game: con = False b.up = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) con = True self.draw_line((r1[0],self.y_offset), (r1[1],self.y_offset)) return con @@ -283,7 +330,7 @@ class Game: con = False b.down = 1 if b.check(): - b.showText(self.current) + b.setOwner(self.current) self.draw_line((r1[0],self.y_end), (r1[1],self.y_end)) return False @@ -293,10 +340,11 @@ class Game: self.screen = pygame.display.get_surface() if not self.screen: self.screen = pygame.display.set_mode((900, 700)) + pygame.display.set_caption(_('Dots and boxes')) self.screen.fill(self.back_color) self.fuente = pygame.font.Font(None, self.box_size[0]) + self.fuente2 = pygame.font.Font(None, self.box_size[0] * 2) self.draw_grid() - run = True while run: while gtk.events_pending(): @@ -315,8 +363,22 @@ class Game: self.current = 'A' if self.parent: self.parent.set_current_player(self.current) - pygame.display.flip() + if self.grid_cant == (PLAYER_A + PLAYER_B): + self.draw_game_end() + run2 = True + while run2: + while gtk.events_pending(): + gtk.main_iteration() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + run2 = False + self.draw_grid() + elif event.type == pygame.MOUSEBUTTONDOWN: + run2 = False + self.draw_grid() + pygame.display.flip() if __name__ == '__main__': g = Game() |