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 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
Пример #3
0
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)