def clicked(self, button_name): if len(self.model.show_stats ) > 0: # we are already showing stats, unshow self.model.show_stats = [] else: num_games = str(len(self.model.times)) best_time = "No Time Data Yet" avg_time = "No Time Data Yet" if len(self.model.times) > 0: best_time = format_secs(min(self.model.times)) avg_time = format_secs( sum(self.model.times) / len(self.model.times)) message_box = planes.Plane( 'message_box', pygame.Rect(left_margin, top_margin, 13 * WINDOW_WIDTH / 16, (WINDOW_HEIGHT - 300))) message_box.image.fill((0, 0, 0)) win_stats = "Game Stats \n" + "Number of Games: " + num_games + "\nBest time: " + best_time + "\nAverage time: " + avg_time message_texts = [] lines = win_stats.split("\n") box_width = 13 * WINDOW_WIDTH / 16 for line in lines: message_texts.append( ScreenText( line, line, pygame.Rect(left_margin, top_margin + 60 * (lines.index(line) + 1), box_width, 45), FONT_BIG)) #message_text.background_color = (255,0,0) #fixthis not transparent self.model.show_stats.append(message_box) self.model.show_stats += message_texts
def selected(self, s): if self._selected: if self._selected.plane: self._selected.plane.remove("outlined_selection") #self._selected.plane.render() if not isinstance(self._selected, Cities.City) and self._selected.plane: self.game_plane.sub( self._selected.plane, insert_before=self.game_plane.subplanes_list[0]) self._selected = s if isinstance(s, BaseObjects.Unit): self.show_city_status(s) img = s.plane.image.copy() pygame.draw.rect(img, red, [0, 0, self.image_size - 1, self.image_size - 1], 3) outlined = planes.Plane( "outlined_selection", Rect([0, 0, self.image_size - 1, self.image_size - 1])) outlined.image.blit(img, (0, 0)) s.plane.sub(outlined) self.game_plane.sub(s.plane) else: pass
def update(self): if self.mode == MODE_HOME: self.actors = [self.title] + self.homebuttons[:] if self.show_stats != None: self.actors += self.show_stats clicked_button = None #add click box for button in self.homebuttons: if button.clickbox: clicked_button = button if clicked_button != None: clicked_box = planes.Plane( "box" + clicked_button.name, pygame.Rect(clicked_button.rect.x - 5, clicked_button.rect.y - 5, clicked_button.rect.width + 10, clicked_button.rect.height + 10), False, False) self.actors.insert(1, clicked_box) else: self.game.numSets() self.game.update() self.actors = self.game.actors[:]
def __init__(self): self.background = (20, 20, 20) self.mode = MODE_HOME self.game = None self.actors = [] try: times_file = open("times_file.txt", "r") except: times_file = open("times_file.txt", "w+") self.times = [int(score.strip()) for score in times_file.readlines()] times_file.close() self.show_stats = [] # a list of things for stats screen ######################## # HOME SCREEN ELEMENTS # ######################## self.title = planes.Plane( "title", pygame.Rect(left_margin, top_margin, 13 * WINDOW_WIDTH / 16, (WINDOW_HEIGHT - 300))) self.start_button = StartButton("start_button", pygame.Rect(200, 500, 100, 100), StartButton.clicked, self) self.stats_button = StatsButton("stats_button", pygame.Rect(500, 500, 100, 100), StatsButton.clicked, self) self.homebuttons = [self.start_button, self.stats_button]
def __init__(self): self.background = (20, 20, 20) self.mode = MODE_HOME self.game_select = NOTIME self.game = None self.actors = [] try: times_file = open("times_file.txt", "r") except: times_file = open("times_file.txt", "w+") self.times = [int(score.strip()) for score in times_file.readlines()] times_file.close() self.show_stats = [] # a list of things for stats screen ######################## # HOME SCREEN ELEMENTS # ######################## self.title = planes.Plane( "title", pygame.Rect(left_margin, top_margin, 13 * WINDOW_WIDTH / 16, (WINDOW_HEIGHT - 300))) self.start_button = StartButton( "start_button", pygame.Rect( 3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2 + 100, 50, 100, 100), StartButton.clicked, self) self.notime_button = NoTimeButton( "notime_button", pygame.Rect(WINDOW_WIDTH / 5 - 50, WINDOW_HEIGHT - 200, 100, 100), NoTimeButton.clicked, self) self.easy_button = EasyButton( "easy_button", pygame.Rect(2 * WINDOW_WIDTH / 5 - 50, WINDOW_HEIGHT - 200, 100, 100), EasyButton.clicked, self) self.med_button = MedButton( "med_button", pygame.Rect(3 * WINDOW_WIDTH / 5 - 50, WINDOW_HEIGHT - 200, 100, 100), MedButton.clicked, self) self.hard_button = HardButton( "hard_button", pygame.Rect(4 * WINDOW_WIDTH / 5 - 50, WINDOW_HEIGHT - 200, 100, 100), HardButton.clicked, self) self.stats_button = StatsButton( "stats_button", pygame.Rect( 3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2 + 100, 200, 100, 100), StatsButton.clicked, self) self.homebuttons = [ self.start_button, self.notime_button, self.easy_button, self.med_button, self.hard_button, self.stats_button ]
def __init__(self, controller, image_size): self.controller = controller self.image_size = image_size pygame.init() self.screenSize = (controller.size[0] * image_size + image_size * 2 + 12, image_size * 17 + image_size * 2 + 30) self.w32windowClass = "pygame" #The win32 window class for this object self.screen = planes.Display(self.screenSize) self.windowCaption = "mPyre" pygame.display.set_caption(self.windowCaption) self.white = pygame.Color("white") self.screen.image.fill(self.white) pygame.display.flip() # #The game screen self.game_plane = planes.Plane( "game screen", Rect(0, 0, (controller.size[0] + 2) * image_size, (controller.size[1] + 2) * image_size)) self.game_background = draw_map(self.game_plane, self.controller.G.map, self.image_size) self.game_plane.image.blit(self.game_background, (0, 0)) scroller = planes.gui.ScrollingPlane( "scroller", Rect(0, 0, self.screenSize[0] - 12, self.screenSize[1] - 30), self.game_plane) self.screen.sub(scroller) #The main status display - turn and player name self.turn_status = planes.gui.Button( "Turn: 1", Rect(0, self.screenSize[1] - 30, 150, 30), self.next_turn) #callback placeholder self.screen.sub(self.turn_status) self.player_status = planes.gui.Button("Player: player_1", Rect(150, self.screenSize[1] - 30, 125, 30), None) #callback placeholder self.screen.sub(self.player_status) self.status_window = None self._selected = None self.city_bubble = None self.advance_turn = False pygame.display.flip()
def __init__(self): self.background = (20, 20, 20) self.game = Game(self) self.actors = [] ######################## # HOME SCREEN ELEMENTS # ######################## self.title = planes.Plane( "title", pygame.Rect(LEFT_MARGIN, TOP_MARGIN, 13 * WINDOW_WIDTH / 16, (WINDOW_HEIGHT - 300)))
def update(self): """update - update the game window""" game = self.controller.G self.game_plane.image.blit(self.game_background, (0, 0)) #reset background drawing for c in game.cities: if not c.plane: c_rect = Rect((c.coords[0] * self.image_size, c.coords[1] * self.image_size), [self.image_size] * 2) c.set_image([self.image_size] * 2, c.owner.color) c.plane = planes.Plane("City of {}".format(c.name), c_rect) c.plane.image.blit(c.image, (0, 0)) self.game_plane.sub(c.plane) for u in game.units: if not u.plane: u_rect = Rect((u.coords[0] * self.image_size, u.coords[1] * self.image_size), [self.image_size] * 2) u.set_image([self.image_size] * 2, u.owner.color) u.plane = planes.Plane("Unit {}".format(u.name), u_rect) u.plane.image.blit(u.image, (0, 0)) self.game_plane.sub( u.plane, insert_before=self.game_plane.subplanes_list[0]) # d.plane.draggable=True # self.game_plane.sub(d.plane) if not self.selected and self.status_window: self.status_window.destroy() self.status_window = None self.turn_status.text = "Turn: {0:,}".format(int(game.turn)) self.player_status.current_color = colors[game.current_player.color] self.player_status.text = "{}".format(game.current_player.name) self.screen.update() self.screen.render() pygame.display.flip() return True
def __init__(self, name, rect, content_plane, draggable=False, grab=False, clicked_callback=None, dropped_upon_callback=None): """Initalise. rect states the dimensions without the scroll bar. """ rect.width = rect.width + 12 # Call base class # planes.Plane.__init__(self, name, rect, draggable, grab, clicked_callback, dropped_upon_callback) self.image.fill(BACKGROUND_COLOR) self.sub( planes.Plane( "content", pygame.Rect((0, 0), (self.rect.width - 12, self.rect.height)))) content_plane.rect.topleft = (0, 0) self.content.sub(content_plane) scrollbar_container = planes.Plane( "scrollbar_container", pygame.Rect((self.rect.width - 12, 0), (12, self.rect.height))) scrollbar_container.image.fill(BACKGROUND_COLOR) draw_border(scrollbar_container, (0, 0, 0)) def scrollbar_container_clicked(plane): """Clicked callback which repositions the content Plane and scrollbar according to the y-position of the mouse. """ x, y = pygame.mouse.get_pos() new_y = y - self.rect.top # Align scrollbar at bottom # if new_y > self.rect.height - self.scrollbar_container.scrollbar.rect.height - 2: new_y = self.rect.height - self.scrollbar_container.scrollbar.rect.height - 2 self.scrollbar_container.scrollbar.rect.top = new_y content_plane = self.content.subplanes[ self.content.subplanes_list[0]] content_plane.rect.top = int(0 - new_y / self.rect.height * content_plane.rect.height) # Invalidate last_image_id to trigger a redraw # TODO: changing content_plane.rect.top above should invalidate last_rect, why is this necessary? # content_plane.last_image_id = None return scrollbar_container.left_click_callback = scrollbar_container_clicked self.sub(scrollbar_container) # Scrollbar height reflects the proportions # self.scrollbar_container.sub( planes.Plane( "scrollbar", pygame.Rect((2, 2), (8, int(self.rect.height / content_plane.rect.height * self.rect.height))))) # Half-bright color taken from Button.clicked() # self.scrollbar_container.scrollbar.image.fill( list(map(lambda i: int(i * 0.5), BACKGROUND_COLOR))) return
def update(self): # if game not in play, display messages, not cards if not self.check_in_play(): self.actors = [] self.actors += self.gamelabels + self.gamebuttons if HINTS: self.hints_left_label.update_text("Hints Remaining: " + str(self.hints_left)) self.left_in_deck_label.update_text("Deck: " + str( len(self.deck) - (len(self.in_play_cards) + len(self.out_of_play_cards)))) message_box = planes.Plane( 'message_box', pygame.Rect( LEFT_MARGIN, TOP_MARGIN, 3 * CARD_WIDTH + 2 * SPACE_HORIZ, 4 * CARD_HEIGHT + 3 * ((WINDOW_HEIGHT - 4 * CARD_HEIGHT - 2 * TOP_MARGIN) / 3))) message_box.image.fill((0, 0, 0)) message_texts = [] self.actors.append(message_box) self.actors += message_texts # game in play else: self.actors = [] #check which cards are clicked self.clicked_cards = [] for card in self.in_play_cards: self.actors.append(card) if card.been_clicked: self.clicked_cards.append(card) card.update() #add click boxes for card in self.clicked_cards: clicked_box = planes.Plane( "box" + card.name, pygame.Rect(card.rect.x - 5, card.rect.y - 5, card.rect.width + 10, card.rect.height + 10), False, False) clicked_box.image = pygame.image.load("img/clickbox.png") self.actors.insert(0, clicked_box) #check for sets if len(self.clicked_cards) == 3: is_set = check_set(self.clicked_cards[0], self.clicked_cards[1], self.clicked_cards[2]) if is_set: self.sets_found += 1 self.sets_found_label.update_text("Sets: " + str(self.sets_found)) #remove cards and add new ones for card in self.clicked_cards: self.out_of_play_cards.append(card) index = self.in_play_cards.index(card) self.in_play_cards.remove(card) if len(self.in_play_cards) < 12: self.add_new_cards(1, index) else: self.sets_wrong += 1 for card in self.clicked_cards: card.been_clicked = False self.actors += self.gamelabels + self.gamebuttons if HINTS: self.hints_left_label.update_text("Hints Remaining: " + str(self.hints_left)) self.left_in_deck_label.update_text("Deck: " + str( len(self.deck) - (len(self.in_play_cards) + len(self.out_of_play_cards))))
def __init__(self, model): ######################## # GAME SCREEN ELEMENTS # ######################## self.deck = [] self.model = model # make 81 unique cards, add to deck for color in COLORS: for shape in SHAPES: for number in NUMBERS: for shade in SHADES: card_to_add = Card(color + shape + shade + str(number), color, shape, number, shade) self.deck.append(card_to_add) card_to_add.image = pygame.image.load( "img/" + card_to_add.name + ".png") self.actors = [] self.in_play_cards = [] self.clicked_cards = [] self.out_of_play_cards = [] self.sets_found = 0 self.sets_wrong = 0 # should we take off points for these? self.hints_left = NUM_HINTS #### Elements of a game #### self.sets_found_label = ScreenText( "sets_found_label", "Sets: " + str(self.sets_found), pygame.Rect(3 * WINDOW_WIDTH / 4, 290, WINDOW_WIDTH / 4, 50), FONT_BIG) self.left_in_deck_label = ScreenText( "left_in_deck_label", "Deck: " + str( len(self.deck) - (len(self.in_play_cards) + len(self.out_of_play_cards))), pygame.Rect(3 * WINDOW_WIDTH / 4, 505, WINDOW_WIDTH / 4, 25), FONT_SMALL) if not AUTO_ADD3: self.add3_button = AddThreeCardsButton( "add_three_cards_button", pygame.Rect( 3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2, 360, 100, 100), AddThreeCardsButton.clicked, self) if HINTS: self.hint_button = HintButton( "hint_button", pygame.Rect( 3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2 + 100, 360, 100, 100), HintButton.clicked, self) self.hints_left_label = ScreenText( "hints_left_label", "Hints Remaining: " + str(self.hints_left), pygame.Rect(3 * WINDOW_WIDTH / 4, 475, WINDOW_WIDTH / 4, 25), FONT_SMALL) self.logo = planes.Plane( "setlogo", pygame.Rect(3 * WINDOW_WIDTH / 4, 50, 240, 162), False, False) self.logo.image = pygame.image.load("img/set.jpg") #### CATEGORIES #### self.gamebuttons = [self.logo] self.gamelabels = [self.sets_found_label, self.left_in_deck_label] if HINTS: self.gamebuttons.append(self.hint_button) self.gamelabels.append(self.hints_left_label) if not AUTO_ADD3: self.gamebuttons.append(self.add3_button) # start the game self.add_new_cards(12)
window.image.fill((127, 127, 127)) pygame.display.set_caption("planes Interactive Live Test") red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) yellow = (255, 255, 0) white = (255, 255, 255) def click(*args): print("Click! args: {0}".format(args)) plane = planes.Plane("plane", pygame.Rect((200, 50), (100, 100)), draggable=True) plane.image.fill(yellow) button = planes.gui.Button("Button", pygame.Rect((50, 50), (100, 50)), click) container = planes.gui.Container("container", padding=10) container.rect.topleft = (250, 50) textbox = planes.gui.TextBox("textbox", pygame.Rect((10, 10), (200, 30))) window.key_sensitive(textbox) clock = pygame.time.Clock() fps = clock.get_fps
def update(self): # if game not in play, display messages, not cards if not self.check_in_play(): self.actors = [] self.actors += self.gamelabels + self.gamebuttons self.hints_left_label.update_text("Hints Remaining: " + str(self.hints_left)) self.left_in_deck_label.update_text("Deck: " + str( len(self.deck) - (len(self.in_play_cards) + len(self.out_of_play_cards)))) message_box = planes.Plane( 'message_box', pygame.Rect( left_margin, top_margin, 3 * CARD_WIDTH + 2 * space_horiz, 4 * CARD_HEIGHT + 3 * ((WINDOW_HEIGHT - 4 * CARD_HEIGHT - 2 * top_margin) / 3))) message_box.image.fill((0, 0, 0)) message_texts = [] # if game won or lost, note time game ended if self.check_if_won() or self.check_if_lost(): if self.end_time == 0: self.end_time = pygame.time.get_ticks() total_time = self.end_time - self.start_time - self.pause_time if self.check_if_won() and not self.added_time: self.model.add_time( (total_time + (self.sets_wrong * TIME_DEDUC)) / 1000) self.added_time = True best_time = "" if len(self.model.times) == 0: best_time = format_secs(total_time / 1000) else: best_time = format_secs(min(self.model.times)) win_stats = "Game Complete! \n" + \ "Total time: " + format_secs ((self.end_time - self.start_time - self.pause_time)/ 1000) + "\n" +\ "Best time: " + best_time win_stats_with_loss = "Game Complete! \n" + \ "Total time: " + format_secs (total_time/ 1000) + "\n" +\ "Incorrect Sets: " + str(self.sets_wrong) + "\n" +\ "Adjusted Time: " + format_secs ((total_time+(self.sets_wrong*TIME_DEDUC))/ 1000) + "\n" +\ "Best time: " + best_time lose_stats = "Game Over!" stats = win_stats_with_loss if self.check_if_lost(): stats = lose_stats lines = stats.split("\n") box_width = 3 * CARD_WIDTH + 2 * space_horiz for line in lines: message_texts.append( ScreenText( line, line, pygame.Rect( left_margin, top_margin + 50 * (lines.index(line) + 1), box_width, 45), FONT_BIG)) elif self.paused_time_at != 0: #game is paused message_texts.append( ScreenText( "message_text", "Game Paused", pygame.Rect( left_margin, top_margin, 3 * CARD_WIDTH + 2 * space_horiz, 4 * CARD_HEIGHT + 3 * ((WINDOW_HEIGHT - 4 * CARD_HEIGHT - 2 * top_margin) / 3)), FONT_BIG)) self.actors.append(message_box) self.actors += message_texts self.actors += self.pausebuttons # game in play else: self.time_box.update() self.actors = [self.time_box] #check which cards are clicked self.clicked_cards = [] for card in self.in_play_cards: self.actors.append(card) if card.been_clicked: self.clicked_cards.append(card) card.update() #add click boxes for card in self.clicked_cards: clicked_box = planes.Plane( "box" + card.name, pygame.Rect(card.rect.x - 5, card.rect.y - 5, card.rect.width + 10, card.rect.height + 10), False, False) clicked_box.image = pygame.image.load("img/clickbox.png") self.actors.insert(1, clicked_box) #check for sets if len(self.clicked_cards) == 3: is_set = check_set(self.clicked_cards[0], self.clicked_cards[1], self.clicked_cards[2]) if is_set: self.sets_found += 1 self.sets_found_label.update_text("Sets: " + str(self.sets_found)) # reset the time box self.time_box.rect.y = -WINDOW_HEIGHT #remove cards and add new ones for card in self.clicked_cards: self.out_of_play_cards.append(card) index = self.in_play_cards.index(card) self.in_play_cards.remove(card) if len(self.in_play_cards) < 12: self.add_new_cards(1, index) else: self.sets_wrong += 1 for card in self.clicked_cards: card.been_clicked = False self.actors += self.gamelabels + self.gamebuttons self.time_label.update_text("Time: " + format_secs( (pygame.time.get_ticks() - self.start_time - self.pause_time) / 1000)) self.hints_left_label.update_text("Hints Remaining: " + str(self.hints_left)) self.left_in_deck_label.update_text("Deck: " + str( len(self.deck) - (len(self.in_play_cards) + len(self.out_of_play_cards))))
def __init__(self, game_select, model): ######################## # GAME SCREEN ELEMENTS # ######################## self.deck = [] self.model = model self.game_select = game_select self.pause_time = 0 self.paused_time_at = 0 self.start_time = pygame.time.get_ticks() self.end_time = 0 # time game ended at #make 81 unique cards, add to deck for color in colors: for shape in shapes: for number in numbers: for shade in shades: card_to_add = Card(color + shape + shade + str(number), color, shape, number, shade) self.deck.append(card_to_add) card_to_add.image = pygame.image.load( "img/" + card_to_add.name + ".png") self.actors = [] self.in_play_cards = [] self.clicked_cards = [] self.out_of_play_cards = [] self.sets_found = 0 self.sets_wrong = 0 # should we take off points for these? self.hints_left = NUM_HINTS # tells if we have already added the game time to the times [] # prevents from adding the time on every update loop self.added_time = False #### Elements of a game #### self.sets_found_label = ScreenText( "sets_found_label", "Sets: " + str(self.sets_found), pygame.Rect(3 * WINDOW_WIDTH / 4, 290, WINDOW_WIDTH / 4, 50), FONT_BIG) self.time_label = ScreenText( "time_label", "Time: " + format_secs(self.start_time / 1000), pygame.Rect(3 * WINDOW_WIDTH / 4, 220, WINDOW_WIDTH / 4, 100), FONT_BIG) self.left_in_deck_label = ScreenText( "left_in_deck_label", "Deck: " + str( len(self.deck) - (len(self.in_play_cards) + len(self.out_of_play_cards))), pygame.Rect(3 * WINDOW_WIDTH / 4, 505, WINDOW_WIDTH / 4, 25), FONT_SMALL) self.add3_button = AddThreeCardsButton( "add_three_cards_button", pygame.Rect(3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2, 360, 100, 100), AddThreeCardsButton.clicked, self) self.hint_button = HintButton( "hint_button", pygame.Rect( 3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2 + 100, 360, 100, 100), HintButton.clicked, self) self.pause_button = PauseButton( "pause_button", pygame.Rect( 3 * WINDOW_WIDTH / 4 + (WINDOW_WIDTH / 4 - 200) / 2 + 50, WINDOW_HEIGHT - 120, 100, 100), PauseButton.clicked, self) self.hints_left_label = ScreenText( "hints_left_label", "Hints Remaining: " + str(self.hints_left), pygame.Rect(3 * WINDOW_WIDTH / 4, 475, WINDOW_WIDTH / 4, 25), FONT_SMALL) self.logo = planes.Plane( "setlogo", pygame.Rect(3 * WINDOW_WIDTH / 4, 50, 240, 162), False, False) self.logo.image = pygame.image.load("img/set.jpg") self.time_box = TimeBox( "time_box", pygame.Rect(0, -WINDOW_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT), game_select) #### PAUSE SCREEN BUTTONS #### message_width = 3 * CARD_WIDTH + 2 * space_horiz # width of playing field self.play_button = PlayButton( "play_button", pygame.Rect(2 * message_width / 5 - 50, WINDOW_HEIGHT - 300, 100, 100), PlayButton.clicked, self) self.restart_button = RestartButton( "restart_button", pygame.Rect(3 * message_width / 5 - 50, WINDOW_HEIGHT - 300, 100, 100), RestartButton.clicked, self) self.back_button = BackButton( "back_button", pygame.Rect(4 * message_width / 5 - 50, WINDOW_HEIGHT - 300, 100, 100), BackButton.clicked, self) #### CATEGORIES #### self.gamebuttons = [ self.add3_button, self.hint_button, self.pause_button, self.logo ] self.gamelabels = [ self.sets_found_label, self.time_label, self.hints_left_label, self.left_in_deck_label ] self.pausebuttons = [ self.play_button, self.restart_button, self.back_button ] # start the game self.add_new_cards(12)