def setup(self): """ initiates this screens attributes\n :return: None """ Screen.setup(self) self._get_background().fill(resource.get_color("startmenu_background").rgb()) self.__btns["START"] = TextButton(resource.get_string("btn_start_text")) self.__btns["INFO"] = TextButton(resource.get_string("btn_info_text")) self.__btns["EXIT"] = TextButton(resource.get_string("btn_exit_text")) screen_mid_w = (self.get_width() / 2) # game title dimen_title = resource.get_dimen("startscreen_title") game_title = resource.get_string("game_title") font_res = resource.get_font("startscreen_title") col = resource.get_color("startmenu_gametitle").rgb() font = pygame.font.SysFont(font_res.name, font_res.size) self.__text = font.render(game_title, 0, col) self.__text_dimen = resource.get_dimen("startscreen_title") self.__text_dimen.x = screen_mid_w - dimen_title.width # set buttons position button_marg = resource.get_dimen("button_marg") for i, btn in enumerate(self.get_btns().itervalues()): x = screen_mid_w - (btn.get_width() / 2) y = button_marg.top + ((i + 1) * button_marg.dist) btn.set_pos(x, y)
def __init__(self, cpu): dimen = resource.get_dimen("cpu") r = dimen.radius # create surface col_fill = resource.get_color("cpu_fill").rgb() col_bord = resource.get_color("cpu_border").rgb() self.__font_res = resource.get_font("cpu") img = pygame.Surface((dimen.diameter(), dimen.diameter()), pygame.SRCALPHA, 32) img.convert_alpha(img) pygame.draw.circle(img, col_fill, (r, r), r) pygame.draw.arc(img, col_bord, img.get_rect(), 3 * 2 / pi, 2 * 2 / pi, 5) ViewObject.__init__(self, img, cpu) # add the address on the surface self.__blit_adress(img, dimen)
def __init__(self, player): """ :param player: :type player: src.game.gameobjects.Player """ self.__player = player img = pygame.Surface(player.get_dimen().wh()) color = resource.get_color("player") img.fill(color.rgb()) ViewObject.__init__(self, img, player)
def setup(self): Screen.setup(self) gs_w = self.get_dimen().width gs_h = self.get_dimen().height gamefield_dimen = resource.get_dimen("gamefield") gf_w = gamefield_dimen.width gf_h = gamefield_dimen.height self.__scaling_factor_x = gs_w / (gf_w + 0.0) self.__scaling_factor_y = gs_h / (gf_h + 0.0) self._get_background().fill( resource.get_color("gamefield_background").rgb()) self.__buffer_view = BufferView(self.__game_controller.get_buffer()) if self.__game_controller is not None: gc = self.__game_controller # PLAYER self.__player_view = PlayerView(gc.get_player()) # CPU n_cpus = len(gc.get_cpus()) cpu_dimen = resource.get_dimen("cpu") top = (self.get_dimen().height / 2) - ( (n_cpus * cpu_dimen.diameter()) - (n_cpus * cpu_dimen.dist)) top /= 2 for idx, cpu in enumerate(gc.get_cpus()): x = cpu.get_dimen().left y = (top + cpu.get_dimen().dist * idx) cpu.set_pos(x, y) cpu_view = CPUView(cpu) self.__cpu_views.append(cpu_view) # PACKET for packet in gc.get_packets(): idx = gc.get_buffer().index(packet) self.__packet_views.append(PacketView(packet)) self.__trash_view = TrashView(gc.get_trash()) self.update()
def setup(self): """ :return: """ ViewScreen.setup(self) self._get_background().fill( resource.get_color("infoscreen_background").rgb()) # set back button position btn_back = self.get_btns()["BACK"] btn_y = self.get_height() - btn_back.get_height() - btn_back.get_dimen( ).bottom btn_back.set_pos(btn_back.get_dimen().left, btn_y) self.update()
class ResultScreen(ViewScreen): """ Screen used for use-case: result\n :author: Mikael Holmbom :version: 1.0 """ __endstate = None __article_player= None __article_player_pos = None __article_player_color = resource.get_color("result_article_player").rgb() __background_color = resource.get_color("resultscreen_background").rgb() __text_color = resource.get_color("resultscreen_text").rgb() __header_result = resource.get_string("result_header_player") def __init__(self): ViewScreen.__init__(self) self._dimen_article_padd = resource.get_dimen("result_article_padd") def set_endstate(self, endstate): """ set this endstate to display as result\n :param endstate: :return: """ self.__endstate = endstate def setup(self): ViewScreen.setup(self) self._get_background().fill(self.__background_color) self._font_res = resource.get_font("result_playerinfo") self._font = pygame.font.SysFont(self._font_res.name, self._font_res.size, self._font_res.style_bold, self._font_res.style_italic) self._font_res_header = resource.get_font("result_header") self._font_header = pygame.font.SysFont(self._font_res_header.name, self._font_res_header.size, self._font_res_header.style_bold,self._font_res_header.style_italic) self.__article_player = self.__create_playerinfo_view() def __create_playerinfo_view(self): """ create article displaying player result\n :return: """ es = self.__endstate rows = [] rows.append("score : " + str(es.score)) rows.append("level : " + str(es.level)) header = self.__header_result.upper() width, height = self._get_article_size(header, rows) surf = pygame.Surface((width, height)) surf.fill(self.__article_player_color) main_surf = self._get_main_surface() mid_x = (main_surf.get_width() / 2) - (width / 2) mid_y = (main_surf.get_height() / 2) - (height / 2) self.__article_player_pos = mid_x, mid_y self._blit_rows(surf, header, self.__text_color, rows) return surf def render(self): ViewScreen.render(self) mainsurf = self._get_main_surface() mainsurf.blit(self.__article_player, self.__article_player_pos)
class BufferView(ViewObject): """ Represents the the buffer\n :author: Mikael Holmbom :version: 1.0 """ __background_color = resource.get_color("buffer_view_background").rgb() __background_surface = None __dimen = resource.get_dimen("buffer_view") __dimen_border = resource.get_dimen("buffer_view_border") __dimen_padd = resource.get_dimen("buffer_view_padd") __image = None def __init__(self, buffer): self.__background_surface = self.create_background() ViewObject.__init__(self, self.__background_surface) self.set_gameobject(buffer) pos = self.__dimen.x, self.__dimen.y self.set_rect(pygame.Rect(pos, self.__dimen.wh())) def create_background(self): """ creates a background for this buffer :return: """ buf_cap = resource.get_value("buffer_capacity") packet_dimen = resource.get_dimen("packet") padd = self.__dimen_padd self.__dimen.width = (padd.left * 2) + packet_dimen.width self.__dimen.height = ((packet_dimen.height + self.__dimen.dist) * buf_cap) + (padd.top * 2) surf = pygame.Surface(self.__dimen.wh()) surf.fill(self.__background_color) border_size = self.__dimen_border.size border_color = tools.shift_intensiveness(self.__background_color, -50) surf = tools.add_shadow(surf, border_color, border_size) return surf def get_logic_pos(self): """ get the logic position of this view\n :return: """ return self.__dimen.pos() def render(self): buf = self.get_gameobject() surf = self.__background_surface.copy() dimen = self.__dimen x = self.__dimen_padd.left for p in buf: idx = buf.index(p) pd_h = p.get_dimen().height y = self.__dimen_padd.top + ((self.__dimen.dist + pd_h) * idx) p.set_pos(dimen.x + x, dimen.y + y) p_view = PacketView(p) surf.blit(p_view.render(), (x, y)) return surf
class GameScreen(Screen): """ defines screen for use-case game\n :author: Mikael Holmbom :version: 1.0 """ __infopanel = None __infopanel_dimen = resource.get_dimen("infopanel") __gamefield_screen = None __game_controller = None __background_color = resource.get_color("gamescreen_background").rgb() def __init__(self, dimen=resource.get_dimen("gamescreen")): Screen.__init__(self, dimen) self.__infopanel = InfoPanel() self.__gamefield_screen = GameFieldScreen() # setup background self._set_background(pygame.Surface(self.get_dimen().wh())) self._get_background().fill(self.__background_color) # init the dimension of the gamefield dimen_gamefield = resource.get_dimen("gamefieldscreen") self.__gamescreen_margin_left = (self.get_dimen().width / 2) - (dimen_gamefield.width / 2) self.__gamescreen_margin_top = (self.get_dimen().height / 2) - (dimen_gamefield.height / 2) self.get_btns()["SOUND"] = self.__create_soundfx_button() def __create_soundfx_button(self): """ create a soundfx switchbutton instance\n :return: SwitchButton instance """ switch_btn_dimen = resource.get_dimen("sound_switch_button") btn_surf_on = pygame.image.load(resource.get_imagesrc("soundfx_on")) btn_surf_off = pygame.image.load(resource.get_imagesrc("soundfx_off")) return SwitchButton(btn_surf_on, btn_surf_off, settings.is_soundfx_enabled(), switch_btn_dimen) def get_infopanel(self): """ get this infopanel :return: infopanel attr instance """ return self.__infopanel def set_game_controller(self, game_controller): """ set a game_controller to get all viewing objects from :param game_controller: :return: None """ self.__game_controller = game_controller self.__gamefield_screen.set_game_controller(game_controller) def click(self, pos): """ delegate a click on this screen at position param pos\n :param pos: the position of the click :return: None """ for b in self.get_btns().itervalues(): if b.get_rect().collidepoint(pos): b.click() break def setup(self): """ setup screens attributes :return: None """ Screen.setup(self) self.__gamefield_screen.setup() if self.get_infopanel() is not None: self.get_infopanel().set_player( self.__game_controller.get_player()) self.update() def update(self): """ update this gamescreen\n :return: """ self.__gamefield_screen.update() def render(self): """ :return: None """ self._blit(self._get_background(), (0, 0)) self._blit( self.__gamefield_screen.render(), (self.__gamescreen_margin_left, self.__gamescreen_margin_top)) self._blit(self.__infopanel.render(), self.__infopanel_dimen.pos()) for b in self.get_btns().itervalues(): self._blit(b) def get_gamefield(self): """ get this gamefield screen\n :return: this GameFieldScreen instance """ return self.__gamefield_screen
class PacketView(ViewObject): """ Represents a packet\n :author: Mikael Holmbom :version: 1.0 """ # TEXT SURFACE __text_color = resource.get_color("packet_text").rgb() __border_color = resource.get_color("packet_border").rgb() __background_color = resource.get_color("packet").rgb() __packet_padd_dimen = resource.get_dimen("packet_padd") __border_dimen = resource.get_dimen("packet_border") __separator = None __packet = None def __init__(self, packet): """ :param sender: sender adress :type sender: int :param receiver: receiver adress :type receiver: int :param checksum: checksum :type checksum: int """ pygame.font.init() self.__packet = packet img = pygame.Surface(packet.get_dimen().wh()) img.fill(self.__background_color) ViewObject.__init__(self, img, packet) self.__separator = pygame.Surface( (self.__border_dimen.size, packet.get_dimen().height)) self.__separator.fill(self.__border_color) self.__blit_adress() self.__blit_borders() def __blit_adress(self): """ blit the adresses to the current image\n used as part of __init__\n :return: None """ p = self.__packet adresses = str(bin(p.get_sender())), \ str(bin(p.get_receiver())), \ str(bin(p.get_checksum())) self.__blit_content(adresses) def __blit_content(self, content): """ blit content to this surface\n used as part of __init__ :param content: the content to blit :return: None """ font_res = resource.get_font("packet") text = pygame.font.SysFont(font_res.name, font_res.size) dimen = self.__packet.get_dimen() img = self.get_image() n_content = len(content) padd_right = self.__packet_padd_dimen.right # add shadow shadow_color = tools.shift_intensiveness(self.__background_color, -50) tools.add_shadow(img, shadow_color, 6) y = (dimen.height / 2) - (font_res.size / 2) # center text for i, data in enumerate(content): text_surf = text.render(data, 0, self.__text_color) width = text.size(data)[0] idx = i + 1 end_line = ((dimen.width / n_content) * idx) img.blit(self.__separator, (end_line, 0)) x = end_line - width - padd_right img.blit(text_surf, (x, y)) def __blit_borders(self): """ blit borders to this image surface\n used as part of __init__\n :return: None """ dimen = self.__packet.get_dimen() img = self.get_image() border_size = self.__border_dimen.size border_color = self.__border_color pygame.draw.lines( img, border_color, False, ((0, 0), (dimen.width - border_size, 0), (dimen.width - border_size, dimen.height - border_size), (0, dimen.height - border_size), (0, 0)), border_size) def __str__(self): return str(self.__packet)
class InfoScreen(ViewScreen): """ screen used for use-case info\n :author: Mikael Holmbom :version: 1.0 """ __text_color = resource.get_color("info_text").rgb() __article_background_color = resource.get_color( "info_article_background").rgb() __controls_view = None __controls_dimen = resource.get_dimen("info_article_controls") __gameinfo_view = None __gameinfo_dimen = resource.get_dimen("info_article_gameinfo") __gameinfo_header_value = resource.get_string("info_header_gameinfo") __controls_header_value = resource.get_string("info_header_controls") def __init__(self): """ """ ViewScreen.__init__(self) self._font_res = resource.get_font("info") self._font = pygame.font.SysFont(self._font_res.name, self._font_res.size) self._font_header = pygame.font.SysFont(self._font_res.name, self._font_res.size, True) self._dimen_article_padd = resource.get_dimen("info_article_padd") self.get_btns()["BACK"] = TextButton( resource.get_string("btn_back_text"), resource.get_dimen("info_btn_back")) def setup(self): """ :return: """ ViewScreen.setup(self) self._get_background().fill( resource.get_color("infoscreen_background").rgb()) # set back button position btn_back = self.get_btns()["BACK"] btn_y = self.get_height() - btn_back.get_height() - btn_back.get_dimen( ).bottom btn_back.set_pos(btn_back.get_dimen().left, btn_y) self.update() def update(self): """ :return: """ self.__controls_view = self.__create_controls_view() self.__gameinfo_view = self.__create_gameinfo_view() def render(self): """ :return: """ main_surf = self._get_main_surface() main_surf.blit(self._get_background(), (0, 0)) main_surf.blit(self.__controls_view, self.__controls_dimen.pos()) main_surf.blit(self.__gameinfo_view, self.__gameinfo_dimen.pos()) for btn in self.get_btns().itervalues(): self._blit(btn) def __create_controls_view(self): """ creates a Surface containing controls information\n :return: """ rows = [] rows.append("__GAME__") rows.append("left: '" + pygame.key.name(game_controls.key_mov_left) + "'") rows.append("up: '" + pygame.key.name(game_controls.key_mov_up) + "'") rows.append("right: '" + pygame.key.name(game_controls.key_mov_right) + "'") rows.append("down: '" + pygame.key.name(game_controls.key_mov_down) + "'") rows.append("grab/release packets: '" + pygame.key.name(game_controls.key_pickup) + "'") rows.append("enabled/disable sound effects: '" + pygame.key.name(game_controls.key_switch_sound_enabled) + "'") rows.append("") rows.append("__NAVIGATION__") rows.append("back: '" + pygame.key.name(nav_controls.key_back) + "'") rows.append("exit: '" + pygame.key.name(nav_controls.key_quit) + "'") header = self.__controls_header_value.upper() width, height = self._get_article_size(header, rows) surf = pygame.Surface((width, height)) surf.fill(self.__article_background_color) self._blit_rows(surf, header, self.__text_color, rows) return surf def __create_gameinfo_view(self): """ creates a Surface containing gameinfo\n :return: """ text_content = resource.get_string("info_gameinfo_text") rows = tools.divide_to_rows( resource.get_value("info_gameinfo_maxwidth"), text_content) header = self.__gameinfo_header_value.upper() article_dimen_wh = self._get_article_size(header, rows) surf = pygame.Surface(article_dimen_wh) surf.fill(self.__article_background_color) self._blit_rows(surf, header, self.__text_color, rows) return surf