def ready(self, *args, **kwargs): super(LogoScene, self).ready() self.background = Image("../resources/image/menu/whiteback.png", alpha=False) self.logo = Image("../resources/image/menu/kawaz.png", alpha=False) self.logo.x = 340 self.logo.y = 230 self.sprites.add(self.background) self.sprites.add(self.logo) self.timer = Timer(210) self.mouse = Mouse(0) self.joypads = [] for i in xrange(0, JoyPad.get_num_joypads()): self.joypads.append(JoyPad(i))
def __init__(self, number, world): u""" number : Player番号。0~3 world : Worldクラスインスタンス """ self.number = number super(Player, self).__init__(0, 0) self.index = self.number self.roads_queue = self._get_initial_roads_queue() self.current_road = self.get_next_road() self.next_road = self.get_next_road() self.cursor_counter_h = 0 self.cursor_counter_v = 0 self.city = City(self, world) self.device = JoyPad(number) self.pad_type = self.PAD_TYPE[self.device.id] self.mapping = self.KEYMAPPINGS_PADS[self.pad_type] self.cursor_threshold = [0, 0] self.cursor_move = False self.ainfo.index = self.number self.point = self.city.root_point.clone()
def __init__(self, n, interfaces=None): self.number = n self.x, self.y = local_to_global(self.initial_position[n]).to_pos() self.point = LocalPoint(self.initial_position[n]) self.interfaces = [] super(Player, self).__init__(u'../resources/image/main/player/cursor.png', area=pygame.rect.Rect(0,n*40,40,40),x=100, y=100) self.animation_enable = False self.joy = JoyPad() if not interfaces: self.set_type() else: self.interfaces = interfaces self.press_counter = [0,0,0,0,0,0] self.key_mode = False self.pre_mouse_point = LocalPoint(0,0)
def ready(self, *args, **kwargs): super(MainMenuScene, self).ready() self.bgm = BGM(u'../resources/music/title.wav', -1) self.cursor_sound = Sound("../resources/sound/cursor.wav") self.decide_sound = Sound('../resources/sound/decide.wav') self.decide_timer = Timer(settings.FPS*2.5) self.num_joypads = JoyPad.get_num_joypads() self.joypads = [] for i in xrange(0, self.num_joypads): self.joypads.append(JoyPad(i)) self.background = Image(os.path.join(self.IMAGE_PATH, "background3.png"), alpha=False) self.logo = Image(os.path.join(self.IMAGE_PATH, "logo.png")) self.config = Image(os.path.join(self.IMAGE_PATH, "config.png"), alpha=False) self.exit = Image(os.path.join(self.IMAGE_PATH, "exit.png"), alpha=False) self.cursor = Image(os.path.join(self.IMAGE_PATH, "cursor.png"), alpha=True) self.cursor_threshold = [[0, 0], ] * self.num_joypads # ジョイスティックを倒したときに、axisがどれくらい倒れたかの総量 self.cursor_move = [False] * self.num_joypads self.load_player_selection(self.num_joypads) self.logo.x = 280; self.logo.y = 20 self.player2.x = 160; self.player2.y = 400 self.player3.x = 380; self.player3.y = 400 self.player4.x = 600; self.player4.y = 400 self.config.x = 380; self.config.y = 460 self.exit.x = 600; self.exit.y = 460 # カーソル位置を初期化 self.options = ((self.player2, self.player3, self.player4), (None, self.config, self.exit)) self.actions = ((lambda:self.start_game(2), # self.player2 lambda:self.start_game(3), # self.player3 lambda:self.start_game(4),)# self.player4 , (lambda:0, # None lambda:Game.get_scene_manager().change_scene('keysetting'), #self.config lambda:sys.exit()) # self.exit ) self.cursor_logical_x = 0; self.cursor_logical_y = 0; self.set_cursor_pos(0, 0) self.sprites.add(self.background) self.sprites.add(self.logo) self.sprites.add(self.player2) self.sprites.add(self.player3) self.sprites.add(self.player4) self.sprites.add(self.config) self.sprites.add(self.exit) self.sprites.add(self.cursor) self.last_press_key = [{}] for dummy in self.joypads: self.last_press_key.append({})
class Player(Image): pressed = False initial_position = ( (settings.STAGE_WIDTH-1, settings.STAGE_HEIGHT-1), (0,0), ) def __init__(self, n, interfaces=None): self.number = n self.x, self.y = local_to_global(self.initial_position[n]).to_pos() self.point = LocalPoint(self.initial_position[n]) self.interfaces = [] super(Player, self).__init__(u'../resources/image/main/player/cursor.png', area=pygame.rect.Rect(0,n*40,40,40),x=100, y=100) self.animation_enable = False self.joy = JoyPad() if not interfaces: self.set_type() else: self.interfaces = interfaces self.press_counter = [0,0,0,0,0,0] self.key_mode = False self.pre_mouse_point = LocalPoint(0,0) def set_type(self): count = self.joy.get_count() r = count for id in xrange(r): if self.joy.sticks[id].get_numbuttons < 6: count -=1 if count == 0: types = ('mouse','key') elif count == 1: types = ('mouse', 'pad') else: types = ('pad', 'pad') self.interfaces.append(types[self.number]) def update(self): u''' なにかキーが押されたらkey_mode=True。このとき、マウス操作は利かない マウス座標が前と変わったら、key_mode=False ''' if not self.pre_mouse_point == self.get_mouse_point(): self.key_mode = False if 'mouse' in self.interfaces and not self.key_mode: self.point = self.get_mouse_point() if self.in_map(): Mouse.hide_cursor() else: Mouse.show_cursor() if 'key' in self.interfaces: if Key.is_press(K_UP): self.press_counter[0] += 1 self.key_mode = True elif Key.is_press(K_DOWN): self.press_counter[1] += 1 self.key_mode = True if Key.is_press(K_LEFT): self.press_counter[2] += 1 self.key_mode = True elif Key.is_press(K_RIGHT): self.press_counter[3] += 1 self.key_mode = True if 'pad' in self.interfaces: if self.joy.get_count() == 1: id = 0 else: id = self.number axes = self.joy.sticks[id].get_numaxes() >= 2 pad = self.joy.sticks[id] if pad.get_button(0) or (axes and pad.get_axis(1) < -0.5): self.press_counter[0] += 1 self.key_mode = True if pad.get_button(1) or (axes and pad.get_axis(1) > 0.5): self.press_counter[1] += 1 self.key_mode = True if pad.get_button(2) or (axes and pad.get_axis(0) < -0.5): self.press_counter[2] += 1 self.key_mode = True if pad.get_button(3) or (axes and pad.get_axis(0) > 0.5): self.press_counter[3] += 1 self.key_mode = True if self.press_counter[0] > 2: self.press_counter[0] = 0 self.point.y -= 1 if self.press_counter[1] > 2: self.press_counter[1] = 0 self.point.y += 1 if self.press_counter[2] > 2: self.press_counter[2] = 0 self.point.x -= 1 if self.press_counter[3] > 2: self.press_counter[3] = 0 self.point.x += 1 if self.point.x < 0: self.point.x = 0 elif self.point.x > settings.STAGE_WIDTH-2: self.point.x = settings.STAGE_WIDTH-2 if self.point.y < 0: self.point.y =0 elif self.point.y > settings.STAGE_HEIGHT-2: self.point.y = settings.STAGE_HEIGHT-2 self.move_pointer() self.pre_mouse_point = self.get_mouse_point() def move_pointer(self): if self.in_map(): self.x, self.y = self.point.to_global().add(LocalPoint(1,1)).to_pos() self.rect.x = self.x self.rect.y = self.y def poll(self): if 'mouse' in self.interfaces and not self.key_mode: if Mouse.is_press('LEFT') and not self.pressed: self.pressed = True return 1 elif Mouse.is_press('RIGHT') and not self.pressed: self.pressed = True return -1 if Mouse.is_release(self): self.pressed = False if 'key' in self.interfaces: if Key.is_press(K_x): if not self.press_counter[4]: self.press_counter[4] = 1 self.key_mode = True return -1 elif Key.is_press(K_z): if not self.press_counter[5]: self.press_counter[5] = 1 self.key_mode = True return 1 else: self.press_counter[4] = 0 self.press_counter[5] = 0 if 'pad' in self.interfaces: if self.joy.get_count() == 1: id = 0 else: id = self.number if self.joy.sticks[id].get_numbuttons >= 13: left = 12 right = 11 else: left = 5 right = 6 pad = self.joy.sticks[id] if pad.get_button(left): if not self.press_counter[4]: self.press_counter[4] = 1 return -1 elif pad.get_button(right): if not self.press_counter[5]: self.press_counter[5] = 1 return 1 else: self.press_counter[4] = 0 self.press_counter[5] = 0 return 0 def get_mouse_point(self): return global_to_local(Mouse.get_pos()) def in_map(self): lp = self.point return 0 <= lp.x < settings.STAGE_WIDTH-1 and 0 <= lp.y < settings.STAGE_HEIGHT-1
class Player(Panel): u"""プレイヤークラス""" IMAGEPATH = u"../resources/image/main/players.png" KEYMAPPINGS = {Device.Key:{'up':K_UP, 'down':K_DOWN, 'right':K_RIGHT, 'left':K_LEFT, 'z':K_z, 'x':K_x, 'c':K_c, 'START':K_RETURN }, Device.JoyPad:{'up':0, 'down':1, 'right':2, 'left':3, 'z':8, 'x':9, 'c':11, 'START':4 }} PAD_TYPE = [0, 0, 0, 1] KEYMAPPINGS_PADS = [{'up':0, 'down':1, 'right':2, 'left':3, 'z':8, 'x':9, 'c':11, 'START':4 }, {'up':0, 'down':1, 'right':7, 'left':6, 'z':6, 'x':7, 'c':1, 'START':9 }] def __init__(self, number, world): u""" number : Player番号。0~3 world : Worldクラスインスタンス """ self.number = number super(Player, self).__init__(0, 0) self.index = self.number self.roads_queue = self._get_initial_roads_queue() self.current_road = self.get_next_road() self.next_road = self.get_next_road() self.cursor_counter_h = 0 self.cursor_counter_v = 0 self.city = City(self, world) self.device = JoyPad(number) self.pad_type = self.PAD_TYPE[self.device.id] self.mapping = self.KEYMAPPINGS_PADS[self.pad_type] self.cursor_threshold = [0, 0] self.cursor_move = False self.ainfo.index = self.number self.point = self.city.root_point.clone() def update(self): self.city.update() if self.pad_type == 0: xaxis = self.device.get_axis(0) yaxis = self.device.get_axis(1) else: xaxis = self.device.get_axis(2) yaxis = self.device.get_axis(3) length = sum(map(lambda x: x*x, list(self.cursor_threshold))) if abs(xaxis) > 0.5: self.cursor_threshold[0] += xaxis if not self.cursor_move or abs(length) > 16: self.point.x += 1 if xaxis > 0 else -1 if abs(yaxis) > 0.5: self.cursor_threshold[1] += yaxis if not self.cursor_move or abs(length) > 16: self.point.y += 1 if yaxis > 0 else -1 if abs(xaxis) > 0.5 or abs(yaxis) > 0.5: self.cursor_move = True else: self.cursor_move = False self.cursor_threshold = [0, 0] if self.point.x < 0: self.point.x = 0 elif self.point.x > settings.STAGE_WIDTH-1: self.point.x = settings.STAGE_WIDTH-1 if self.point.y < 0: self.point.y = 0 elif self.point.y > settings.STAGE_HEIGHT-1: self.point.y = settings.STAGE_HEIGHT-1 self.current_road.point = self.point.clone() if not self.device.joy: return def draw(self, surface=Game.get_screen()): super(Player, self).draw(surface) self.city.draw() def poll(self): self.device.poll() if self.device.is_press(self.mapping['z']): return -1 elif self.device.is_press(self.mapping['x']): return 1 elif self.device.is_press(self.mapping['c']): return 2 elif self.device.is_press(self.mapping['START']): return 3 return 0 def get_next_road(self): if len(self.roads_queue) == 0: self.roads_queue = self._get_initial_roads_queue() return self.roads_queue.pop(0)(0, 0) def _get_initial_roads_queue(self): roads = [LShapeRoad, TShapeRoad, IShapeRoad, CrossRoad] random.shuffle(roads) return roads def attach_road(self): self.current_road.on_attach() self.current_road = self.next_road self.current_road.point = self.point self.next_road = self.get_next_road()
def ready(self, *args, **kwargs): super(MainMenuScene, self).ready() self.bgm = BGM(u'../resources/music/title.wav', -1) self.cursor_sound = Sound("../resources/sound/cursor.wav") self.decide_sound = Sound('../resources/sound/decide.wav') self.decide_timer = Timer(settings.FPS * 2.5) self.num_joypads = JoyPad.get_num_joypads() self.joypads = [] for i in xrange(0, self.num_joypads): self.joypads.append(JoyPad(i)) self.background = Image(os.path.join(self.IMAGE_PATH, "background3.png"), alpha=False) self.logo = Image(os.path.join(self.IMAGE_PATH, "logo.png")) self.config = Image(os.path.join(self.IMAGE_PATH, "config.png"), alpha=False) self.exit = Image(os.path.join(self.IMAGE_PATH, "exit.png"), alpha=False) self.cursor = Image(os.path.join(self.IMAGE_PATH, "cursor.png"), alpha=True) self.cursor_threshold = [ [0, 0], ] * self.num_joypads # ジョイスティックを倒したときに、axisがどれくらい倒れたかの総量 self.cursor_move = [False] * self.num_joypads self.load_player_selection(self.num_joypads) self.logo.x = 280 self.logo.y = 20 self.player2.x = 160 self.player2.y = 400 self.player3.x = 380 self.player3.y = 400 self.player4.x = 600 self.player4.y = 400 self.config.x = 380 self.config.y = 460 self.exit.x = 600 self.exit.y = 460 # カーソル位置を初期化 self.options = ((self.player2, self.player3, self.player4), (None, self.config, self.exit)) self.actions = ( ( lambda: self.start_game(2), # self.player2 lambda: self.start_game(3), # self.player3 lambda: self.start_game(4), ) # self.player4 , ( lambda: 0, # None lambda: Game.get_scene_manager().change_scene('keysetting' ), #self.config lambda: sys.exit()) # self.exit ) self.cursor_logical_x = 0 self.cursor_logical_y = 0 self.set_cursor_pos(0, 0) self.sprites.add(self.background) self.sprites.add(self.logo) self.sprites.add(self.player2) self.sprites.add(self.player3) self.sprites.add(self.player4) self.sprites.add(self.config) self.sprites.add(self.exit) self.sprites.add(self.cursor) self.last_press_key = [{}] for dummy in self.joypads: self.last_press_key.append({})