def __init__(self, x, y, width, height, move=True): # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) self.type = "Trigger Right" # Make the image self.image = pygame.Surface((width, height), pygame.SRCALPHA) self.image.fill((0, 0, 0, 0)) pygame.draw.rect(self.image, (255, 80, 21), self.image.get_rect(), 3) left_arrow = pygame.image.load(get_image("Gameplay_Objects\\right_arrow.png")) self.image.blit(left_arrow, (width / 2 - left_arrow.get_width() / 2, height / 2 - left_arrow.get_height() / 2)) self.rect = pygame.Rect((x, y), (width, height)) self.x = x self.y = y self.width = width self.height = height self.move = move
def resize_image(width, height, location): image = pygame.image.load(get_image(location)).convert_alpha() if width == 0: width = image.get_width() if height == 0: height = image.get_height() image = pygame.transform.scale(image, (width, height)) return image
def __init__(self, x, y, move=True): # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) self.type = "Black Hole" # Make the image self.image = pygame.image.load(get_image("Gameplay_Objects\\black_hole.png")) self.rect = self.image.get_rect() self.rect.center = x, y self.mass = 5 self.x = x self.y = y self.move = move
def __init__(self, screen, image): image = "Static_Images/Cutscenes/" + image self.image = pygame.image.load(get_image(image)).convert_alpha() if screen.get_width() > screen.get_height(): width = int(screen.get_height() / self.image.get_height() * self.image.get_width()) height = int(screen.get_height()) self.image = pygame.transform.scale(self.image, (width, height)) # If the window is taller than it is wide # Resize the cutscene so it fits horizontally elif screen.get_width() < screen.get_height(): width = int(screen.get_width()) height = int(screen.get_width() / self.image.get_width() * screen.get_height()) self.image = pygame.transform.scale(self.image, (width, height)) else: width = int(screen.get_width()) height = int(screen.get_height()) self.image = pygame.transform.scale(self.image, (width, height))
def __init__(self, x, y, width, height): # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) # Make the image self.image = pygame.Surface((width, height), pygame.SRCALPHA) self.image.fill((0, 0, 0, 0)) # Make the image clouds = pygame.image.load(get_image("Gameplay_Objects\\clouds.png")) self.image.blit(clouds, (0, 0)) #pygame.draw.rect(self.image, (255, 0, 0), self.image.get_rect(), 3) self.rect = pygame.Rect((x, y), (width, height)) self.x = x self.y = y self.width = width self.height = height self.move = False
def __init__(self, screen, x, y, width=0, height=0, image="button.png"): # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) # Load the image self.image = pygame.image.load(get_image(image)).convert_alpha() # 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: rect_width = self.image.get_width() else: rect_width = width if height == 0: rect_height = self.image.get_height() else: rect_height = height self.rect = pygame.Rect(0, 0, rect_width, rect_height) self.rect.center = eval(x, {"width":screen.get_width()}), eval(y, {"height":screen.get_height()}) # Stores the equations for positions self.position_formula = x, y
def __init__(self, x, y, angle, velocity): logging.debug("Initialising player") logging.info("Player position: " + str(x) + ", " + str(y)) logging.info("Player vector (Magnitude, direction): " + str(velocity) + ", " + str(angle)) # Initialise the sprite module pygame.sprite.Sprite.__init__(self, self.containers) # Make the image self.image = pygame.image.load(get_image("Gameplay_Objects\\planet.png")) self.rect = self.image.get_rect() self.rect.center = x, y self.mass = 1 self.x, self.y = x, y self.velocity = velocity self.angle = angle self.lastangle = self.angle #work out the position of where this object would have been 1 frame ago self.lastpos = (self.x + (math.sin(self.angle + 3.14159265359)*self.velocity), self.y - (math.cos(self.angle + 3.14159265359)*self.velocity)) self.killed = False; self.finish = False
def run_menu(): logging.info("Initialising pygame") # Initialise pygame modules pygame.init() logging.info("Loading music") music = Music("Menu") music.play_music() logging.info("Creating new screen, overriding old") logging.info("Screen size: " + str(SCREEN_SIZE)) logging.info("Screen flags: pygame.FULLSCREEN") # Create screen with size START_SIZE and allow resizing, give it an icon old_screen = pygame.display.get_surface() if old_screen: old_size = old_screen.get_size() screen = pygame.display.set_mode((max(old_size[0], MAX_SIZE[0]), max(old_size[1], MAX_SIZE[1])), old_screen.get_flags()) # Create the screen else: screen = pygame.display.set_mode(SCREEN_SIZE, pygame.FULLSCREEN) pygame.display.set_icon(pygame.image.load(get_image("Static_Images\\icon.png"))) # Hide the mouse pygame.mouse.set_visible(0) # Create a group to control and contain the button and image sprites static_items = pygame.sprite.Group() Button.containers = static_items Image.containers = static_items logging.info("Creating menu buttons") # Create the buttons start_button = Button(screen, "Start", "self.exit = True", "width/2", "((height - 220) / 8) * 1 + 220", init_command="self.exit = False") options_button = Button(screen, "Help", "", "width/2", "((height - 220) / 8) * 3 + 220", type="about") quit_button = Button(screen, "Quit", "import sys; sys.exit()", "width/2", "((height - 220) / 8) * 5 + 220") logging.info("Creating title") # Create the title Image(screen, "width/2", "120", image="Static_Images\\game_title.png") logging.info("Creating clock") # This is used to cap the framerate clock = pygame.time.Clock() logging.info("Creating mouse") mouse = Mouse() logging.info("Running menu event loop") # Run the menu while not start_button.exit: # Clear the screen screen.fill(BACKGROUND) # Run through events for event in pygame.event.get(): # If close pressed then quit game if event.type == pygame.QUIT: logging.info("Close pressed, closing") sys.exit() # If screen is resized then move items to new positions if event.type == pygame.VIDEORESIZE: logging.debug("Screen resized, requested size: " + str(event.size[0]) + ", " + str(event.size[1])) # maintain the flags of the surface old_screen = pygame.display.get_surface() screen = pygame.display.set_mode((max(event.size[0], 400), max(event.size[1], 400)), old_screen.get_flags()) logging.info("Moving item positions") for item in static_items: item.update_pos(screen) if event.type == pygame.KEYUP: if event.key == pygame.K_F11: logging.debug("Fullscreen activated") # get the flags of the surface old_screen = pygame.display.get_surface() logging.info("Resizing screen") screen = pygame.display.set_mode((START_SIZE[0], START_SIZE[1]) if old_screen.get_flags() == pygame.FULLSCREEN else SCREEN_SIZE, pygame.RESIZABLE if old_screen.get_flags() == pygame.FULLSCREEN else pygame.FULLSCREEN) # Update and draw the static items for item in static_items: pressed = item.update(pygame.mouse.get_pos(), pygame.mouse.get_pressed(), screen) if pressed: if item.type == "about": for item in ["help1", "help2", "help3", "help4"]: cut = Cutscene(screen, "../" + item + ".png") 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) static_items.draw(screen) # Draw mouse if pygame.mouse.get_focused(): screen.blit(mouse.image, pygame.mouse.get_pos()) # Draw frame pygame.display.update() # Play music music.play_music() # if the song playing is from the menu songs then stop the music if music.song in music.songs: music.stop_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()