def __init__(self, lib_mode = False): self.init_pygame() self.lib_mode = lib_mode self.black = pygame.color.Color("black") self.white = pygame.color.Color("white") self.font = pygame.font.Font("freesansbold.ttf", 16) if not lib_mode: self.scr = Screen((700, 50), "DSConfig") self.parser = ConfigParser.ConfigParser()
class DSConfig(): def init_pygame(self): pygame.init() pygame.joystick.init() pygame.display.init() def __init__(self, lib_mode = False): self.init_pygame() self.lib_mode = lib_mode self.black = pygame.color.Color("black") self.white = pygame.color.Color("white") self.font = pygame.font.Font("freesansbold.ttf", 16) if not lib_mode: self.scr = Screen((700, 50), "DSConfig") self.parser = ConfigParser.ConfigParser() '''Wait for a button press in a joystick, and return the joystick.''' def wait_for_joystick(self): print pygame.joystick.get_count() for i in range(1, pygame.joystick.get_count() + 1): # Joystick init. print i DSLib.Joystick(i) while True: e = pygame.event.wait() if e.type == pygame.JOYBUTTONDOWN: print e.joy return DSLib.Joystick(e.joy + 1) elif e.type == pygame.QUIT: pygame.quit(); sys.exit(0) '''Wait for a button press and return this button. 'joy' argument: The Joystick that you want to capture the button. 'buttons' argument: (optional) The list of Buttons that you want to exclude.''' def wait_for_button(self, joy, buttons = []): btnnumbers = [] for i in buttons: btnnumbers.append(i.get_name()) while True: e = pygame.event.wait() if e.type == pygame.JOYBUTTONDOWN and e.joy == joy.get_order() - 1 and (e.button + 1) not in btnnumbers: return DSLib.Button(e.button + 1, joy) elif e.type == pygame.QUIT: pygame.quit(); sys.exit(0) def write(self, text): self.scr.fill(self.black) self.scr.write_text((0,0), text, self.font, self.white) self.scr.flip() def main(self): buttons = [] self.write("Let's start: press a button on your joystick") joy = self.wait_for_joystick() self.write("Okay. Now press a button for green fret.") buttons.append(self.wait_for_button(joy, buttons)) self.write("A good start! Now press a button for red fret.") buttons.append(self.wait_for_button(joy, buttons)) self.write("Right! Now press a button for yellow fret.") buttons.append(self.wait_for_button(joy, buttons)) self.write("Really good! Now press a button for blue fret.") buttons.append(self.wait_for_button(joy, buttons)) self.write("Almost finished! Now press a button for the orange fret.") buttons.append(self.wait_for_button(joy, buttons)) # Adding to config file. self.parser.add_section("Buttons") self.parser.set("Buttons", "joystick", str(joy.get_order())) self.parser.set("Buttons", "green", str(buttons[0].get_name())) self.parser.set("Buttons", "red", str(buttons[1].get_name())) self.parser.set("Buttons", "yellow", str(buttons[2].get_name())) self.parser.set("Buttons", "blue", str(buttons[3].get_name())) self.parser.set("Buttons", "orange", str(buttons[4].get_name())) with open("buttons.cfg", "w") as f: self.parser.write(f) while True: pygame.time.delay(1000/50) self.write("Finished! Now, at game, set ENTER as the pick and buttons on frets.") e = pygame.event.wait() if e.type == pygame.QUIT: pygame.quit()
def main(self): scr = Screen((400, 300), "IGNORE ME") white = pygame.color.Color("white") black = pygame.color.Color("black") orange = pygame.color.Color("orange") font48 = pygame.font.Font("freesansbold.ttf", 48) font12 = pygame.font.Font("freesansbold.ttf", 12) font16 = pygame.font.Font("freesansbold.ttf", 16) menu_position = 0 menu_items = {"Time for first press:": self.time_window, "Time for repress:": self.time_repress} while True: scr.fill(black) scr.write_text((100, 0), "Go play!", font48, white) scr.write_text((25, 72), "If you want to exit, press ALT-F4 or click the X Button", font12, white) i = 82 for k, v in self.parser.items("Buttons"): i += 13 if k == "joystick": scr.write_text((10, i), "Joystick: (%d) %s" % (self.joystick.get_order(), self.joystick.get_name()), font12, white) else: scr.write_text((10, i), "%s button: %s" % (k.capitalize(), v), font12, white) i += 20 for k, v in menu_items.iteritems(): i += 16 if menu_items.keys()[menu_position] == k: scr.write_text((10, i), "%s < %d >" % (k, v), font16, orange) else: scr.write_text((10, i), "%s < %d >" % (k, v), font16, white) scr.flip() pygame.event.pump() pygame.event.clear() while True: e = pygame.event.wait() if e.type in [pygame.QUIT, pygame.JOYBUTTONDOWN, pygame.KEYDOWN]: break # Event manegament if e.type == pygame.QUIT: self.handleExitEvent() elif e.type == pygame.JOYBUTTONDOWN and DSLib.Button(e.button + 1, joystick) in [self.green, self.red, self.yellow, self.blue, self.orange]: self.handleJoyEvent() elif e.type == pygame.KEYDOWN and e.key in [pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT]: if e.key == pygame.K_UP and menu_position > 0: menu_position -= 1 elif e.key == pygame.K_DOWN and menu_position < 1: menu_position += 1 elif e.key == pygame.K_LEFT: if menu_items.values()[menu_position] > 0: menu_items[menu_items.keys()[menu_position]] -= 1 if menu_position == 0: self.time_window -= 1 elif menu_position == 1: self.time_repress -= 1 elif e.key == pygame.K_RIGHT: menu_items[menu_items.keys()[menu_position]] += 1 if menu_position == 0: self.time_window += 1 elif menu_position == 1: self.time_repress += 1