def __init__(self, location): logging.debug("Initialising music controls") # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) font = pygame.font.Font(get_font('arial.ttf'), 15) self.font_surface = font.render("Music", 1, (0, 0, 0)) # Make the image self.image = pygame.Surface((120, 50 + self.font_surface.get_height() - 5)) self.image.fill(BLUE) self.offset = (20, 20) self.rect = self.image.get_rect() self.rect.topleft = self.offset self.buttons = [] self.button_group = pygame.sprite.Group() Button.containers = self.button_group self.buttons.append(Button(self.image, "", PLAY_PAUSE_SWAP_IMAGE, "width/4", "25+(height-50)", image="Buttons\\pause_button.png", highlight_image="Buttons\\pause_button_highlighted.png", pressed_image="Buttons\\pause_button_pressed.png", type="play pause", init_command=PLAY_PAUSE_LOAD_IMAGES)) self.buttons.append(Button(self.image, "", "", "width*3/4", "25+(height-50)", image="Buttons\\skip_forwards_button.png", highlight_image="Buttons\\skip_forwards_button_highlighted.png", pressed_image="Buttons//skip_forwards_button_pressed.png", type="skip")) self.button_group.draw(self.image) font = pygame.font.Font(get_font('arial.ttf'), 15) self.font_surface = font.render("Music", 1, (255, 255, 255)) # Create music self.music = Music(location) self.music.play_music()
def __init__(self, screen, text, command, x, y, width=0, height=0, image="Buttons\\button.png", highlight_image="Buttons\\button_highlighted.png", pressed_image="Buttons\\button_pressed.png", init_command="", text_size=24, text_align="center", image_align="center", type="Button", press_method="click"): # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) # Make the image self.image = resize_image(width, height, image) self.image_align = image_align # Set up the rect for drawing # if the width or height is 0, that is the default width or height for the image if width == 0: self.rect_width = self.image.get_width() else: self.rect_width = width if height == 0: self.rect_height = self.image.get_height() else: self.rect_height = height self.rect = pygame.Rect(0, 0, self.rect_width, self.rect_height) if self.image_align == "center": self.rect.center = eval(x, {"width":screen.get_width()}), eval(y, {"height":screen.get_height()}) elif self.image_align == "topleft": self.rect.topleft = eval(x, {"width":screen.get_width()}), eval(y, {"height":screen.get_height()}) # Stores the equations for positions self.position_formula = x, y # Store the images self.plain_image = resize_image(width, height, image) self.highlight_image = resize_image(width, height, highlight_image) self.pressed_image = resize_image(width, height, pressed_image) # This gets whether the mouse pressed self self.pressed = 0 self.text_size = text_size self.text = text self.text_align = text_align self.image_locations = [get_image(image), get_image(highlight_image), get_image(pressed_image)] self.type = type font = pygame.font.Font(get_font('arial.ttf'), text_size) font_surface = font.render(text, 1, (0, 0, 0)) font_size = font.size(text) if text_align == "center": self.plain_image.blit(font_surface, (self.image.get_width() / 2 - font_size[0] / 2, self.image.get_height() / 2 - font_size[1] / 2)) self.highlight_image.blit(font_surface, (self.image.get_width() / 2 - font_size[0] / 2, self.image.get_height() / 2 - font_size[1] / 2)) self.pressed_image.blit(font_surface, (self.image.get_width() / 2 - font_size[0] / 2, self.image.get_height() / 2 - font_size[1] / 2)) elif text_align == "left": self.plain_image.blit(font_surface, (20, self.image.get_height() / 2 - font_size[1] / 2)) self.highlight_image.blit(font_surface, (20, self.image.get_height() / 2 - font_size[1] / 2)) self.pressed_image.blit(font_surface, (20, self.image.get_height() / 2 - font_size[1] / 2)) # This is the command run when the button is pressed self.command = command self.init_command = init_command self.press_method = press_method # Run the init_command self.run_init_command()
def run_game(screen, level): level_file = get_level(str(level) + ".txt") with open(level_file) as l: level_objects = l.read() # Get level items level_items, player_items, game_items = level_objects.split("--\n") level_items = level_items.split("\n") # Create the side menu and game menu_group = pygame.sprite.Group() side_menu.containers = menu_group # Initialise the side menu side = side_menu(screen, player_items.split("\n")) text = [" "] # How long each text lasts for text_time = 300 text_timer = 0 # Initialise game for item in level_items: item = item.split(":") if item[0] == "Screen": game_size = item[1].split(",") if item[0] == "Text": text.append(item[1]) if item[0] == "Cutscene": cut = Cutscene(screen, item[1]) show_cutscene = True while show_cutscene: screen.fill((0, 0, 0)) screen.blit(cut.image, (screen.get_width() / 2 - cut.image.get_width() / 2, screen.get_height() / 2 - cut.image.get_height() / 2)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: show_cutscene = False # If the window is resized then update all objects if event.type == pygame.VIDEORESIZE: old_screen = pygame.display.get_surface() screen = pygame.display.set_mode((max(event.size[0], 700), max(event.size[1], 500)), old_screen.get_flags()) side.update_pos(screen) play_area.update_size(screen) if item[0] == "Credits": credits(screen) font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(" ", 1, (255, 255,255)) with open(get_file("Levels/levels_info.txt")) as f: num_levels = f.readline() unlocked_levels = int(f.readline()) with open(get_file("Levels/levels_info.txt"), "w") as f: max_level = max(unlocked_levels, level + 1) f.write(num_levels + str(max_level)) return False play_area = Game(screen, (int(game_size[0]), int(game_size[1])), game_items.split("\n")) # Create music and additional music data music_group = pygame.sprite.Group() Music_Controls.containers = music_group music = Music_Controls(str(ceil((level * 8)/17))) music.play_music(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) # Create the clock # This is used to limit the frame-rate clock = pygame.time.Clock() # Initialise the mouse mouse = Mouse() while True: for event in pygame.event.get(): # If the close button is pressed then quit if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: side.commands.append("play game") side.play() # If the window is resized then update all objects if event.type == pygame.VIDEORESIZE: old_screen = pygame.display.get_surface() screen = pygame.display.set_mode((max(event.size[0], 700), max(event.size[1], 500)), old_screen.get_flags()) side.update_pos(screen) play_area.update_size(screen) font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(text[0], 1, (255, 255,255)) offset = [text_render.get_width() / 2, text_render.get_height() / 2] text_position = ((screen.get_width() / 2 - offset[0], screen.get_height() / 2 - offset[1])) if event.type == pygame.KEYUP: if event.key == pygame.K_F11: # get the flags of the surface old_screen = pygame.display.get_surface() screen = pygame.display.set_mode((MAX_SIZE[0], MAX_SIZE[1]) if old_screen.get_flags() == pygame.FULLSCREEN else SCREEN_SIZE, pygame.RESIZABLE if old_screen.get_flags() == pygame.FULLSCREEN else pygame.FULLSCREEN) font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(text[0], 1, (255, 255,255)) offset = [text_render.get_width() / 2, text_render.get_height() / 2] text_position = ((screen.get_width() / 2 - offset[0], screen.get_height() / 2 - offset[1])) # Update the side menu side.update(screen, play_area.pieces) play_area.pieces = [] if "menu" in side.commands: font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(" ", 1, (255, 255,255)) return True if "level select" in side.commands: font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(" ", 1, (255, 255,255)) return False if "restart game" in side.commands: level_file = get_level(str(level) + ".txt") with open(level_file) as l: level_objects = l.read() # Get level items level_items, player_items, game_items = level_objects.split("--\n") level_items = level_items.split("\n") #screen = pygame.display.set_mode(screen.get_size(), pygame.RESIZABLE) # Create the side menu and game menu_group = pygame.sprite.Group() side_menu.containers = menu_group # Initialise the side menu side = side_menu(screen, player_items.split("\n")) # Initialise game for item in level_items: item = item.split(":") if item[0] == "Screen": game_size = item[1].split(",") play_area = Game(screen, (int(game_size[0]), int(game_size[1])), game_items.split("\n")) # Update the play area play_area.move(side.commands) # Remove the used commands side.commands = [] # Draw the menu menu_group.draw(screen) screen_fill = pygame.Rect(play_area.draw_area) screen_fill.topleft = (0, 0) screen.fill((10, 20, 20), screen_fill) # Draw the game screen.blit(play_area.image, (0, 0), area=play_area.draw_area) # Draw the music controls music_group.draw(screen) # Draw text if text_timer == 0: text.pop(0) if len(text) == 0: text.append(" ") font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(text[0], 1, (255, 255,255)) offset = [text_render.get_width() / 2, text_render.get_height() / 2] text_position = ((screen.get_width() / 2 - offset[0], screen.get_height() / 2 - offset[1])) text_timer = text_time screen.blit(text_render, text_position) text_timer -= 1 # Draw mouse if pygame.mouse.get_focused(): x, y = pygame.mouse.get_pos() screen.blit(mouse.image, (x - 5, y - 5)) # Draw screen pygame.display.update() # Cap framerate clock.tick(60) music.play_music(pygame.mouse.get_pos(), pygame.mouse.get_pressed()) if play_area.finish == True: if play_area.loose == False: with open(get_file("Levels/levels_info.txt")) as f: num_levels = f.readline() unlocked_levels = int(f.readline()) with open(get_file("Levels/levels_info.txt"), "w") as f: max_level = max(unlocked_levels, level + 1) f.write(num_levels + str(max_level)) font = pygame.font.Font(get_font('arial.ttf'), int(min((screen.get_width() / 700) * 20, 32))) text_render = font.render(" ", 1, (255, 255,255)) return False
def credits(screen): credits_location = get_level("credits.txt") with open(credits_location) as l: credit_modules = l.read() credit_modules = credit_modules.split("--") for module in credit_modules: items = module.split("\n") static_items = [] static_item_pos = [] buttons = pygame.sprite.Group() Button.containers = buttons for item in items: item = item.split(":") if item[0] == "Header": item[1] = item[1].split(",") font = pygame.font.Font(get_font('arial.ttf'), int(item[1][6])) static_items.append(font.render(item[1][0], 1, (int(item[1][1]), int(item[1][2]), int(item[1][3])))) if item[1][7] == "centre": offset = [static_items[-1].get_width() / 2, static_items[-1].get_height() / 2] else: offset = [0,0] static_item_pos.append((item[1][4] + "-" + str(offset[0]), item[1][5] + "-" + str(offset[1]))) elif item[0] == "Button": item[1] = item[1].split(",") Button(screen, "", PLAY_MUSIC, item[1][0], item[1][1], width=150, height=150, image=item[1][2], highlight_image=item[1][2], pressed_image=item[1][2], image_align="topleft", init_command="self.web=\"" + item[1][3].replace(";", ":") + "\"") # Initialise the mouse mouse = Mouse() finished = False while not finished: screen.fill((0, 0, 0)) i = 0 for item in static_items: x = eval(static_item_pos[i][0], {"width":screen.get_width()}) y = eval(static_item_pos[i][1], {"height":screen.get_height()}) screen.blit(item,(x,y)) i+=1 x, y = pygame.mouse.get_pos() buttons.update((x, y), pygame.mouse.get_pressed(), screen) buttons.draw(screen) screen.blit(mouse.image, (x - 5, y - 5)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pass if event.type == pygame.KEYDOWN: finished = True # If the window is resized then update all objects if event.type == pygame.VIDEORESIZE: old_screen = pygame.display.get_surface() screen = pygame.display.set_mode((max(event.size[0], 700), max(event.size[1], 500)), old_screen.get_flags()) side.update_pos(screen) play_area.update_size(screen)