def __init__(self, display_surf, logger): super(MainMenuScreen, self).__init__(display_surf=display_surf, logger=logger.getChild(__name__)) self._background_surf = pygame.image.load( os.path.join(graphics_path, "beyblade_logo.png")).convert_alpha() self._background_surf = pygame.transform.scale( self._background_surf, (int(WIDTH / 2), int(HEIGHT / 2))) font_size = 32 # text buttons font size self._buttons = {} self._buttons["play"] = TextButton(logger=self.logger, width=40, height=15, centerx=int(WIDTH / 2), centery=HEIGHT - int(HEIGHT / 4), text="Play", text_color=WHITE, bgd_color=BLACK, alt_text_color=BRIGHTYELLOW, font_size=font_size) self._buttons["quit"] = TextButton( logger=self.logger, width=40, height=15, centerx=int(WIDTH / 2), centery=self._buttons["play"].get_center_posy() + self._buttons["play"].get_height() * 3, text="Quit Game", text_color=WHITE, bgd_color=BLACK, alt_text_color=BRIGHTYELLOW, font_size=font_size) self.reset_played_bbs() # changes all BB status to not played return
def _check_battle_over(self): if not self._player_won: txt = "Player Lost!" self._player_bb.set_lost() else: txt = "Player Won!" self._opp_bb.set_lost() self.logger.info(txt) if self._player_bb.is_out_of_screen() or self._opp_bb.is_out_of_screen(): self._battle_over_txt_obj = Text(txt, WIDTH / 2, HEIGHT / 2, WHITE, None, WHITE, font_size=48) self._battle_over = True self._continue_button = TextButton(self.logger, 100, 50, int(WIDTH / 2), int(HEIGHT * 7.0 / 8), "Continue", WHITE, RED, BRIGHTYELLOW, 32) # self.on_exit(key=txt) return
def setO(self,i): b= self.buttons[i] button =TextButton(b.xpos,b.ypos,3,1,210,210) button.set_text("O",None,200,button.xpos + button.sizeX/ 5,button.ypos - button.sizeY /10) button.press() self.buttons[i]=button self.values[i]=2 self.xTurn=True
def on_update(self): move_spdy = 1 if self._frames_counter < FRAMES_TIMER - 1: self._frames_counter += 1 move_spdy = MAX_SPEED - self._r[ self._frames_counter] # keep slowing down elif self._next_opp_bb["rect"].top == self._player_bb_dict["rect"].top: move_spdy = 0 if self._player_bb_txt_obj is None: self.create_fade_in_text() # else: # move_spdy = MAX_SPEED - self._r[self._frames_counter] # keep slowing down for bb in self._opp_bb_dict.values(): bb["rect"].top = (bb["rect"].top + move_spdy) % HEIGHT if self._player_bb_txt_obj is not None: self._player_bb_txt_obj.on_update() self._opp_bb_txt_obj.on_update() self._player_bb_txt_fade_box.on_update() if self._oppnenet_bb_txt_fade_box.on_update(): # finished fade in transition if self._continue_button is None: self._continue_button = TextButton(self.logger, 100, 50, int(WIDTH * 4.0 / 5), int(HEIGHT * 7.0 / 8), "Continue", WHITE, RED, BRIGHTYELLOW, 32) elif self._continue_button.on_update(self._l_mouse_clicked, self._mousex, self._mousey): self.on_exit(True) return
def __init__(self, drawing_size=(300, 300), window_size=(800, 600)): self.drawing_size = drawing_size self.window_size = window_size self.window = pygame.display.set_mode(window_size) pygame.display.set_caption('GNU PAINT3R') self.drawing = Drawing(window=self.window, rect=(0, 0), size=self.drawing_size, zoom_factor=2) self.clock = pygame.time.Clock() self.running = False self.MODE_BUTTONS = [ TextButton(self.window, (self.window_size[0] - 50, 75), (75, 75), 'Draw'), TextButton(self.window, (self.window_size[0] - 50, 150), (75, 75), 'Erase'), TextButton(self.window, (self.window_size[0] - 50, 225), (75, 75), 'Fill'), TextButton(self.window, (self.window_size[0] - 50, 300), (75, 75), 'Replace'), TextButton(self.window, (self.window_size[0] - 50, 375), (75, 75), 'Clear') ] self.MODE_BUTTONS[0].selected = True self.mode = 'd' self.modes = ['d', 'e', 'f', 'r', 'c'] self.BRUSHSIZE_BUTTONS = [ TextButton(self.window, (self.window_size[0] - 50, 425), (25, 25), '1'), TextButton(self.window, (self.window_size[0] - 50, 450), (25, 25), '2'), TextButton(self.window, (self.window_size[0] - 50, 475), (25, 25), '3'), TextButton(self.window, (self.window_size[0] - 50, 500), (25, 25), '4'), TextButton(self.window, (self.window_size[0] - 50, 525), (25, 25), '5') ] self.BRUSHSIZE_BUTTONS[0].selected = True self.brushsize = 1 self.colors = ((255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 255, 255), (0, 0, 0)) self.COLOR_BUTTONS = [ ColorButton(window=self.window, center=(self.window_size[0] - 170, 75), size=(50, 50), base_color=(225, 0, 0), light_color=(255, 0, 0)), ColorButton(window=self.window, center=(self.window_size[0] - 120, 75), size=(50, 50), base_color=(0, 225, 0), light_color=(0, 255, 0)), ColorButton(window=self.window, center=(self.window_size[0] - 120, 125), size=(50, 50), base_color=(0, 0, 225), light_color=(0, 0, 255)), ColorButton(window=self.window, center=(self.window_size[0] - 170, 125), size=(50, 50), base_color=(225, 225, 0), light_color=(255, 255, 0)), ColorButton(window=self.window, center=(self.window_size[0] - 120, 175), size=(50, 50), base_color=(225, 225, 225), light_color=(255, 255, 255)), ColorButton(window=self.window, center=(self.window_size[0] - 170, 175), size=(50, 50), base_color=(10, 10, 10), light_color=(20, 20, 20)), ] self.COLOR_BUTTONS[0].selected = True self.color = self.colors[0]
def __init__(self): self.values=[] self.xTurn=True self.winner=None self.ended=False self.timer = 100 for i in range(9): self.values.append(0) self.buttons=[] for i in range(3): button=TextButton(i* 200,0,2,1,210,210) button.set_text("") self.buttons.append(button) for i in range(3): button=TextButton(i* 200,200,2,1,210,210) button.set_text("") self.buttons.append(button) for i in range(3): button=TextButton(i* 200,400,2,1,210,210) button.set_text("") self.buttons.append(button)
class CampaignScreen(Screen): def __init__(self, display_surf, logger): super(CampaignScreen, self).__init__(display_surf=display_surf, logger=logger) # load player bb player_bb_name = self.load_player_bb() if player_bb_name is None: player_bb_name = "medusa" self._player_bb_dict = get_player_bb_dict(player_bb_name) # load opponents BB list and pick the next opponent self._opp_bb_dict = self.get_bb_dict() if not any(self._opp_bb_dict): # if there are no more opponents in the dictionary, player won the game # TODO: add a game won screen or message and a leaderboard pass self._next_opp_bb = self.get_next_opponent() self._frames_counter = 0 self._r = np.logspace( 0, 0.5, FRAMES_TIMER) # create a non-linear list 0->~3.16 self._player_bb_txt_obj = None self._player_bb_txt_fade_box = None self._opp_bb_txt_obj = None self._oppnenet_bb_txt_fade_box = None self._continue_button = None def on_update(self): move_spdy = 1 if self._frames_counter < FRAMES_TIMER - 1: self._frames_counter += 1 move_spdy = MAX_SPEED - self._r[ self._frames_counter] # keep slowing down elif self._next_opp_bb["rect"].top == self._player_bb_dict["rect"].top: move_spdy = 0 if self._player_bb_txt_obj is None: self.create_fade_in_text() # else: # move_spdy = MAX_SPEED - self._r[self._frames_counter] # keep slowing down for bb in self._opp_bb_dict.values(): bb["rect"].top = (bb["rect"].top + move_spdy) % HEIGHT if self._player_bb_txt_obj is not None: self._player_bb_txt_obj.on_update() self._opp_bb_txt_obj.on_update() self._player_bb_txt_fade_box.on_update() if self._oppnenet_bb_txt_fade_box.on_update(): # finished fade in transition if self._continue_button is None: self._continue_button = TextButton(self.logger, 100, 50, int(WIDTH * 4.0 / 5), int(HEIGHT * 7.0 / 8), "Continue", WHITE, RED, BRIGHTYELLOW, 32) elif self._continue_button.on_update(self._l_mouse_clicked, self._mousex, self._mousey): self.on_exit(True) return def on_render(self): g_left = None self._display_surf.fill(BLACK) # draw player image = self._player_bb_dict["image"] left = self._player_bb_dict["rect"].left top = self._player_bb_dict["rect"].top self._display_surf.blit(image, (left, top)) # draw opponents moving column for bb in self._opp_bb_dict.values(): image = bb["image"] left = bb["rect"].left if g_left is None: g_left = left top = bb["rect"].top self._display_surf.blit(image, (left, top)) # draw top gradient draw_vertical_gradient_box(self._display_surf, BB_WIDTH, BB_HEIGHT * 3, g_left, 0, BLACK, GRADIENT_DOWN, 20) # draw bottom gradient draw_vertical_gradient_box(self._display_surf, BB_WIDTH, BB_HEIGHT * 3, g_left, HEIGHT - BB_HEIGHT * 3, BLACK, GRADIENT_UP, 20) if self._player_bb_txt_obj is not None: self._player_bb_txt_obj.on_render(self._display_surf) self._opp_bb_txt_obj.on_render(self._display_surf) self._player_bb_txt_fade_box.on_render(self._display_surf) self._oppnenet_bb_txt_fade_box.on_render(self._display_surf) if self._continue_button is not None: self._continue_button.on_render(self._display_surf) super(CampaignScreen, self).on_render() return def on_exit(self, key=False): self._running = False if hasattr(key, "type"): if key.type is pygame.QUIT: return if key: from battle_screen import BattleScreen self._next_screen = BattleScreen return def load_player_bb(self): return load(key="player_beyblade") def get_next_opponent(self): possible_opponents_list = [] for bb in self._opp_bb_dict.values(): if bb["played"] is False: possible_opponents_list.append(bb) opponent = choice(possible_opponents_list) self.logger.info("Random opponent selected: {}".format( opponent["name"])) save(save_dict={"opp_beyblade": opponent["name"]}) return opponent def get_bb_dict(self): opp_bb_list = shuffle_bb_list(self._player_bb_dict["name"]) bb_dict = {} centerx = int(WIDTH / 2.0) + int(BB_WIDTH / 2.0) for bb in opp_bb_list: centery = HEIGHT - int( BB_HEIGHT / 2.0) - BB_HEIGHT * opp_bb_list.index(bb) bb_played = load(bb + "_played") if bb_played is None: bb_played = False image = bb + ".png" image_surf = pygame.image.load(os.path.join( graphics_path, image)).convert_alpha() image_surf = pygame.transform.smoothscale(image_surf, (BB_WIDTH, BB_HEIGHT)) image_surf_rect = image_surf.get_rect(center=(centerx, centery)) if bb_played: image_surf.set_alpha(128) bb_dict[bb] = { "name": bb, "image": image_surf, "rect": image_surf_rect, "played": bb_played } return bb_dict def create_fade_in_text(self): name = self._player_bb_dict["name"].upper().replace("_", " ") rect = self._player_bb_dict["rect"] self._player_bb_txt_obj = Text(name, int(WIDTH / 4.0), rect.centery, WHITE, BLACK, WHITE) txt_rect = self._player_bb_txt_obj.get_rect() self._player_bb_txt_fade_box = FadeInBox(txt_rect.width, txt_rect.height, txt_rect.top, txt_rect.left, BLACK, 255) name = self._next_opp_bb["name"].upper().replace("_", " ") rect = self._next_opp_bb["rect"] self._opp_bb_txt_obj = Text(name, int(WIDTH * 3.0 / 4), rect.centery, WHITE, BLACK, WHITE) txt_rect = self._opp_bb_txt_obj.get_rect() self._oppnenet_bb_txt_fade_box = FadeInBox(txt_rect.width, txt_rect.height, txt_rect.top, txt_rect.left, BLACK, 255) return
class BattleScreen(Screen): def __init__(self, display_surf, logger): super(BattleScreen, self).__init__(display_surf=display_surf, logger=logger.getChild(__name__)) self._background_surf = self.load_arena() player_name = load(key="player_beyblade") opp_name = load(key="opp_beyblade") self._player_bb = Beyblade(logger=self.logger, name=player_name, player=True) # self._opp_bb = AIBeyblade(self.logger, name=opp_name) self._opp_bb = Beyblade(self.logger, name=opp_name, player=False) self._player_title = None self._player_hp_txt_bar = None self._player_spd_txt_bar = None self._opp_title = None self._opp_hp_txt_bar = None self._opp_spd_txt_bar = None self.create_bars() # create HP & SPD bars self._collided = False self._battle_over = False self._battle_over_txt_obj = None self._continue_button = None self._player_won = False return def create_bars(self): player_hp_txt_cntrx = int(WIDTH / 20.0) player_hp_txt_cntry = int(HEIGHT / 20.0) player_hp = self._player_bb.get_hp() player_spd = self._player_bb.get_spd() player_bar_left = int(WIDTH * 1.0 / 9) opp_hp_txt_cntrx = int(WIDTH * 19.0 / 20) opp_hp = self._opp_bb.get_hp() opp_spd = self._opp_bb.get_spd() opp_bar_left = int(WIDTH * 5.0 / 9) bars_width = int(WIDTH * 3.0 / 9) self._player_title = Text("Player", center_posx=int(WIDTH/10.0), center_posy=player_hp_txt_cntry, text_color=WHITE, bgd_color=None, alt_text_color=WHITE, font_size=48) self._opp_title = Text("Computer", center_posx=int(WIDTH * 17.0 / 20), center_posy=player_hp_txt_cntry, text_color=WHITE, bgd_color=None, alt_text_color=WHITE, font_size=48) height = self._player_title.get_height() self._player_hp_txt_bar = TextBar(text="HP", text_centerx=player_hp_txt_cntrx, text_centery=player_hp_txt_cntry + height, text_color=WHITE, bgd_text_color=BGD_COLOR, alt_text_color=WHITE, font_size=32, value=player_hp, max_value=player_hp, width=bars_width, bar_left=player_bar_left, bar_color=BRIGHTYELLOW, bar_bgd_color=RED) self._opp_hp_txt_bar = TextBar(text="HP", text_centerx=opp_hp_txt_cntrx, text_centery=player_hp_txt_cntry + height, text_color=WHITE, bgd_text_color=BGD_COLOR, alt_text_color=WHITE, font_size=32, value=opp_hp, max_value=opp_hp, width=bars_width, bar_left=opp_bar_left, bar_color=BRIGHTYELLOW, bar_bgd_color=RED) height = self._player_hp_txt_bar.get_height() self._player_spd_txt_bar = TextBar(text="SPD", text_centerx=player_hp_txt_cntrx, text_centery=player_hp_txt_cntry + 3*height, text_color=WHITE, bgd_text_color=BGD_COLOR, alt_text_color=WHITE, font_size=32, value=player_spd, max_value=player_spd*2, width=bars_width, bar_left=player_bar_left, # width=int(WIDTH * 3.0 / 9), bar_left=int(WIDTH * 1.0 / 9), bar_color=WHITE, bar_bgd_color=BRIGHTBLUE) self._opp_spd_txt_bar = TextBar(text="SPD", text_centerx=opp_hp_txt_cntrx, text_centery=player_hp_txt_cntry + 3*height, text_color=WHITE, bgd_text_color=BGD_COLOR, alt_text_color=WHITE, font_size=32, value=opp_spd, max_value=opp_spd*2, width=bars_width, bar_left=opp_bar_left, bar_color=WHITE, bar_bgd_color=BRIGHTBLUE) return def load_arena(self): background_surf = pygame.image.load(os.path.join(graphics_path, "rings_arena.png")).convert_alpha() min_dim = min(WIDTH, HEIGHT) background_surf = pygame.transform.scale(background_surf, (min_dim, min_dim)) return background_surf def on_update(self): if self._battle_over: if self._continue_button.on_update(self._l_mouse_clicked, self._mousex, self._mousey): if self._player_won: bb = self._opp_bb.get_name() save({bb + "_played": True}) self.on_exit(key=self._player_won) else: self.control_ai_bb() if self._l_mouse_clicked: opp_rect = self._opp_bb.get_rect() self._player_bb.attack(self._opp_bb.get_radius()) self._l_mouse_clicked = False # return to default value till next click elif self._r_mouse_clicked: self._player_bb.evade() self._r_mouse_clicked = False # return to default value till next click self._player_bb.on_update() self._player_hp_txt_bar.on_update(self._player_bb.get_hp()) self._player_spd_txt_bar.on_update(self._player_bb.get_spd()) self._opp_bb.on_update() self._opp_hp_txt_bar.on_update(self._opp_bb.get_hp()) self._opp_spd_txt_bar.on_update(self._opp_bb.get_spd()) if self._check_collision(): if not self._collided: self._collided = True self.logger.info("Collision!") self._player_bb.collided(self._opp_bb) self._opp_bb.collided(self._player_bb) self._player_bb.unset_attacking() self._opp_bb.unset_attacking() else: self._collided = False if self._player_bb.get_hp() <= 0: self._player_won = False self._check_battle_over() elif self._opp_bb.get_hp() <= 0: self._player_won = True self._check_battle_over() # TODO: remove the on_exit from conditions above, replace with if player.rect outside screen # TODO: need to update which BB lost for opponent selection screen return def _check_battle_over(self): if not self._player_won: txt = "Player Lost!" self._player_bb.set_lost() else: txt = "Player Won!" self._opp_bb.set_lost() self.logger.info(txt) if self._player_bb.is_out_of_screen() or self._opp_bb.is_out_of_screen(): self._battle_over_txt_obj = Text(txt, WIDTH / 2, HEIGHT / 2, WHITE, None, WHITE, font_size=48) self._battle_over = True self._continue_button = TextButton(self.logger, 100, 50, int(WIDTH / 2), int(HEIGHT * 7.0 / 8), "Continue", WHITE, RED, BRIGHTYELLOW, 32) # self.on_exit(key=txt) return def on_render(self): self._display_surf.fill(BGD_COLOR) bgd_left = int(int(WIDTH / 2.0) - min(WIDTH, HEIGHT) / 2.0) self._display_surf.blit(self._background_surf, (bgd_left, 0)) self._player_bb.on_render(self._display_surf) self._player_title.on_render(self._display_surf) self._player_hp_txt_bar.on_render(self._display_surf) self._player_spd_txt_bar.on_render(self._display_surf) self._opp_bb.on_render(self._display_surf) self._opp_title.on_render(self._display_surf) self._opp_hp_txt_bar.on_render(self._display_surf) self._opp_spd_txt_bar.on_render(self._display_surf) if self._battle_over: self._battle_over_txt_obj.on_render(self._display_surf) self._continue_button.on_render(self._display_surf) super(BattleScreen, self).on_render() return def on_exit(self, key=None): self._running = False if hasattr(key, "type"): if key.type is pygame.QUIT: return if key: from campaign_screen import CampaignScreen self._next_screen = CampaignScreen else: from main_menu_screen import MainMenuScreen self._next_screen = MainMenuScreen def _check_collision(self): player_bb_rect = self._player_bb.get_rect() player_bb_centerx = player_bb_rect.centerx player_bb_centery = player_bb_rect.centery player_bb_radius = int(player_bb_rect.height / 2.7) opp_bb_rect = self._opp_bb.get_rect() opp_bb_centerx = opp_bb_rect.centerx opp_bb_centery = opp_bb_rect.centery opp_bb_radius = int(opp_bb_rect.height / 2.7) radius_sum = player_bb_radius + opp_bb_radius delta_x = abs(player_bb_centerx - opp_bb_centerx) delta_y = abs(player_bb_centery - opp_bb_centery) distance = math.sqrt(delta_x ** 2 + delta_y ** 2) if distance <= radius_sum: return True else: return False def control_ai_bb(self): if self._opp_bb.is_attacking() or self._opp_bb.is_evading(): return rnd_num = random.randint(0, 3) if rnd_num == 1: # 30% chance self.logger.info("Opponent BB Evading") self._opp_bb.evade() elif rnd_num == 2: self.logger.info("Attacking") self._opp_bb.attack(opp_radius=self._player_bb.get_radius()) else: self.logger.info("Moving") self._opp_bb.unset_evading() self._opp_bb.unset_attacking()