def do_command(self, id): button = self.buttons[id] if id == 'full_screen': if MAGIC_POTATO.is_full_screen(): MAGIC_POTATO.set_full_screen(False) else: MAGIC_POTATO.set_full_screen(True) elif id == 'back': if len(self.buttons) == 5: self.next = self.bg else: from src.menus.TitleScene import TitleScene # because top of file didn't work self.next = TitleScene() elif id == 'main_menu': from src.menus.TitleScene import TitleScene # because top of file didn't work self.next = TitleScene()
def __init__(self, background_scene): self.next = None self.bg = background_scene self.buttons = { 'full_screen': [0] * 4, 'sfx_volume': [0] * 4, 'music_volume': [0] * 4, 'back': [0] * 4 } if self.bg != None: self.buttons.update({'main_menu': [0] * 4}) self.cursor = (0, 0) self.currently_over = None self.sfx_vol = MAGIC_POTATO.get_sound_volume() self.music_vol = MAGIC_POTATO.get_music_volume() self.mouse = 0 self.over = None
def _play_music(self, id): if id != None: pygame.mixer.music.set_volume(MAGIC_POTATO.get_music_volume()) if self.current_song != id: self.current_song = id songs = [] for t in id.split(','): songs.append(t.strip()) song = random.choice(songs) pygame.mixer.music.load('music' + os.sep + song + '.ogg') pygame.mixer.music.play(-1)
def do_sound(self, id): width_over_two = GAME_WIDTH // 2 mx, my = self.cursor if id == 'sfx_volume': if mx < (width_over_two - 20) and self.mouse == 0: pass else: MAGIC_POTATO.set_sound_volume((mx - width_over_two) // 2) self.sfx_vol = MAGIC_POTATO.get_sound_volume() elif id == 'music_volume': if mx < (width_over_two - 20) and self.mouse == 0: pass else: MAGIC_POTATO.set_music_volume((mx - width_over_two) // 2) self.music_vol = MAGIC_POTATO.get_music_volume()
def _play_sound(self, id): if id != None: snd = self.sounds.get(id, None) if snd == None: snd = [] for t in id.split(','): t = t.strip() file = id if not file.endswith('.wav'): file += '.ogg' snd.append(pygame.mixer.Sound('sfx' + os.sep + file)) self.sounds[id] = snd sound = None if len(snd) == 1: sound = snd[0] elif len(snd) > 1: sound = random.choice(snd) if sound != None: sound.set_volume(MAGIC_POTATO.get_sound_volume()) sound.play()
def render(self, screen, render_counter): if self.bg != None: self.bg.render(screen, render_counter) if self.bg == None: screen.fill((0,0,0)) draw_alpha_rectangle(screen, 0, 0, GAME_WIDTH, GAME_HEIGHT, 40, 40, 40, 200) mx, my = self.cursor y = GAME_HEIGHT // 3 x = GAME_WIDTH // 5 width_over_two = GAME_WIDTH // 2 current = None options = [('Full Screen', 'full_screen'),('SFX Volume', 'sfx_volume'),('Music Volume', 'music_volume'),('Back', 'back')] if len(self.buttons) == 5: options.append(('Return to Main Menu','main_menu')) for option in options: label, id = option button = self.buttons[id] hover = mx > button[0] and mx < button[2] and my > button[1] and my < button[3] if self.over == id: current = id button_color = (255, 255, 255, 255) text_color = 'white' sfx_color = (255, 0, 0, 255) music_color = (0, 255, 0, 255) elif hover and (self.over == None): current = id button_color = (255, 255, 255, 255) text_color = 'white' sfx_color = (255, 0, 0, 255) music_color = (0, 255, 0, 255) else: button_color = (150, 150, 150, 150) text_color = 'gray' sfx_color = (180, 0, 0, 180) music_color = (0, 180, 0, 180) coords = TEXT.render(screen, label, text_color, x, y) if id == 'full_screen': self.buttons[id] = (x, y, width_over_two + 20, coords[1]) pygame.draw.rect(screen, button_color, pygame.Rect(width_over_two, y, 15, 15), 0 if MAGIC_POTATO.is_full_screen() else 1) elif id == 'sfx_volume': pygame.draw.line(screen, button_color, (width_over_two, y + 7), (width_over_two + 200, y + 7), 2) for i in [18, 36, 55, 73, 91, 109, 127, 145, 164, 182]: # ours go to 11! pygame.draw.line(screen, button_color, (width_over_two + i, y + 1), (width_over_two + i, y + 14), 1) for i in [0, 200]: # end lines pygame.draw.line(screen, button_color, (width_over_two + i, y - 1), (width_over_two + i, y + 16), 1) pygame.draw.rect(screen, sfx_color, pygame.Rect(width_over_two + (self.sfx_vol * 2) - 1, y + 3, 3, 10), 0) self.buttons[id] = (x, y, width_over_two + 220, coords[1]) elif id == 'music_volume': pygame.draw.line(screen, button_color, (width_over_two, y + 7), (width_over_two + 200, y + 7), 2) for i in [18, 36, 55, 73, 91, 109, 127, 145, 164, 182]: # ours go to 11! pygame.draw.line(screen, button_color, (width_over_two + i, y + 1), (width_over_two + i, y + 14), 1) for i in [0, 200]: # end lines pygame.draw.line(screen, button_color, (width_over_two + i, y - 1), (width_over_two + i, y + 16), 1) pygame.draw.rect(screen, music_color, pygame.Rect(width_over_two + (self.music_vol * 2) - 1, y + 3, 3, 10), 0) self.buttons[id] = (x, y, width_over_two + 220, coords[1]) else: self.buttons[id] = (x, y, coords[0], coords[1]) y += 30 self.currently_over = current
def main(): pygame.init() is_fullscreen = False resizeable = pygame.RESIZABLE real_screen = pygame.display.set_mode(SCREEN_SIZE, resizeable) pygame.display.set_caption("Swamped Sysadmins") virtual_screen = pygame.Surface((GAME_WIDTH, GAME_HEIGHT)).convert_alpha() active_scene = TitleScene() mouse_pos = (0, 0) counter = 0 while True: begin = time.time() if MAGIC_POTATO.is_full_screen() != is_fullscreen: is_fullscreen = MAGIC_POTATO.is_full_screen() if is_fullscreen: real_screen = pygame.display.set_mode((800, 600), pygame.FULLSCREEN) else: real_screen = pygame.display.set_mode(SCREEN_SIZE, resizeable) SND.ensure_music_volume() events = [] last_mouse_event = None for e in pygame.event.get(): if e.type == pygame.MOUSEBUTTONDOWN: mouse_pos = e.pos x, y = mouse_pos last_mouse_event = Event('mousedown', x, y, e.button == 1) events.append(last_mouse_event) elif e.type == pygame.MOUSEBUTTONUP: mouse_pos = e.pos x, y = mouse_pos last_mouse_event = Event('mouseup', x, y, e.button == 1) events.append(last_mouse_event) elif e.type == pygame.MOUSEMOTION: mouse_pos = e.pos x, y = mouse_pos last_mouse_event = Event('mousemove', x, y, False) events.append(last_mouse_event) elif e.type == pygame.QUIT: return elif e.type == pygame.KEYDOWN: pressed_keys = pygame.key.get_pressed() if e.key == pygame.K_F4 and (pressed_keys[pygame.K_LALT] or pressed_keys[pygame.K_RALT]): return elif e.key == pygame.K_w and (pressed_keys[pygame.K_LCTRL] or pressed_keys[pygame.K_RCTRL]): return elif e.key == pygame.K_ESCAPE: return else: events.append(Event('keydown', e.key, 0, False)) elif e.type == pygame.VIDEORESIZE: w, h = e.size SCREEN_SIZE[0] = w SCREEN_SIZE[1] = h real_screen = pygame.display.set_mode(SCREEN_SIZE, resizeable) if last_mouse_event != None: mouse_pos = (last_mouse_event.x, last_mouse_event.y) active_scene.update(events, mouse_pos) active_scene.render(virtual_screen, counter) pygame.transform.scale(virtual_screen, real_screen.get_size(), real_screen) pygame.display.flip() next_scene = active_scene.next if next_scene != None: active_scene.next = None active_scene = next_scene if active_scene == 'exit': return counter += 1 end = time.time() diff = end - begin delay = 1.0 / FPS wait = delay - diff if wait > 0: time.sleep(wait)
def ensure_music_volume(self): potato_volume = MAGIC_POTATO.get_music_volume() / 100.0 mixer_volume = pygame.mixer.music.get_volume() if abs(potato_volume - mixer_volume) > .001: pygame.mixer.music.set_volume(potato_volume)