#!/usr/bin/env python import pygame from chatwisted.pygame_gui import PygamePluginClient from chatwisted.spg import gui class Connect4Field(pygame.Surface): """ A surface that displays a Connect4 game grid """ colors = {"X":(17, 95, 210), "O":(241, 63, 63), "_":(255,255,255)} def __init__(self, width, height, put_func, highlight_key = "X"): """ Define and set some attributes """ pygame.Surface.__init__(self, (width, height), pygame.RLEACCEL) self.highlight_color = self.colors[highlight_key] self.put_func = put_func self.col_width = 0 def draw_field(self, rows): """ redraw the whole filed """ self.fill((0,0,0,0)) width, height = self.get_size() self.col_width = col_width = int(width)/len(rows[0]) row_height = int(height)/len(rows) for row, items in enumerate(rows): for col, item in enumerate(items): left, top = col * col_width, row * row_height color = self.colors[item] oval = pygame.draw.ellipse(self, color, pygame.Rect(left, top, col_width, row_height)) def click(self, pos): x, y = pos if 0 5: self.logged_lines = self.logged_lines[-5:] self.console.text = "\n".join(self.logged_lines) # self.gui.events = events self.desktop.update() self.blit(self.field, self.field_rect) mouse_pos = pygame.mouse.get_pos() if self.field_rect.collidepoint (mouse_pos) and self.field.col_width: rect = pygame.Rect(0,0,self.field.col_width, self.field.col_width) rect.center = mouse_pos[0], self.field_rect.top pygame.draw.ellipse(self, (17, 95, 210), rect) self.desktop.draw(self) def send_message(self, dummy=None): """ Sends a line to the server """ line = self.entry.text if line: if PygamePluginClient.send_message(self, line): self.entry.text = "" def cmd_field(self, field): """ Update the game grid """ lines = [line for line in field.replace("|", "").split("-") if line.strip()] self.field.draw_field(lines) def put(self, col): """ Put a chip into column col """ self.send_cmd("put", col) ############### # def highlight(self, chip): # """ # highlight a chip # :type chip: TagOrID # """ # self.itemconfig(chip, fill="black")#self.highlight_color) # def set_normal(self, chip, item): # """ # un-highlight a chip # :tpye chip: TagOrID # :param item: Item to represent with the chip (to get the color) # :type item: string # """ # self.itemconfig(chip, fill=self.colors[itm]) ################### #class TkConnect4Client(TkPluginClient): # """ # A simple connect4 game client with Tkinter # :todo: write and inherit as well from a gui independent Connect4Client class # """ # name = "connect4" # def __init__(self, master, facotry, name): # """ # Pack a TkConnect4Field on the predefined main_frame and add a Button # to start the game # """ # TkPluginClient.__init__(self, master, facotry, name) # self.game_frame = tk.Frame(self) # self.field = TkConnect4Field(self.main_frame, 200, 200, self.put) # self.field.pack() # self.start_but = tk.Button(self.main_frame, text="start", # command=lambda:self.send_cmd("start", "")) # self.start_but.pack() # def cmd_field(self, field): # """ # Update the game grid # """ # lines = [line for line in field.replace("|", "").split("-") # if line.strip()] # self.field.draw_field(lines) # def put(self, col): # """ # Put a chip into column col # """ # self.send_cmd("put", col)