def popMatches(self): ''' 'pops' the matches and then displays a good job message ''' bubble = self.shoot_bubble bubble.erase(self.gameDisplay) bubble.draw(self.gameDisplay) matches = self.current_matches if matches != [bubble]: # erase the matches # display good job message good_job = Popup("You popped bubbles! Good job!.", int(DISPLAY_X * 0.25), int(DISPLAY_Y * 0.30), self.gameDisplay) good_job.create() pygame.display.update() for b in matches: b.erase(self.gameDisplay) index = self.board_bubbles.index(b) self.board_bubbles[index] = 0 #b.colour = WHITE self.num_bubbles_poopped += 1 bubble.erase(self.gameDisplay) # send good job message print("You popped bubbles!") self.success_popped = True time.sleep(0.3) good_job.erase() self.drawScore() self.shooting = False self.checkToRemoveColours() return
def addToBoard(self): global POPUP_COUNTER, POPUP """ funtion to add stuff around the main board space it will ultimately be used for things like score and varioius similar metrics """ meme_font = pygame.font.SysFont('Comic Sans MS', 25) # write some memes on the sides # # left_meme = meme_font.render('It ya boi', False, BLACK) right_meme = meme_font.render('To play click', False, BLACK) right_meme_pt2 = meme_font.render('where you want', False, BLACK) right_meme_pt3 = meme_font.render('the bubble to go', False, BLACK) # gameDisplay.blit(left_meme, (int(DISPLAY_X * 0.05), int(DISPLAY_Y * 0.1))) self.gameDisplay.blit(right_meme, (int(DISPLAY_X * 0.76), int(DISPLAY_Y * 0.02))) self.gameDisplay.blit(right_meme_pt2, (int(DISPLAY_X * 0.76), int(DISPLAY_Y * 0.04))) self.gameDisplay.blit(right_meme_pt3, (int(DISPLAY_X * 0.76), int(DISPLAY_Y * 0.06))) if self.won: # create the success popup success_poppup = Popup('You Won! Good Job!', int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.5), self.gameDisplay, BLUE) self.current_popups.append(success_poppup) success_poppup.create() # store the popup for deletion later # POPUP = ['Words Matched! Good Job!', int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.5)] # create second success popu TODO: this is a meme, remove it success_poppup2 = Popup('Winner, winner, chicken dinner', int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.4), self.gameDisplay, BLUE) self.current_popups.append(success_poppup2) success_poppup.create() elif self.success_popped: # create the success popup popped_bubble = Popup('Words Matched! Good Job!', int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.5), self.gameDisplay, BLUE) self.current_popups.append(popped_bubble) popped_bubble.create() # start the counter POPUP_COUNTER += 1 # store the popup for deletion later #POPUP = ['Words Matched! Good Job!', int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.5)] self.success_popped = False elif self.game_over: # create the game over popup lost_popup = Popup('Game over man, game over!', int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.4), self.gameDisplay, RED) self.current_popups.append(lost_popup) lost_popup.create() # start the counter POPUP_COUNTER += 1
def game_loop(): global running, gameDisplay, POPUP, POPUP_COUNTER pygame.init() pygame.font.init() # for writing text to pygame gameDisplay = pygame.display.set_mode((DISPLAY_X, DISPLAY_Y)) pygame.display.set_caption('Version 1.0') gameDisplay.fill(BACKGROUND_COLOUR) clock = pygame.time.Clock() running = True won = False board = Board(gameDisplay) board.createWordList() board.drawBoard() board.drawAllBubbles() board.addToBoard() while running: # if (board.future_bubbles == [] or len(board.board_bubbles) == 22) and not board.shooting: # TODO: check if this is actually right # print("You did not win. Try again!") # running = False if board.shooting and board.shoot_pos != []: board.shoot_bubble.erase(gameDisplay) #pygame.display.update() board.shoot_bubble.pos = board.shoot_pos[0] board.shoot_bubble.drawAsGrey(gameDisplay) board.shoot_pos.pop(0) elif board.shooting and board.shoot_pos == [] and board.future_bubbles != []: # load in new bubble try: board.popMatches() board.shoot_bubble = Bubble(SHOOT_POSITION[0], SHOOT_POSITION[1], \ board.future_bubbles[0][0], board.future_bubbles[0][1]) board.shooting = False board.drawAllBubbles() except IndexError: running = False if board.board_bubbles.count(0) == len(board.board_bubbles): board.won = True print("You won! Good job! :)") won = True running = False elif board.future_bubbles == [] and not board.shooting: # TODO: check if this is actually right print("Game over, better luck next time :(") running = False # board.drawBoard() # board.drawAllBubbles() # board.addToBoard() board.drawShootBubble() for event in pygame.event.get(): # this section handles the erasing of a popup after 5000 ticks if POPUP_COUNTER >= 0: POPUP_COUNTER += 1 if POPUP_COUNTER >= 20: POPUP_COUNTER = 0 i = 0 #TODO: find a way for it to find the popup -> board.current_popups.index(popped_bubble) #board.current_popups.pop(i).erase() board.success_popped = False # if not board.success_popped: # for p in board.current_popups: # p.erase() if board.future_bubbles == []: board.game_over = True board.future_bubbles = [(" ", WHITE)] # TODO: fix this kludge print("Game over, better luck next time :(") running = False if board.board_bubbles == []: # print a win message to the screen board.won = True print("You won! Good job! :)") won = True running = False if event.type == pygame.MOUSEBUTTONDOWN: pos = getPos() if board.board.collidepoint(pos): board.shooting = True # valid selection for bubble, shoot it e = board.shootBubble(pos) if e == "ValueError" or e == "IndexError" or e == "Game Over": running = False print("Game over, better luck next time :(") elif board.help_box.collidepoint(pos): # make help box appear board.displayHelpBox() else: # TODO: display an error pass if event.type == pygame.QUIT: running = False quitGame() # board.addToBoard() pygame.display.update() clock.tick(60) # make it pretty for the end of the game board.drawBoard() if won: # display a winning message p = Popup("Good job! You won!", int(DISPLAY_X * 0.35), int(DISPLAY_Y * 0.30), gameDisplay) else: board.drawAllBubbles() # display a better luck next time message p = Popup("Better luck next time! Try again!", int(DISPLAY_X * 0.25), int(DISPLAY_Y * 0.30), gameDisplay) p.create() exit_message = Popup("Press any key or click to exit game.", int(DISPLAY_X * 0.25), int(DISPLAY_Y * 0.34), gameDisplay) exit_message.create() pygame.display.update() paused = True while paused: for ev in pygame.event.get(): if ev.type == pygame.KEYDOWN or ev.type == pygame.MOUSEBUTTONDOWN: paused = False quitGame() elif ev.type == pygame.QUIT: paused = False quitGame()